package com.example.iot_controlhost.ui.dialog;

import android.content.Context;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;

import com.example.iot_controlhost.R;
import com.example.iot_controlhost.base.BaseDialog;
import com.example.iot_controlhost.databinding.DialogDeviceCustomSetBinding;
import com.example.iot_controlhost.model.controller.relay.Relay;
import com.example.iot_controlhost.ui.view.CustomSpinnerAdapter;
import com.example.iot_controlhost.utils.MyUtil;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.log.UserLog;
import com.example.iot_controlhost.utils.log.MyLog;

import java.util.Arrays;
import java.util.HashSet;

import android_serialport_api.SerialPortUtil;
import es.dmoral.toasty.Toasty;

/**
 * @Description 设置端口设备
 * @Author DuanKaiji
 * @CreateTime 2024年04月10日 11:45:19
 */
public class SetPortDialog extends BaseDialog<DialogDeviceCustomSetBinding> {
    public SetPortDialog(@NonNull Context context, int port,Runnable afterSetRelay) {
        super(context,true);
        binding.ivDialogClose.setOnClickListener(view -> {
            RoomController.relay.setPowerSupply(false, port);
            dismiss();
        });
        binding.tvTitle.setText(port + mContext.getString(R.string.set));
        binding.tilSpinner1.setVisibility(View.VISIBLE);
        binding.acInput1.setHint(mContext.getString(R.string.no_select));
        // 删除部分设备
        java.util.Set<String> set = new HashSet<>(Arrays.asList(RoomController.DEVICE_NAME));
        set.remove("空调红外控制器");
        set.remove("继电器");
        set.remove("换气扇");
        binding.acInput1.setAdapter(new CustomSpinnerAdapter(mContext, set.toArray(new String[0])));
        binding.btn1.setVisibility(View.VISIBLE);
        binding.btn1.setText(mContext.getText(R.string.test_power_on));
        binding.btn1.setOnClickListener(view -> {
            Button button = (Button) view;
            if (SerialPortUtil.getInstance(SerialPortUtil.TYPE_CONTROLLER).openSerialPort()) {
                boolean powerSupply;
                if (button.getText().toString().equals(getString(R.string.test_power_on))) {
                    //测试开
                    powerSupply = true;
                    button.setText(R.string.test_power_off);
                } else {
                    //测试关
                    powerSupply = false;
                    button.setText(R.string.test_power_on);
                }
                RoomController.relay.setPowerSupply(powerSupply, port);
            } else {
                Toasty.error(mContext, "串口未开启").show();
            }
        });
        binding.btnYes.setText(mContext.getText(R.string.set));
        binding.btnYes.setOnClickListener(view -> {
            //移除之前设备的端口设置
            String oldDeviceName = MyUtil.getPortToDeviceName(port);
            Relay relay = RoomController.getRelay(oldDeviceName);
            if (relay != null) {
                relay.removePort(port);
            } else {
                //当前端口之前未配置
            }
            String deviceName = binding.acInput1.getText().toString();
            if (!deviceName.isEmpty()) {
                //设置新设备的端口设置
                Relay r = RoomController.getRelay(deviceName);
                if (r != null) {
                    r.addPort(port);
                } else {
                    Toasty.error(mContext, "当前设备不支持").show();
                }
            }
            //确定后关闭弹窗
            RoomController.relay.setPowerSupply(false, port);
            UserLog.operate("本机屏幕", "继电器端口配置变化：端口" + port + "=" + oldDeviceName
                    + " → " + (deviceName.isEmpty() ? "未配置" : deviceName));
            MyLog.app("继电器端口配置修改：端口=" + port + "，设备=" + oldDeviceName
                    + "→" + (deviceName.isEmpty() ? "未配置" : deviceName));
            dismiss();
            afterSetRelay.run();
        });
    }

}
