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

import static com.bonait.bnframework.MainApplication.getContext;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.base.BaseFragment;
import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE;
import com.bonait.bnframework.common.helper.I.IWriteCallBack;
import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.fragment.from.DiyMemoryActivity;
import com.bonait.bnframework.modules.home.fragment.from.ImageChooseActivity;
import com.orhanobut.logger.Logger;
import com.qmuiteam.qmui.widget.QMUIViewPager;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.ArrayList;

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

public class WokSetGKCXFragment extends BaseFragment {
    //region 变量
    @BindView(R.id.edit_ydw1)
    EditText edit_ydw1;//原点下限

    @BindView(R.id.edit_ydw2)
    EditText edit_ydw2;//原点上限

    @BindView(R.id.edit_xgw1)
    EditText edit_xgw1;//洗锅位下限

    @BindView(R.id.edit_xgw2)
    EditText edit_xgw2;//洗锅位上限

    @BindView(R.id.edit_ccw1)
    EditText edit_ccw1;//出餐位置下限

    @BindView(R.id.edit_ccw2)
    EditText edit_ccw2;//出餐位置上限

    @BindView(R.id.edit_ctl1)
    EditText edit_ctl1;//抽料位置下限

    @BindView(R.id.edit_ctl2)
    EditText edit_ctl2;//抽料位置上限

    @BindView(R.id.edit_czw11)
    EditText edit_czw11;//1#炒制位下限

    @BindView(R.id.edit_czw12)
    EditText edit_czw12;//1#炒制位上限

    @BindView(R.id.edit_czw21)
    EditText edit_czw21;//2#炒制位下限

    @BindView(R.id.edit_czw22)
    EditText edit_czw22;//2#炒制位上限

    @BindView(R.id.edit_czw31)
    EditText edit_czw31;//3#炒制位下限

    @BindView(R.id.edit_czw32)
    EditText edit_czw32;//3#炒制位上限

    @BindView(R.id.edit_gzfw1)
    EditText edit_gzfw1;//工作范围下限

    @BindView(R.id.edit_gzfw2)
    EditText edit_gzfw2;//工作范围上限

    @BindView(R.id.chaoguo_weizhi)
    TextView chaoguo_weizhi;//炒锅当前位置

    @BindView(R.id.ydw_status)
    RelativeLayout ydw_status;
    @BindView(R.id.xgw_status)
    RelativeLayout xgw_status;
    @BindView(R.id.ccw_status)
    RelativeLayout ccw_status;
    @BindView(R.id.ctl_status)
    RelativeLayout ctl_status;
    @BindView(R.id.czw1_status)
    RelativeLayout czw1_status;
    @BindView(R.id.czw2_status)
    RelativeLayout czw2_status;
    @BindView(R.id.czw3_status)
    RelativeLayout czw3_status;

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

    @BindView(R.id.gkfx_button)
    Button gkfx_button;
    //endregion

    //region 界面
    private Context context;

    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_wok_set_gkcx, null);
        ButterKnife.bind(this, root);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Logger.d("我的fragment创建");
        context = getContext();
        Init();
        RefreshThread();
    }
    //endregion

    //region 函数

    /**
     * 初始化
     */
    public void Init() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        SetShowValue("原点位置",edit_ydw1,edit_ydw2);
                        SetShowValue("洗锅位置",edit_xgw1,edit_xgw2);
                        SetShowValue("倒菜位置",edit_ccw1,edit_ccw2);
                        SetShowValue("抽调料位置",edit_ctl1,edit_ctl2);
                        SetShowValue("1#炒制位置",edit_czw11,edit_czw12);
                        SetShowValue("2#炒制位置",edit_czw21,edit_czw22);
                        SetShowValue("3#炒制位置",edit_czw31,edit_czw32);
                        SetShowValue("炒锅工作范围",edit_gzfw1,edit_gzfw2);
                    }
                });
            }
        }).start();

        gkzx_button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    Log.e("鼠标", "按下: ");
                    ExecuteTheRecipe.WritePLC("转动速度", 50, null);
                    ExecuteTheRecipe.WritePLC("翻转正转", true, null);
                } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    Log.e("鼠标", "松开: ");
                    ExecuteTheRecipe.WritePLC("翻转正转", false, null);
                }
                return false;
            }
        });

        gkfx_button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    Log.e("鼠标", "按下: ");
                    ExecuteTheRecipe.WritePLC("转动速度", 50,null);
                    ExecuteTheRecipe.WritePLC("翻转反转", true, null);
                } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    Log.e("鼠标", "松开: ");
                    ExecuteTheRecipe.WritePLC("翻转反转", false, null);
                }
                return false;
            }
        });
    }

    /**
     * 刷新UI线程
     */
    public void RefreshThread()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true)
                    {
                        Object o0=ExecuteTheRecipe.ReadPLC("编码器当前位值");
                        Object o1=ExecuteTheRecipe.ReadPLC("原点位反馈");
                        Object o2=ExecuteTheRecipe.ReadPLC("清洗位反馈");
                        Object o3=ExecuteTheRecipe.ReadPLC("倒菜位反馈");
                        Object o4=ExecuteTheRecipe.ReadPLC("抽料位反馈");
                        Object o5=ExecuteTheRecipe.ReadPLC("炒菜位1反馈");
                        Object o6=ExecuteTheRecipe.ReadPLC("炒菜位2反馈");
                        Object o7=ExecuteTheRecipe.ReadPLC("炒菜位3反馈");

                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if(o0!=null) { chaoguo_weizhi.setText(o0 + ""); }
                                else {chaoguo_weizhi.setText("未知");}

                                if(o1!=null) { ydw_status.setBackgroundResource((boolean)o1?R.mipmap.status_z:R.mipmap.status_y); }
                                else {ydw_status.setBackgroundResource(R.mipmap.status_y); }

                                if(o2!=null) { xgw_status.setBackgroundResource((boolean)o2?R.mipmap.status_z:R.mipmap.status_y); }
                                else {xgw_status.setBackgroundResource(R.mipmap.status_y); }

                                if(o3!=null) { ccw_status.setBackgroundResource((boolean)o3?R.mipmap.status_z:R.mipmap.status_y); }
                                else {ccw_status.setBackgroundResource(R.mipmap.status_y); }

                                if(o4!=null) { ctl_status.setBackgroundResource((boolean)o4?R.mipmap.status_z:R.mipmap.status_y); }
                                else {ctl_status.setBackgroundResource(R.mipmap.status_y); }

                                if(o5!=null) { czw1_status.setBackgroundResource((boolean)o5?R.mipmap.status_z:R.mipmap.status_y); }
                                else {czw1_status.setBackgroundResource(R.mipmap.status_y); }

                                if(o6!=null) { czw2_status.setBackgroundResource((boolean)o6?R.mipmap.status_z:R.mipmap.status_y); }
                                else {czw2_status.setBackgroundResource(R.mipmap.status_y); }

                                if(o7!=null) { czw3_status.setBackgroundResource((boolean)o7?R.mipmap.status_z:R.mipmap.status_y); }
                                else {czw3_status.setBackgroundResource(R.mipmap.status_y); }
                            }
                        });
                        Thread.sleep(500);
                    }
                }catch (Exception ex)
                {
                }
            }
        }).start();
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.ydw_save, R.id.xgw_save,
            R.id.ccw_save, R.id.ctl_save,
            R.id.czw1_save, R.id.czw2_save, R.id.czw3_save,
            R.id.gzfw_save,

            R.id.ydw_button, R.id.xgw_button,
            R.id.ccw_button, R.id.ctl_button,
            R.id.czw1_button, R.id.czw2_button, R.id.czw3_button,
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ydw_save:
                SaveValue("原点位置",edit_ydw1,edit_ydw2);
                break;
            case R.id.xgw_save:
                SaveValue("洗锅位置",edit_xgw1,edit_xgw2);
                break;
            case R.id.ccw_save:
                SaveValue("倒菜位置",edit_ccw1,edit_ccw2);
                break;
            case R.id.ctl_save:
                SaveValue("抽调料位置",edit_ctl1,edit_ctl2);
                break;
            case R.id.czw1_save:
                SaveValue("1#炒制位置",edit_czw11,edit_czw12);
                break;
            case R.id.czw2_save:
                SaveValue("2#炒制位置",edit_czw21,edit_czw22);
                break;
            case R.id.czw3_save:
                SaveValue("3#炒制位置",edit_czw31,edit_czw32);
                break;
            case R.id.gzfw_save:
                SaveValue("炒锅工作范围",edit_gzfw1,edit_gzfw2);
                break;

            case R.id.ydw_button:
                ButtomZhiWeiZhi("原点位");
                break;
            case R.id.xgw_button:
                ButtomZhiWeiZhi("清洗位");
                break;
            case R.id.ccw_button:
                ButtomZhiWeiZhi("倒菜位");
                break;
            case R.id.ctl_button:
                ButtomZhiWeiZhi("抽料位");
                break;
            case R.id.czw1_button:
                ButtomZhiWeiZhi("炒菜位1");
                break;
            case R.id.czw2_button:
                ButtomZhiWeiZhi("炒菜位2");
                break;
            case R.id.czw3_button:
                ButtomZhiWeiZhi("炒菜位3");
                break;
        }
    }
    //endregion

    //region 私有函数

    /**
     * 根据监听设置值
     * @param name
     * @param edit1
     * @param edit2
     */
    private void SetShowValue(String name,EditText edit1,EditText edit2)
    {
        Object val1= ExecuteTheRecipe.ListeningValue.get(name+"下限");
        Object val2= ExecuteTheRecipe.ListeningValue.get(name+"上限");
        if(val1!=null)
        {
            String str=String.valueOf(val1);
            edit1.setText(str.toCharArray(), 0, str.length());
        }

        if(val2!=null)
        {
            String str=String.valueOf(val2);
            edit2.setText(str.toCharArray(), 0, str.length());
        }
    }


    /**
     * 写入设置值
     * @param name
     * @param edit1
     * @param edit2
     */
    private void SaveValue(String name,EditText edit1,EditText edit2) {
        String val1 = edit1.getText().toString();
        String val2 = edit2.getText().toString();
        if (val1.isEmpty()) {
            ToastUtils.warning(name + "下限不能为空！");
            return;
        }
        if (val2.isEmpty()) {
            ToastUtils.warning(name + "上限不能为空！");
            return;
        }
        int va1 = 0;
        int va2 = 0;
        try {
            va1=Integer.parseInt(val1);
            va2=Integer.parseInt(val2);
        } catch (Exception ex)
        {
            ToastUtils.warning(name + "异常了,"+ex.getMessage());
            return;
        }

        ExecuteTheRecipe.WritePLC(name+"下限",va1,null);
        ExecuteTheRecipe.WritePLC(name+"上限",va2,null);
        ToastUtils.info(name + "保存成功！");
    }

    /**
     * 执行去某个位置
     */
    private void ButtomZhiWeiZhi(String name)
    {
        ExecuteTheRecipe.WritePLC(name,true,null);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ExecuteTheRecipe.WritePLC(name, false, null);
            }
        }, 500);
        ToastUtils.info(name+"执行成功！");
    }
    //endregion
}