package com.example.bpa.view.from;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.example.bpa.Model.IWriteCallBack;
import com.example.bpa.R;
import com.example.bpa.helper.MessageLog;
import com.example.bpa.service.DeviceData;
import com.example.bpa.config.DataBus;
import com.example.bpa.helper.T;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 流速校验
 */
public class lsjy_activity extends AppCompatActivity implements View.OnClickListener {
    //region 变量
    /**
     * 返回按钮
     */
    ImageView gongneng_fanhui;
    /**
     * 标题设置
     */
    TextView gongneng_title;
    //通道校准值
    EditText passwayText_1, passwayText_2, passwayText_3, passwayText_4, passwayText_5, passwayText_6, passwayText_7, passwayText_8, passwayText_9, passwayText_10, passwayText_11, passwayText_12;
    //校准基准时间
    EditText timeText;
    List<EditText> editTextLists = new ArrayList<EditText>();
    //通道校准开关
    Switch Switch_1, Switch_2, Switch_3, Switch_4, Switch_5, Switch_6, Switch_7, Switch_8, Switch_9, Switch_10, Switch_11, Switch_12;
    List<Switch> switchLists = new ArrayList<Switch>();
    //开始校准
    Button StartButton;
    //保存
    Button SaveButton;
    //清零
    Button ClearText;
    //重量
    TextView zhongliang;
    //endregion

    //region 私有函数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lsjy);
        Init();
        initEvents();
        Initdata();
        Run();
    }

    @Override
    protected void onDestroy() {

        try {
            //设置自动模式
            DeviceData.Get().setHandOrAutoSwitch(true, null);
        } catch (Exception e) {

        }
        super.onDestroy();
    }

    //endregion

    //region 公共函数

    /**
     * 初始化
     */
    private void Init() {
        gongneng_fanhui = this.findViewById(R.id.gongneng_fanhui);
        gongneng_title = this.findViewById(R.id.gongneng_title);
        //通过Activity.getIntent()获取当前页面接收到的Intent。 getXxxExtra方法获取Intent传递过来的数据
        String msg = getIntent().getStringExtra("data");
        gongneng_title.setText(msg);
        //校准数值
        passwayText_1 = findViewById(R.id.passwayText_1);
        passwayText_2 = findViewById(R.id.passwayText_2);
        passwayText_3 = findViewById(R.id.passwayText_3);
        passwayText_4 = findViewById(R.id.passwayText_4);
        passwayText_5 = findViewById(R.id.passwayText_5);
        passwayText_6 = findViewById(R.id.passwayText_6);
        passwayText_7 = findViewById(R.id.passwayText_7);
        passwayText_8 = findViewById(R.id.passwayText_8);
        passwayText_9 = findViewById(R.id.passwayText_9);
        passwayText_10 = findViewById(R.id.passwayText_10);
        passwayText_11 = findViewById(R.id.passwayText_11);
        passwayText_12 = findViewById(R.id.passwayText_12);

        editTextLists = Arrays.asList(passwayText_1, passwayText_2, passwayText_3, passwayText_4, passwayText_5, passwayText_6, passwayText_7, passwayText_8, passwayText_9, passwayText_10, passwayText_11, passwayText_12);

        //校准开关
        Switch_1 = findViewById(R.id.Switch_1);
        Switch_2 = findViewById(R.id.Switch_2);
        Switch_3 = findViewById(R.id.Switch_3);
        Switch_4 = findViewById(R.id.Switch_4);
        Switch_5 = findViewById(R.id.Switch_5);
        Switch_6 = findViewById(R.id.Switch_6);
        Switch_7 = findViewById(R.id.Switch_7);
        Switch_8 = findViewById(R.id.Switch_8);
        Switch_9 = findViewById(R.id.Switch_9);
        Switch_10 = findViewById(R.id.Switch_10);
        Switch_11 = findViewById(R.id.Switch_11);
        Switch_12 = findViewById(R.id.Switch_12);
        switchLists = Arrays.asList(Switch_1, Switch_2, Switch_3, Switch_4, Switch_5, Switch_6, Switch_7, Switch_8, Switch_9, Switch_10, Switch_11, Switch_12);

        //校准按钮
        StartButton = findViewById(R.id.StartButton);
        timeText = findViewById(R.id.edi_time);
        SaveButton = findViewById(R.id.savetime);

        ClearText = findViewById(R.id.ClearText);
        zhongliang = findViewById(R.id.zhongliang);
    }

    /**
     * 初始化按钮事件
     */
    private void initEvents() {
        gongneng_fanhui.setOnClickListener(this);
        StartButton.setOnClickListener(this);
        SaveButton.setOnClickListener(this);
        ClearText.setOnClickListener(this);
    }

    /**
     * 初始化数据加载
     */
    public void Initdata() {
        //初始化标准值数据
        try {
            for (int i = 0; i < 12; i++) {
                String s = String.valueOf(DeviceData.Get().getCalibrationValue(i + 1));
                editTextLists.get(i).setText(s);
            }
            String time = String.valueOf(DeviceData.Get().getCalibrationReferenceTime());
            //校准时间
            timeText.setText(time);

            //设置手动模式
            DeviceData.Get().setHandOrAutoSwitch(false, null);
        } catch (Exception e) {

        }
    }
    //endregion

    //region 点击事件

    /**
     * 本页面点击事件监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gongneng_fanhui://返回按钮
                this.finish();
                break;
            case R.id.savetime://校准基准时间保存
                String stime = timeText.getText().toString();
                if (stime.equals("")) {
                    T.show(lsjy_activity.this, "校准基准时间不能为空！");
                    return;
                }
                int ftime = 0;
                try {
                    ftime = Integer.parseInt(stime);
                } catch (Exception e) {
                    T.show(lsjy_activity.this, "校准基准时间异常！" + e.getMessage());
                    return;
                }

                DeviceData.Get().setCalibrationReferenceTime((short) ftime, new IWriteCallBack() {
                    @Override
                    public void onSuccess() {
                        T.show(lsjy_activity.this, "校准时间写入成功");
                    }

                    @Override
                    public void onFailure(String ErrorMsg) {
                        T.show(lsjy_activity.this, "校准时间写入失败");
                    }
                });

                for (int i = 0; i < 12; i++) {
                    if (switchLists.get(i).isChecked() == true)//校准开关打开后,允许校准
                    {
                        String value = editTextLists.get(i).getText().toString();
                        Short val = 0;
                        try {
                            val = Short.parseShort(value);
                        } catch (Exception e) {
                        }
                        if (val <= 0) {
                            T.show(lsjy_activity.this, "请检查[通道" + (i + 1) + "]数值不能小于0");
                            return;
                        }
                    }
                }
                //写开关状态 数值
                for (int i = 0; i < 12; i++) {
                    String value = editTextLists.get(i).getText().toString();
                    Short val = 0;
                    try {
                        val = Short.parseShort(value);
                    } catch (Exception e) {
                    }
                    DeviceData.Get().setCalibrationValue(val, i + 1, new IWriteCallBack() {
                        @Override
                        public void onSuccess() {
                        }

                        @Override
                        public void onFailure(String ErrorMsg) {
                        }
                    });
                }

                break;
            case R.id.StartButton://开始校准
                //写开关状态 数值
                for (int i = 0; i < 12; i++) {
                    //1.写入开关状态
                    DeviceData.Get().setChCalibrationSwitch(switchLists.get(i).isChecked(), i + 1, new IWriteCallBack() {
                        @Override
                        public void onSuccess() {
                        }

                        @Override
                        public void onFailure(String ErrorMsg) {
                        }
                    });
                }
                //写开始校准
                DeviceData.Get().setChStartCalibration(new IWriteCallBack() {
                    @Override
                    public void onSuccess() {
                        T.show(lsjy_activity.this, "通道开始校准");
                    }

                    @Override
                    public void onFailure(String ErrorMsg) {
                        T.show(lsjy_activity.this, "通道开始校准失败");
                    }
                });
                break;
            case R.id.ClearText://清空称重重量
                DeviceData.Get().setWeightClear(new IWriteCallBack() {
                    @Override
                    public void onSuccess() {
                        T.show(lsjy_activity.this, "清零成功");
                    }

                    @Override
                    public void onFailure(String ErrorMsg) {
                        T.show(lsjy_activity.this, "清零失败");
                    }
                });
                break;
        }
    }
    //endregion

    //region 线程

    /**
     * 实时显示线程
     */
    public void Run() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (DataBus.getInstance().PlcIsConnect) {
                    try {
                        lsjy_activity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    zhongliang.setText(String.valueOf(DeviceData.Get().getCallCurrentWeight(1)/10.0)+" g");
                                } catch (Exception e) {
                                    MessageLog.ShowInfo("重量解析显示异常！"+e.getMessage());
                                }
                            }
                        });
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();
    }
    //endregion
}