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.T;
import com.example.bpa.service.DeviceData;

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

public class wdgl_activity extends AppCompatActivity  implements View.OnClickListener{

    //region 变量
    /**
     * 返回按钮
     */
    ImageView gongneng_fanhui;
    /**
     * 标题设置
     */
    TextView gongneng_title;
    /**
     * 温度上限
     */
    EditText gtwd1_up,gtwd2_up,gtwd3_up;
    /**
     * 温度下限
     */
    EditText gtwd1_down,gtwd2_down,gtwd3_down;
    /**
     * 当前温度
     */
    TextView wendu1,wendu2,wendu3;
    /**
     * 保存按钮
     */
    Button mwSave_button1,mwSave_button2,mwSave_button3;
    //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_wdgl);
        Init();
        initEvents();
        startListening();
    }
    //endregion

    //region 公共函数

    /**
     * 初始化
     */
    private void Init(){
        gongneng_fanhui = this.findViewById(R.id.gongneng_fanhui);
        gongneng_title = this.findViewById(R.id.gongneng_title);
        gtwd1_up= this.findViewById(R.id.gtwd1_up);
        gtwd2_up= this.findViewById(R.id.gtwd2_up);
        gtwd3_up= this.findViewById(R.id.gtwd3_up);

        gtwd1_down= this.findViewById(R.id.gtwd1_down);
        gtwd2_down= this.findViewById(R.id.gtwd2_down);
        gtwd3_down= this.findViewById(R.id.gtwd3_down);

        wendu1= this.findViewById(R.id.wendu1);
        wendu2= this.findViewById(R.id.wendu2);
        wendu3= this.findViewById(R.id.wendu3);

        mwSave_button1= this.findViewById(R.id.mwSave_button1);
        mwSave_button2= this.findViewById(R.id.mwSave_button2);
        mwSave_button3= this.findViewById(R.id.mwSave_button3);


        //通过Activity.getIntent()获取当前页面接收到的Intent。 getXxxExtra方法获取Intent传递过来的数据
        String msg = getIntent().getStringExtra("data");
        gongneng_title.setText(msg);

        Initdata();
    }

    /**
     * 初始化按钮事件
     */
    private void initEvents() {
        gongneng_fanhui.setOnClickListener(this);
        mwSave_button1.setOnClickListener(this);
        mwSave_button2.setOnClickListener(this);
        mwSave_button3.setOnClickListener(this);
    }
    /**
     * 监听外部数据变化
     */
    private void startListening() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                wendu1.setText(String.valueOf(DeviceData.Get().getFructoseTemperature1())+"°C");
                                wendu2.setText(String.valueOf(DeviceData.Get().getFructoseTemperature2())+"°C");
                                wendu3.setText(String.valueOf(DeviceData.Get().getWashTheTankTemperature())+"°C");
                            }
                        });
                        Thread.sleep(1000);

                    } catch (Exception e) {

                    }
                }
            }
        }).start();
    }
    /**
     * 初始化数据加载
     */
    public void Initdata() {
        try {
            gtwd1_up.setText(String.valueOf(DeviceData.Get().getFructoseTemperature1_up()));
            gtwd1_down.setText(String.valueOf(DeviceData.Get().getFructoseTemperature1_down()));
            wendu1.setText(String.valueOf(DeviceData.Get().getFructoseTemperature1()));

            gtwd2_up.setText(String.valueOf(DeviceData.Get().getFructoseTemperature2_up()));
            gtwd2_down.setText(String.valueOf(DeviceData.Get().getFructoseTemperature2_down()));
            wendu2.setText(String.valueOf(DeviceData.Get().getFructoseTemperature2()));

            gtwd3_up.setText(String.valueOf(DeviceData.Get().getWashTheTankTemperature_up()));
            gtwd3_down.setText(String.valueOf(DeviceData.Get().getWashTheTankTemperature_down()));
            wendu3.setText(String.valueOf(DeviceData.Get().getWashTheTankTemperature()));

        } 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.mwSave_button1://按钮1
                try {
                    int up = Integer.parseInt((gtwd1_up.getText().toString().isEmpty() ? "0" : gtwd1_up.getText().toString()));
                    int down = Integer.parseInt((gtwd1_down.getText().toString().isEmpty() ? "0" : gtwd1_down.getText().toString()));
                    if (down <= 0) {
                        T.show(this, "温度下限不能小于0!请调整后重试.");
                        return;
                    }
                    if (up <= down) {
                        T.show(this, "温度上限不能小于等于温度下限!请调整后重试.");
                        return;
                    }
                    DeviceData.Get().setFructoseTemperature1(up, down, new IWriteCallBack() {
                        @Override
                        public void onSuccess() {
                            T.show(wdgl_activity.this, "保存成功.");
                        }

                        @Override
                        public void onFailure(String ErrorMsg) {
                            T.show(wdgl_activity.this, "保存失败.");
                        }
                    });
                } catch (Exception e) {
                    T.show(this, "异常信息:"+e.getMessage());
                }
                break;
            case R.id.mwSave_button2://按钮2
                try {
                    int up = Integer.parseInt((gtwd2_up.getText().toString().isEmpty() ? "0" : gtwd2_up.getText().toString()));
                    int down = Integer.parseInt((gtwd2_down.getText().toString().isEmpty() ? "0" : gtwd2_down.getText().toString()));
                    if (down <= 0) {
                        T.show(this, "温度下限不能小于0!请调整后重试.");
                        return;
                    }
                    if (up <= down) {
                        T.show(this, "温度上限不能小于等于温度下限!请调整后重试.");
                        return;
                    }
                    DeviceData.Get().setFructoseTemperature2(up, down, new IWriteCallBack() {
                        @Override
                        public void onSuccess() {
                            T.show(wdgl_activity.this, "保存成功.");
                        }

                        @Override
                        public void onFailure(String ErrorMsg) {
                            T.show(wdgl_activity.this, "保存失败.");
                        }
                    });                } catch (Exception e) {
                    T.show(this, "异常信息:"+e.getMessage());
                }
                break;
            case R.id.mwSave_button3://按钮3
                try {
                    int up = Integer.parseInt((gtwd3_up.getText().toString().isEmpty() ? "0" : gtwd3_up.getText().toString()));
                    int down = Integer.parseInt((gtwd3_down.getText().toString().isEmpty() ? "0" : gtwd3_down.getText().toString()));
                    if (down <= 0) {
                        T.show(this, "温度下限不能小于0!请调整后重试.");
                        return;
                    }
                    if (up <= down) {
                        T.show(this, "温度上限不能小于等于温度下限!请调整后重试.");
                        return;
                    }
                    DeviceData.Get().setWashTheTankTemperature(up, down, new IWriteCallBack() {
                        @Override
                        public void onSuccess() {
                            T.show(wdgl_activity.this, "保存成功.");
                        }

                        @Override
                        public void onFailure(String ErrorMsg) {
                            T.show(wdgl_activity.this, "保存失败.");
                        }
                    });                } catch (Exception e) {
                    T.show(this, "异常信息:"+e.getMessage());
                }
                break;
        }
    }

    //endregion
}