package com.example.iot_controlhost.ui.dialog;

import android.content.Context;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.StringUtils;
import com.example.iot_controlhost.R;
import com.example.iot_controlhost.base.BaseDialog;
import com.example.iot_controlhost.databinding.DialogSetSensorBinding;
import com.example.iot_controlhost.model.sensor.Sensor;
import com.example.iot_controlhost.model.thread.QueueIOUITask;
import com.example.iot_controlhost.ui.view.CustomSpinnerAdapter;
import com.example.iot_controlhost.utils.MyQueue;
import com.example.iot_controlhost.utils.global.SpinnerList;
import com.example.iot_controlhost.utils.log.UserLog;
import com.example.iot_controlhost.utils.log.MyLog;

import android_serialport_api.SerialPortUtil;

/**
 * @Description 配置传感器弹窗
 * @Author DuanKaiji
 * @CreateTime 2024年03月13日 13:46
 */
public class SetSensorDialog extends BaseDialog<DialogSetSensorBinding> {
    Sensor sensor;

    boolean isEnable;

    public SetSensorDialog(@NonNull Context context, Sensor sensor, setSensor listener) {
        super(context, true);
        this.sensor = sensor;
        binding.tvTitle.setText(sensor.getName() + context.getString(R.string.set));
        binding.acSensorModel.setHint(context.getString(R.string.select_model));
        binding.acSensorModel.setAdapter(new CustomSpinnerAdapter(context, sensor.getModels()));
        binding.acAddress.setHint(context.getString(R.string.select_address));
        binding.acAddress.setAdapter(new CustomSpinnerAdapter(context, SpinnerList.ADDRESS));
        isEnable = sensor.isEnable();
        binding.switcher.setChecked(isEnable);
        binding.switcher.setOnCheckedChangeListener((compoundButton, b) -> isEnable = b);
        binding.btnLoadData.setOnClickListener(view -> {
            String address = binding.acAddress.getText().toString();
            if (StringUtils.isEmpty(address)) {
                infoTips("请先选择地址");
                return;
            }
            //读数据事件
            MyQueue.start(MyQueue.TYPE_SENSOR, new QueueIOUITask<String>() {
                @Override
                public String doInQueueThread() throws Exception {
                    //先通过指定波特率打开串口
                    if (SerialPortUtil.getInstance(SerialPortUtil.TYPE_SENSOR).openSerialPort()) {
                        //尝试读取数据
                        if (sensor.collectSensorData(binding.acSensorModel.getText().toString(), address)) {
                            return sensor.toString();
                        } else {
                            return mContext.getString(R.string.operate_fail);
                        }
                    } else {
                        return "串口打开失败";
                    }
                }

                @Override
                public void doInUIThread(String s) {
                    binding.tvSensorInfo.setText(s);
                }
            });
        });
        binding.btnYes.setOnClickListener(button -> {
            //设置事件
            boolean oldEnable = sensor.isEnable();
            String oldModel = sensor.getModel();
            String oldAddress = sensor.getAddress();
            boolean enable = binding.switcher.isChecked();
            if (enable) {
                String address = binding.acAddress.getText().toString();
                String model = binding.acSensorModel.getText().toString();
                if (StringUtils.isEmpty(address)) {
                    infoTips("请先选择型号");
                    return;
                }
                if (StringUtils.isEmpty(model)) {
                    infoTips("请先选择地址");
                    return;
                }
                sensor.setEnable(isEnable);
                sensor.setModel(binding.acSensorModel.getText().toString());
                sensor.setAddress(binding.acAddress.getText().toString());
            }
            sensor.setEnable(enable);
            //刷新页面
            listener.run(sensor);
            String change = "传感器配置变化：名称=" + sensor.getName() + "，启用=" + oldEnable + " → "
                    + sensor.isEnable() + "，型号=" + oldModel + " → " + sensor.getModel()
                    + "，地址=" + oldAddress + " → " + sensor.getAddress();
            UserLog.operate("本机屏幕", change);
            MyLog.app(change);
            dismiss();
        });
    }

    public interface setSensor{
        void run(Sensor sensor);
    }

}
