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.EditText;
import android.widget.ImageView;
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;

public class wdgl_new_Activity extends AppCompatActivity implements View.OnClickListener {

    //region 变量
    /**
     * 返回按钮
     */
    ImageView gongneng_fanhui;
    /**
     * 标题设置
     */
    TextView gongneng_title;
    /**
     * 温度上限
     */
    EditText sc_bw, jrx_bw;
    /**
     * 当前温度
     */
    TextView wendu1, wendu2;
    /**
     * 保存按钮
     */
    Button mwSave_button1, mwSave_button2;
    //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_new);
        Init();
        initEvents();
        startListening();
    }
    //endregion

    //region 公共函数

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


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

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


        //通过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);
    }

    /**
     * 监听外部数据变化
     */
    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().getBWX_FructoseTemperature()) + "°C");
                                wendu2.setText(String.valueOf(DeviceData.Get().getJRX_FructoseTemperature()) + "°C");
                            }
                        });
                        Thread.sleep(1000);

                    } catch (Exception e) {

                    }
                }
            }
        }).start();
    }

    /**
     * 初始化数据加载
     */
    public void Initdata() {
        try {
            wendu1.setText(String.valueOf(DeviceData.Get().getBWX_FructoseTemperature()));
            wendu2.setText(String.valueOf(DeviceData.Get().getJRX_FructoseTemperature()));

            sc_bw.setText(String.valueOf(DeviceData.Get().getBWX_FructoseTemperatureSZ()));
            jrx_bw.setText(String.valueOf(DeviceData.Get().getJRX_FructoseTemperatureSZ()));

        } 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((sc_bw.getText().toString().isEmpty() ? "0" : sc_bw.getText().toString()));
                    if (up <= 0) {
                        T.show(this, "温度不能小于0!请调整后重试.");
                        return;
                    }

                    DeviceData.Get().setBWX_FructoseTemperature(up, new IWriteCallBack() {
                        @Override
                        public void onSuccess() {
                            T.show(wdgl_new_Activity.this, "保存成功.");
                        }

                        @Override
                        public void onFailure(String ErrorMsg) {
                            T.show(wdgl_new_Activity.this, "保存失败.");
                        }
                    });
                } catch (Exception e) {
                    T.show(this, "异常信息:" + e.getMessage());
                }
                break;
            case R.id.mwSave_button2://按钮2
                try {
                    int up = Integer.parseInt((jrx_bw.getText().toString().isEmpty() ? "0" : jrx_bw.getText().toString()));
                    if (up <= 0) {
                        T.show(this, "温度不能小于0!请调整后重试.");
                        return;
                    }

                    DeviceData.Get().setJRX_FructoseTemperature(up, new IWriteCallBack() {
                        @Override
                        public void onSuccess() {
                            T.show(wdgl_new_Activity.this, "保存成功.");
                        }

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

    //endregion
}