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

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
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.constant.DataBus;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.utils.NetworkUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.fragment.from.DiyUpdateActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class add_qupenren extends LinearLayout {

    @BindView(R.id.quzhizuo)
    Button quzhizuo;

    @BindView(R.id.quzhizuo_ms)
    TextView quzhizuo_ms;
    @BindView(R.id.bianji)
    RelativeLayout bianji;

    @BindView(R.id.chaozhi)
    RelativeLayout chaozhi;
    @BindView(R.id.close_from)
    Button close_from;

    @BindView(R.id.good_name)
    TextView good_name;//商品名称

    @BindView(R.id.yihao_text)
    TextView yihao_text;//1号

    @BindView(R.id.erhao_text)
    TextView erhao_text;//1号

    @BindView(R.id.sanhao_text)
    TextView sanhao_text;//1号

    @BindView(R.id.sihao_text)
    TextView sihao_text;//1号
    /**
     * 点击事件
     */
    public MyClickListener mListener = null;
    private View root;

    private Context contextMian;

    public add_qupenren(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        contextMian = context;
        root = LayoutInflater.from(context).inflate(R.layout.dialog_qupenren, this);
        ButterKnife.bind(this, root);
        Init();
    }


    public void Init() {
        //初始化界面控件的事件
        initEvent();
    }


    private void initData() {
        if(mode!=null)
        {
            String id= mode.getAccount().toString();
            good_name.setText(mode.getTag().toString()+"");

            ConcurrentHashMap<Integer,String> resultData= DataBus.getInstance().GetMainDisplayText(id);

            yihao_text.setText(resultData.get(1)+"");
            erhao_text.setText(resultData.get(2)+"");
            sanhao_text.setText(resultData.get(3)+"");
            sihao_text.setText(resultData.get(4)+"");

            if(ExecuteTheRecipe.IsStart)
            {
                quzhizuo.setVisibility(View.GONE);
                quzhizuo_ms.setVisibility(View.VISIBLE);
            }else
            {
                quzhizuo.setVisibility(View.VISIBLE);
                quzhizuo_ms.setVisibility(View.GONE);
            }
        }
    }

    public SectionItem mode = null;

    /**
     * 设置数据
     *
     * @param
     */
    public void SetData(SectionItem data,MyClickListener listener) {
        try {
            mListener=listener;
            mode = data;
            initData();
        } catch (Exception ex) {

        }
    }

    /**
     *
     */
    public void RefreshData()
    {
        initData();
    }


    private void initEvent() {
        if (ConfigName.getInstance().user.userID.equals("研发用户")) {
            bianji.setVisibility(View.VISIBLE);
        } else {
            bianji.setVisibility(View.GONE);
        }

        quzhizuo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ConfigName.getInstance().PlcIsConnect) {
                    ToastUtils.warning("PLC未准备就绪!!!");
                    return;
                }

                Object chushiover = ExecuteTheRecipe.getListingValue("初始化完成");
                //Object jiting = ExecuteTheRecipe.getListingValue("设备急停");
                if (chushiover == null || !(boolean) chushiover) {
                    ToastUtils.warning("设备未初始化,请先手动初始化设备!!!");
                    return;
                }
                if (mListener != null) {
                    mListener.clickListenerNew(view, 5, mode);
                }
            }
        });
        close_from.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.clickListenerNew(v, 0, mode);
                }
            }
        });

        bianji.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.clickListenerNew(view, 3, mode);
                }
            }
        });

        chaozhi.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.clickListenerNew(view, 99, mode);
                }
            }
        });
    }
}