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

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

import android.content.Context;
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.TextView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.base.BaseFragment;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.orhanobut.logger.Logger;

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

public class WokSetTLZZ1Fragment extends BaseFragment {

    //region 变量
    @BindView(R.id.pyz_zz)
    Button pyz_zz;

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

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

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


    @BindView(R.id.edit_pyz_hlsd)
    EditText edit_pyz_hlsd;

    @BindView(R.id.edit_pyz_ddsd)
    EditText edit_pyz_ddsd;

    @BindView(R.id.edit_pyz_zdsd)
    EditText edit_pyz_zdsd;

    @BindView(R.id.edit_fzz_hlsd)
    EditText edit_fzz_hlsd;

    @BindView(R.id.edit_fzz_ddsd)
    EditText edit_fzz_ddsd;

    @BindView(R.id.edit_fzz_zdsd)
    EditText edit_fzz_zdsd;

    @BindView(R.id.edit_pyz_dqz)
    TextView edit_pyz_dqz;
    @BindView(R.id.edit_pyz_wz1)
    TextView edit_pyz_wz1;
    @BindView(R.id.edit_pyz_wz2)
    TextView edit_pyz_wz2;
    @BindView(R.id.edit_pyz_wz3)
    TextView edit_pyz_wz3;
    @BindView(R.id.edit_pyz_wz4)
    TextView edit_pyz_wz4;

    @BindView(R.id.edit_fzz_dqz)
    TextView edit_fzz_dqz;
    @BindView(R.id.edit_fzz_wz1)
    TextView edit_fzz_wz1;
    @BindView(R.id.edit_fzz_wz2)
    TextView edit_fzz_wz2;
    @BindView(R.id.edit_fzz_wz3)
    TextView edit_fzz_wz3;
    @BindView(R.id.edit_fzz_wz4)
    TextView edit_fzz_wz4;
    //endregion

    //region 界面
    private Context context;

    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_wok_set_tlzz1, 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("翻转1-回原点速度", edit_pyz_hlsd);
                        SetShowValue("翻转1-点动速度", edit_pyz_ddsd);
                        SetShowValue("翻转1-定位运行速度", edit_pyz_zdsd);

                        SetShowValue("翻转2-回原点速度", edit_fzz_hlsd);
                        SetShowValue("翻转2-点动速度", edit_fzz_ddsd);
                        SetShowValue("翻转2-定位运行速度", edit_fzz_zdsd);
                    }
                });
            }
        }).start();

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

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

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

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

    /**
     * 刷新UI线程
     */
    public void RefreshThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        Object o0 = ExecuteTheRecipe.ReadPLC("翻转1-伺服当前值");
                        Object o1 = ExecuteTheRecipe.ReadPLC("翻转1-1号位置值");
                        Object o2 = ExecuteTheRecipe.ReadPLC("翻转1-2号位置值");
                        Object o3 = ExecuteTheRecipe.ReadPLC("翻转1-3号位置值");
                        Object o4 = ExecuteTheRecipe.ReadPLC("翻转1-4号位置值");

                        Object m0 = ExecuteTheRecipe.ReadPLC("翻转2-伺服当前值");
                        Object m1 = ExecuteTheRecipe.ReadPLC("翻转2-1号位置值");
                        Object m2 = ExecuteTheRecipe.ReadPLC("翻转2-2号位置值");
                        Object m3 = ExecuteTheRecipe.ReadPLC("翻转2-3号位置值");
                        Object m4 = ExecuteTheRecipe.ReadPLC("翻转2-4号位置值");

                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (o0 != null) {
                                    edit_pyz_dqz.setText(o0 + "");
                                } else {
                                    edit_pyz_dqz.setText("未知");
                                }
                                if (o1 != null) {
                                    edit_pyz_wz1.setText(o1 + "");
                                } else {
                                    edit_pyz_wz1.setText("未知");
                                }
                                if (o2 != null) {
                                    edit_pyz_wz2.setText(o2 + "");
                                } else {
                                    edit_pyz_wz2.setText("未知");
                                }
                                if (o3 != null) {
                                    edit_pyz_wz3.setText(o3 + "");
                                } else {
                                    edit_pyz_wz3.setText("未知");
                                }
                                if (o4 != null) {
                                    edit_pyz_wz4.setText(o4 + "");
                                } else {
                                    edit_pyz_wz4.setText("未知");
                                }

                                if (m0 != null) {
                                    edit_fzz_dqz.setText(m0 + "");
                                } else {
                                    edit_fzz_dqz.setText("未知");
                                }
                                if (m1 != null) {
                                    edit_fzz_wz1.setText(m1 + "");
                                } else {
                                    edit_fzz_wz1.setText("未知");
                                }
                                if (m2 != null) {
                                    edit_fzz_wz2.setText(m2 + "");
                                } else {
                                    edit_fzz_wz2.setText("未知");
                                }
                                if (m3 != null) {
                                    edit_fzz_wz3.setText(m3 + "");
                                } else {
                                    edit_fzz_wz3.setText("未知");
                                }
                                if (m4 != null) {
                                    edit_fzz_wz4.setText(m4 + "");
                                } else {
                                    edit_fzz_wz4.setText("未知");
                                }
                            }
                        });
                        Thread.sleep(500);
                    }
                } catch (Exception ex) {
                }
            }
        }).start();
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({
            R.id.pyz_hl,
            R.id.pyz_wzsz1, R.id.pyz_wzsz2, R.id.pyz_wzsz3, R.id.pyz_wzsz4,
            R.id.pyz_qu1, R.id.pyz_qu2, R.id.pyz_qu3, R.id.pyz_qu4,

            R.id.fzz_hl,
            R.id.fzz_wzsz1, R.id.fzz_wzsz2, R.id.fzz_wzsz3, R.id.fzz_wzsz4,
            R.id.fzz_qu1, R.id.fzz_qu2, R.id.fzz_qu3, R.id.fzz_qu4,

            R.id.dcqd_button1,R.id.dcqd_button2,

            R.id.pyz_savevalue, R.id.fzz_savevalue
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.pyz_hl:
                ButtomZhiWeiZhi("翻转1-回原点");
                break;
            case R.id.pyz_wzsz1:
                ButtomZhiWeiZhi("翻转1-1#位置设定");
                break;
            case R.id.pyz_wzsz2:
                ButtomZhiWeiZhi("翻转1-2#位置设定");
                break;
            case R.id.pyz_wzsz3:
                ButtomZhiWeiZhi("翻转1-3#位置设定");
                break;
            case R.id.pyz_wzsz4:
                ButtomZhiWeiZhi("翻转1-4#位置设定");
                break;
            case R.id.pyz_qu1:
                ButtomZhiWeiZhi("翻转1-去1号位");
                break;
            case R.id.pyz_qu2:
                ButtomZhiWeiZhi("翻转1-去2号位");
                break;
            case R.id.pyz_qu3:
                ButtomZhiWeiZhi("翻转1-去3号位");
                break;
            case R.id.pyz_qu4:
                ButtomZhiWeiZhi("翻转1-去4号位");
                break;

            case R.id.fzz_hl:
                ButtomZhiWeiZhi("翻转2-回原点");
                break;
            case R.id.fzz_wzsz1:
                ButtomZhiWeiZhi("翻转2-1#位置设定");
                break;
            case R.id.fzz_wzsz2:
                ButtomZhiWeiZhi("翻转2-2#位置设定");
                break;
            case R.id.fzz_wzsz3:
                ButtomZhiWeiZhi("翻转2-3#位置设定");
                break;
            case R.id.fzz_wzsz4:
                ButtomZhiWeiZhi("翻转2-4#位置设定");
                break;
            case R.id.fzz_qu1:
                ButtomZhiWeiZhi("翻转2-去1号位");
                break;
            case R.id.fzz_qu2:
                ButtomZhiWeiZhi("翻转2-去2号位");
                break;
            case R.id.fzz_qu3:
                ButtomZhiWeiZhi("翻转2-去3号位");
                break;
            case R.id.fzz_qu4:
                ButtomZhiWeiZhi("翻转2-去4号位");
                break;
            case R.id.dcqd_button1:
                ButtomZhiWeiZhi("主料1号位");
                break;
            case R.id.dcqd_button2:
                ButtomZhiWeiZhi("主料2号位");
                break;

            case R.id.pyz_savevalue:
                SaveValue("翻转1", edit_pyz_hlsd, edit_pyz_ddsd, edit_pyz_zdsd);
                break;
            case R.id.fzz_savevalue:
                SaveValue("翻转2", edit_fzz_hlsd, edit_fzz_ddsd, edit_fzz_zdsd);
                break;
        }
    }
    //endregion

    //region 私有

    /**
     * 写入设置值
     *
     * @param name
     * @param edit1
     * @param edit2
     */
    private void SaveValue(String name, EditText edit1, EditText edit2, EditText edit3) {
        String val1 = edit1.getText().toString();
        String val2 = edit2.getText().toString();
        String val3 = edit3.getText().toString();

        if (val1.isEmpty()) {
            ToastUtils.warning(name + "回零速度不能为空！");
            return;
        }
        if (val2.isEmpty()) {
            ToastUtils.warning(name + "点动速度不能为空！");
            return;
        }
        if (val3.isEmpty()) {
            ToastUtils.warning(name + "自动速度不能为空！");
            return;
        }
        int va1 = 0;
        int va2 = 0;
        int va3 = 0;

        try {
            va1 = Integer.parseInt(val1);
            va2 = Integer.parseInt(val2);
            va3 = Integer.parseInt(val3);

        } catch (Exception ex) {
            ToastUtils.warning(name + "异常了," + ex.getMessage());
            return;
        }

        ExecuteTheRecipe.WritePLC(name + "-回原点速度", va1, null);
        ExecuteTheRecipe.WritePLC(name + "-点动速度", va2, null);
        ExecuteTheRecipe.WritePLC(name + "-定位运行速度", va3, null);
        ToastUtils.info(name + "保存成功！");
    }

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

    /**
     * 执行去某个位置
     */
    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
}