package com.sybotan.android.demo.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
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.framework.mvvm.model.db.entity.task.ObjectEntity;
import com.sybotan.android.demo.R;
import com.sybotan.android.demo.bean.PipeConfigEntityPackClass;
import com.sybotan.android.demo.tools.ToastUtils;

import java.util.Objects;


/**
 * 设备关系
 */

public class PipeDialog extends Dialog implements View.OnClickListener {

    private TextView relationTv;
    private Submit submit;
    private TextView toleft;
    private TextView toright;
    private TextView connect;

    public PipeDialog(@NonNull Context context, String localId, ObjectEntity equip, PipeConfigEntityPackClass configEntity) {
        super(context, R.style.style_dialog);
        setDisplay(context);
        init(localId, equip, configEntity);
    }

    private void init(String localId, ObjectEntity equip, PipeConfigEntityPackClass configEntity) {
        relationTv = findViewById(R.id.relationTv);
        TextView leftName = findViewById(R.id.leftName);
        TextView rightName = findViewById(R.id.rightName);
        TextView relationTitle = findViewById(R.id.relationTitle);
        findViewById(R.id.toleft).setOnClickListener(this);
        findViewById(R.id.toright).setOnClickListener(this);
        findViewById(R.id.connect).setOnClickListener(this);
        findViewById(R.id.btnSubmit).setOnClickListener(this);
        toleft = findViewById(R.id.toleft);
        toright = findViewById(R.id.toright);
        connect = findViewById(R.id.connect);

        relationTitle.setText(configEntity.pipeTypeName);

        if (Objects.equals(configEntity.entity.getClassCodeFrom(), equip.getClassCode())) {
            leftName.setText(localId);
            rightName.setText(equip.getLocalId());
        } else {
            rightName.setText(localId);
            leftName.setText(equip.getLocalId());
        }
        for (PipeConfigEntityPackClass.TargetPipeType type : configEntity.targetTypes) {
            if (type.targetCode.equals(equip.getClassCode())) {
                if (type.canFrom) {
                    toleft.setVisibility(View.VISIBLE);
                }
                if (type.canTo) {
                    toright.setVisibility(View.VISIBLE);
                }
                if (type.canConnect) {
                    connect.setVisibility(View.VISIBLE);
                }
                break;
            }
        }
    }

    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.btnSubmit:
                if (TextUtils.isEmpty(relationTv.getText().toString().trim())) {
                    ToastUtils.showMyToast("请选择方向");
                    return;
                }
                if (submit != null) {
                    submit.onSubmit(relationTv.getText().toString());
                }
                dismiss();
                break;
            default:
                break;
        }
    }

    public interface Submit {
        void onSubmit(String s);
    }

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

}