package com.sybotan.android.demo.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.sybotan.android.demo.R;


/**
 * 设备关系
 */

public class ObjectRelationDialog extends Dialog implements View.OnClickListener {

    private TextView relationTv;
    private Submit submit;

    public ObjectRelationDialog(@NonNull Context context) {
        super(context, R.style.style_dialog);
        setDisplay(context);
        init();
    }

    private void init() {
        relationTv = findViewById(R.id.relationTv);
        findViewById(R.id.toleft).setOnClickListener(this);
        findViewById(R.id.toright).setOnClickListener(this);
        findViewById(R.id.connect).setOnClickListener(this);
        findViewById(R.id.btn_submit).setOnClickListener(this);
    }

    public void setDisplay(Context context) {
        setContentView(R.layout.dialog_object_relation);
        setProperty();
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialog.dismiss();
            }
        });
    }

    public void setProperty() {
        Window window = getWindow();//得到对话框的窗口．
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;// 设置对话框的位置．0为中间
        wl.y = 0;
        wl.alpha = 1;// 设置对话框的透明度
        wl.gravity = Gravity.CENTER;
        window.setAttributes(wl);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toleft:
                relationTv.setText("->");
                break;
            case R.id.toright:
                relationTv.setText("<-");
                break;
            case R.id.connect:
                relationTv.setText("--");
                break;
            case R.id.btn_submit:
                if (submit != null) {
                    submit.onSubmit();
                }
                dismiss();
                break;
            default:
                break;
        }
    }

    public interface Submit {
        void onSubmit();
    }

    public void setOnSubmit(Submit submit) {
        this.submit = submit;
    }

}