package com.bonait.bnframework.modules.home.fragment.mode;

import android.content.Context;
import android.os.Handler;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_PLCADDRESS;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.suke.widget.SwitchButton;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * PLC控制
 */
public class fragment_plc_control extends LinearLayout {
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.name_PLC)
    TextView name_PLC;//分组

    @BindView(R.id.edittext_plc)
    EditText edittext_plc;//输入文本
    @BindView(R.id.wenben)
    LinearLayout wenben;
    @BindView(R.id.kaiguan)
    RelativeLayout kaiguan;

    @BindView(R.id.zhidu)
    RelativeLayout zhidu;

    @BindView(R.id.update_plc)
    Button update_plc;//下发按钮

    @BindView(R.id.control_switch)
    SwitchButton control_switch;//开关按钮

    @BindView(R.id.control_botton)
    Button control_botton;//控制按钮


    @BindView(R.id.text_wb)
    TextView text_wb;//分组
    private View root;
    public fragment_plc_control(Context context, @Nullable AttributeSet attrs, BPA_PLCADDRESS _model) {
        super(context, attrs);
        root= LayoutInflater.from(context).inflate(R.layout.fragment_item_plc_control, this);
        ButterKnife.bind(this, root);
        model=_model;
        Init();
    }

    public void Init()
    {
        if (model.address.equals("-------------"))
        {
            name.setText(model.name+"：");
            wenben.setVisibility(View.GONE);
            kaiguan.setVisibility(View.GONE);
            zhidu.setVisibility(View.GONE);
        }else
        {
            name_PLC.setText(model.name);
            if(model.iswrite==1)
            {
                zhidu.setVisibility(View.GONE);
                if(model.address.startsWith("M"))//开关
                {
                    wenben.setVisibility(View.GONE);//显示
                    kaiguan.setVisibility(View.VISIBLE);
                    //1. 初始化 停止 复位 搅拌  炒锅抽水 炒锅放水 出菜 原点位
                    if(ConfigName.getInstance().BottomList.contains(model.name))//Bottom
                    {
                        control_switch.setVisibility(View.GONE);
                        control_botton.setVisibility(View.VISIBLE);
                    }else //开关
                    {
                        control_switch.setVisibility(View.VISIBLE);
                        control_botton.setVisibility(View.GONE);

                        if(model.isread==1)
                        {
                            Object val= ExecuteTheRecipe.ListeningValue.get(model.name);
                            if(val!=null)
                            {
                                control_switch.setChecked((boolean)val);
                            }
                        }
                    }
                } else //文本
                {
                    wenben.setVisibility(View.VISIBLE);
                    kaiguan.setVisibility(View.GONE);
                    if(model.isread==1)
                    {
                        Object val= ExecuteTheRecipe.ListeningValue.get(model.name);
                        if(val!=null)
                        {
                            String str=String.valueOf(val);
                            edittext_plc.setText(str.toCharArray(), 0, str.length());
                        }
                    }

                    if(model.address.startsWith("VR"))
                    {
                        edittext_plc.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
                    }else
                    {
                        edittext_plc.setInputType(InputType.TYPE_CLASS_NUMBER);
                    }
                }
            }else //只读
            {
                zhidu.setVisibility(View.VISIBLE);
                wenben.setVisibility(View.GONE);
                kaiguan.setVisibility(View.GONE);
                Object val= ExecuteTheRecipe.ListeningValue.get(model.name);
                if(model.address.startsWith("M"))//开关状态
                {
                    if(val!=null)
                    {
                        if((boolean)val)
                        {
                            text_wb.setText("打开");
                            text_wb.setTextColor(this.getResources().getColor(R.color.green_primary_dark));
                        }else
                        {
                            text_wb.setText("关闭");
                            text_wb.setTextColor(this.getResources().getColor(R.color.red_primary_dark));
                        }
                    }else
                    {
                        text_wb.setText("关闭");
                        text_wb.setTextColor(this.getResources().getColor(R.color.red_primary_dark));
                    }
                }else //文本
                {
                    String dw="";
                    if(model.name.contains("温度"))
                    {
                        dw=" °C";
                    }else if(model.name.contains("电压"))
                    {
                        dw=" V";
                    }else if(model.name.contains("电流"))
                    {
                        dw=" A";
                    }else if(model.name.contains("转速"))
                    {
                        dw=" r/min";
                    }else if(model.name.contains("频率"))
                    {
                        dw=" Hz";
                    }else if(model.name.contains("功率"))
                    {
                        dw=" KW";
                    }else if(model.name.contains("挡位"))
                    {
                        dw=" 挡";
                    }
                    text_wb.setText(val+dw);
                }
            }
        }

        control_switch.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                boolean ischick= control_switch.isChecked();
                ExecuteTheRecipe.WritePLC(model.name,ischick,null);
                ToastUtils.info("写入地址:"+model.address+"成功！状态:"+(ischick?"打开":"关闭"));
            }
        });

        /**
         * Bottom 按钮按下
         */
        control_botton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ExecuteTheRecipe.WritePLC(model.name,true,null);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ExecuteTheRecipe.WritePLC(model.name, false, null);
                    }
                }, 500);

                ToastUtils.info(model.name+"执行成功！");
            }
        });
    }
    public BPA_PLCADDRESS model=null;
    @OnClick({R.id.update_plc})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.update_plc:
                String text=edittext_plc.getText().toString();
                if(text.isEmpty())
                {
                    ToastUtils.warning("输入信息不能为空！");
                    return;
                }

                if(model.address.startsWith("VW"))
                {
                    Short val=Short.parseShort(text);
                    ExecuteTheRecipe.WritePLC(model.name,val,null);
                }else if(model.address.startsWith("VD"))
                {
                    int val=Integer.parseInt(text);
                    ExecuteTheRecipe.WritePLC(model.name,val,null);
                }
                else if(model.address.startsWith("VR"))
                {
                    float val=0;
                    try {
                        val=Float.parseFloat(text);
                    }
                    catch (Exception ex)
                    {
                        ToastUtils.warning("输入数据不合法！");
                        return;
                    }
                    ExecuteTheRecipe.WritePLC(model.name,val,null);
                }else
                {
                    Short val=Short.parseShort(text);
                    ExecuteTheRecipe.WritePLC(model.name,val,null);
                }
                ToastUtils.info("写入地址:"+model.address+"成功！");
                break;

        }
    }
}