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.ProgressBar;
import android.widget.Switch;
import android.widget.TextView;

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

/**
 * 自动清洗
 */
public class zdqx_activity extends AppCompatActivity implements View.OnClickListener {
    //region 变量
    /**
     * 返回按钮
     */
    ImageView gongneng_fanhui;
    /**
     * 标题设置
     */
    TextView gongneng_title;
    //参数
    EditText edit_pstime, edit_jstime, edit_jqxjtime, edit_xhtime,edit_qxjrtime;
    //按钮
    Button bt_save, bt_start;
    //switch
    Switch qsmode;
    //进度条
    ProgressBar progress_bar;

//    TextView progress_text;

    boolean IsStart = false;
    //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_zdqx);
        Init();
        initEvents();
        InitData();
        startListening();
    }

    @Override
    protected void onDestroy() {
        try {
            if(DataBus.getInstance().PlcIsConnect)
            {
                DeviceData.Get().setCleaningMode(false, 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);
        edit_pstime = this.findViewById(R.id.edit_pstime);
        edit_jstime = this.findViewById(R.id.edit_jstime);
        edit_jqxjtime = this.findViewById(R.id.edit_jqxjtime);
        edit_xhtime = this.findViewById(R.id.edit_xhtime);
        edit_qxjrtime= this.findViewById(R.id.edit_qxjrtime);
        bt_save = this.findViewById(R.id.bt_save);
        bt_start = this.findViewById(R.id.bt_start);
        qsmode = this.findViewById(R.id.qsmode);
        progress_bar = this.findViewById(R.id.progress_bar);
//        progress_text = this.findViewById(R.id.progress_text);
        //通过Activity.getIntent()获取当前页面接收到的Intent。 getXxxExtra方法获取Intent传递过来的数据
        String msg = getIntent().getStringExtra("data");
        gongneng_title.setText(msg);
    }

    /**
     * 初始化按钮事件
     */
    private void initEvents() {
        gongneng_fanhui.setOnClickListener(this);
        bt_save.setOnClickListener(this);
        bt_start.setOnClickListener(this);
        qsmode.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            DeviceData.Get().setCleaningMode(isChecked, null);//设置清洗模式
                            if (!isChecked) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        IsStart = false;
                                        ProgressClose();
                                        bt_start.setText("开始清洗");
                                        k = 0;
                                    }
                                });

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }

    private void InitData() {

        edit_pstime.setText(String.valueOf(DeviceData.Get().getDrainageTime()));//排水
        edit_jstime.setText(String.valueOf(DeviceData.Get().getInletTime()));//进水
        edit_jqxjtime.setText(String.valueOf(DeviceData.Get().getAddCleaningAgentTime()));//加清洗剂时间
        edit_xhtime.setText(String.valueOf(DeviceData.Get().getCyclicCleaningTime()));//循环清洗
        edit_qxjrtime.setText(String.valueOf(DeviceData.Get().getCleaningAndHeatingTime()));//清洗加热时间

        qsmode.setChecked(DeviceData.Get().getCleaningMode());

        ProgressClose();
        try {
            if(DataBus.getInstance().PlcIsConnect) {
                DeviceData.Get().setCleaningMode(false, null);//打开自动清洗模式
            }
        } catch (Exception e) {
        }
    }

    /**
     * 关闭进度条
     */
    public void ProgressClose() {
        progress_bar.setIndeterminate(true); // 将进度条设置为不确定状态
        progress_bar.setProgress(100);
    }

    int k = 0;

    /**
     * 打开进度条
     */
    public void ProgressStart() {
        progress_bar.setIndeterminate(false);
        progress_bar.setProgress(0);
        IsStart = true;
        k = 0;
    }

    /**
     * 监听外部数据变化
     */
    private void startListening() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        if (IsStart) {
                            if (DeviceData.Get().getCleaningComplete()) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        IsStart = false;
                                        ProgressClose();
                                        bt_start.setText("开始清洗");
                                        k = 0;
                                        T.show(zdqx_activity.this, "清洗完成");
                                    }
                                });
                            }
                            k++;
                            progress_bar.setProgress(k <= 100 ? k : 100);
                        }
                        Thread.sleep(1000);

                    } catch (Exception e) {

                    }
                }
            }
        }).start();
    }
    //endregion

    //region 点击事件

    /**
     * 本页面点击事件监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        short ps = Short.parseShort(edit_pstime.getText().toString());
        short js = Short.parseShort(edit_jstime.getText().toString());
        short qxj = Short.parseShort(edit_jqxjtime.getText().toString());
        switch (v.getId()) {
            case R.id.gongneng_fanhui://返回按钮
                this.finish();
                break;
            case R.id.bt_save://保存

                if (qxj >= js || qxj >= ps) {
                    T.show(zdqx_activity.this, "加清洗剂时间不能大于进水时间！");
                    return;
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!edit_pstime.getText().toString().equals("")) {
                                DeviceData.Get().setDrainageTime(Short.parseShort(edit_pstime.getText().toString()), null);
                                Thread.sleep(200);
                            }
                            if (!edit_jstime.getText().toString().equals("")) {
                                DeviceData.Get().setInletTime(Short.parseShort(edit_jstime.getText().toString()), null);
                                Thread.sleep(200);
                            }
                            if (!edit_jqxjtime.getText().toString().equals("")) {
                                DeviceData.Get().setAddCleaningAgentTime(Short.parseShort(edit_jqxjtime.getText().toString()), null);
                                Thread.sleep(200);
                            }
                            if (!edit_xhtime.getText().toString().equals("")) {
                                DeviceData.Get().setCyclicCleaningTime(Short.parseShort(edit_xhtime.getText().toString()), null);
                                Thread.sleep(200);
                            }

                            if (!edit_qxjrtime.getText().toString().equals("")) {
                                DeviceData.Get().setCleaningAndHeatingTime(Short.parseShort(edit_qxjrtime.getText().toString()), null);
                                Thread.sleep(200);
                            }

                        } catch (InterruptedException e) {

                        } catch (Exception e) {

                        }

                    }
                }).start();
                T.show(zdqx_activity.this, "保存成功！");
                break;
            case R.id.bt_start://开始清洗

                if (qxj >= js || qxj >= ps) {
                    T.show(zdqx_activity.this, "加清洗剂时间不能大于进水时间！");
                    return;
                }
                if (!qsmode.isChecked()) {
                    T.show(zdqx_activity.this, "请先打开清洗模式！");
                    return;
                }

                if (IsStart) {
                    T.show(zdqx_activity.this, "正在清洗中,请勿重复点击！");
                    return;
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            DeviceData.Get().setStartCleaning(new IWriteCallBack() {
                                @Override
                                public void onSuccess() {
                                    //UI线程操作UI控件
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            ProgressStart();
                                            bt_start.setText("正在清洗");
                                        }
                                    });
                                }

                                @Override
                                public void onFailure(String ErrorMsg) {

                                }
                            });

                        } catch (Exception e) {

                        }

                    }
                }).start();
                break;
        }
    }
    //endregion
}