package com.example.bpa.view.from;

import static com.example.bpa.helper.T.show;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
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.app.DeviceData;
import com.example.bpa.db.QueryDB;
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;
    //按钮
    Button bt_save,bt_start;
    //switch
    Switch qsmode;
    //进度条
    ProgressBar progress_bar;

//    TextView progress_text;

    boolean mValue = false;
    //endregion

    //region 私有函数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zdqx);
        Init();
        initEvents();
        InitData();
        startListening();
    }
    //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);
        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);//设置清洗模式
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }

    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()));//循环清洗
        qsmode.setChecked(DeviceData.Get().getCleaningMode());
        progress_bar.setProgress(0);
        //progress_bar.setIndeterminate(true); // 将进度条设置为不确定状态
    }


    /**
     * 监听外部数据变化
     */
    private void startListening(){
        new Thread(new Runnable() {
            @Override
            public void run() {
               while (true) {
                 try {
                     if (SetValue(mValue,DeviceData.Get().getCleaningMode())) {
                         runOnUiThread(new Runnable() {
                             @Override
                             public void run() {
                                 progress_bar.setIndeterminate(false);//停止旋转
                                 progress_bar.setProgress(100);//进度条设置为100%
                                 bt_start.setText("清洗完成");
                                // T.show(zdqx_activity.this,"清洗完成");
                             }
                         });
                     }

                     Thread.sleep(1000);


                 }catch (Exception e) {

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

    //region 点击事件
    /**
     * 本页面点击事件监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gongneng_fanhui://返回按钮
                this.finish();
                break;
            case R.id.bt_save://保存
                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);
                            }
                        }catch (InterruptedException e) {

                        }catch (Exception e) {

                        }

                    }
                });
                break;
            case R.id.bt_start://开始清洗
                    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() {
                                                progress_bar.setVisibility(View.VISIBLE);
                                                progress_bar.setIndeterminate(true);//进度条开始旋转
                                                bt_start.setText("开始清洗");
                                            }
                                        });
                                    }

                                    @Override
                                    public void onFailure(String ErrorMsg) {

                                    }
                                });

                            } catch (Exception e) {

                            }

                        }
                    });
                break;
        }
    }
    //endregion

    //判断boolen 从fales变成true
    private boolean SetValue(boolean value, boolean newValue) {
        boolean mflag = value;

        if (newValue && !mflag) { // 当新值为 true 且前一个值为 false 时，发出通知
            value = newValue; // 更新标志变量的值
            return true;
        }
        value = newValue; // 更新标志变量的值
        return  false;
    }
}