package com.example.iot_controlhost.utils.control.ventilator;

import com.example.iot_controlhost.model.thread.QueueIOTask;
import com.example.iot_controlhost.utils.MyQueue;
import com.example.iot_controlhost.utils.MyUtil;
import com.example.iot_controlhost.utils.control.Control;
import com.example.iot_controlhost.utils.global.AIModelSet;
import com.example.iot_controlhost.utils.global.AutoModelSet;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.global.RoomSetting;
import com.example.iot_controlhost.utils.global.RxTag;
import com.example.iot_controlhost.utils.global.SpinnerList;
import com.example.iot_controlhost.utils.log.MyLog;

/**
 * @Description 自动模式下换气扇控制
 * @Author DuanKaiji
 * @CreateTime 2023年11月09日 10:26:30
 */
public class VentilatorController extends Control {
    /**
     * 开启持续时间
     */
    int targetOpen;
    /**
     * 关闭持续时间
     */
    int targetClose;

    /**
     * 单例模式
     */
    private static VentilatorController instance;

    public static VentilatorController getInstance() {
        if (instance == null) {
            synchronized (VentilatorController.class) {
                if (instance == null) {
                    instance = new VentilatorController();
                }
            }
        }
        return instance;
    }

    @Override
    protected boolean startMethod() {
        //每次开启时获取最新参数
        refreshRule();
        if (!RoomController.fan.isAvailable()) {
            MyLog.autoError("换气自动控制无法启动：换气扇不可用");
            return false;
        }
        MyLog.auto("换气自动控制初始化：先关闭换气扇，开启时长=" + targetOpen
                + "分钟，关闭时长=" + targetClose + "分钟");
        stopFan(0);
        return true;
    }

    private void refreshRule() {
        if (RoomSetting.getMode() == 1) {
            targetOpen = AutoModelSet.getCycleStart();
            targetClose = AutoModelSet.getCycleStop();
        } else if (RoomSetting.getMode() == 2) {
            targetOpen = AIModelSet.getAiCycleStart();
            targetClose = AIModelSet.getAiCycleStop();
        }
    }

    private void startFan(int minutes) {
        MyLog.auto("换气调度：计划" + minutes + "分钟后开启换气扇");
        pollingTask.startDelayedTask(RxTag.VENTILATOR_AUTO_START_FUN, minutes * 60, () -> {
            try {
                refreshRule();
                if (targetOpen == 0 && targetClose == 0) {
                    //如果开始停止时间均为0，则3分钟后再次检测配置项
                    MyLog.auto("换气控制暂停：开启和关闭时长均为0，3分钟后重新读取配置");
                    startFan(3);
                    return;
                }
                // 开启换气扇
                MyQueue.getInstance(MyQueue.TYPE_CONTROLLER).addTask(new QueueIOTask(() -> {
                    String airExchangeMode = AutoModelSet.getAirExchangeMode();
                    boolean fanResult = MyUtil.autoControlOperateThird(RoomController.fan, true);
                    MyLog.auto("换气控制执行结果：设备=换气扇，目标=开启，结果="
                            + (fanResult ? "成功" : "失败") + "，计划运行=" + targetOpen + "分钟");
                    if (RoomController.airExchange.isAvailable() && airExchangeMode.equals(SpinnerList.AIR_EXCHANGE_MODES[1])) {
                        boolean airExchangeResult = MyUtil.autoControlOperateThird(RoomController.airExchange, true);
                        MyLog.auto("换气联动执行结果：设备=新风，目标=开启，结果="
                                + (airExchangeResult ? "成功" : "失败"));
                    }
                }));
                MyLog.auto("换气调度：计划" + targetOpen + "分钟后关闭换气扇");
                stopFan(targetOpen);
            } catch (Throwable throwable) {
                MyLog.autoError("换气扇开启阶段异常：异常=" + throwable.getClass().getSimpleName()
                        + "，原因=" + throwable.getMessage() + "，1分钟后尝试恢复调度");
                startFan(1);
            }
        });
    }

    /**
     * 关闭风扇
     *
     * @param minutes 该时间后检测下一步动作
     */
    private void stopFan(int minutes) {
        // 开启换气扇，持续targetOpen分钟
        pollingTask.startDelayedTask(RxTag.VENTILATOR_AUTO_STOP_FUN, minutes * 60, () -> {
            try {
                refreshRule();
                // 关闭换气扇
                MyQueue.getInstance(MyQueue.TYPE_CONTROLLER).addTask(new QueueIOTask(() -> {
                    if (RoomController.airExchange.isAvailable() && AutoModelSet.getAirExchangeMode().equals(SpinnerList.AIR_EXCHANGE_MODES[1])) {
                        boolean airExchangeResult = MyUtil.autoControlOperateThird(RoomController.airExchange, false);
                        MyLog.auto("换气联动执行结果：设备=新风，目标=关闭，结果="
                                + (airExchangeResult ? "成功" : "失败"));
                    }
                    boolean fanResult = MyUtil.autoControlOperateThird(RoomController.fan, false);
                    MyLog.auto("换气控制执行结果：设备=换气扇，目标=关闭，结果="
                            + (fanResult ? "成功" : "失败") + "，计划停运=" + targetClose + "分钟");
                }));
                startFan(targetClose);
            } catch (Throwable throwable) {
                MyLog.autoError("换气扇关闭阶段异常：异常=" + throwable.getClass().getSimpleName()
                        + "，原因=" + throwable.getMessage() + "，1分钟后尝试恢复调度");
                startFan(1);
            }
        });
    }

    @Override
    protected void stopMethod() {
        MyLog.auto("停止自动控制换气————————————————————————————————————————————————————");
        MyQueue.getInstance(MyQueue.TYPE_CONTROLLER).addTask(new QueueIOTask(() -> {
            MyUtil.autoControlOperateThird(RoomController.fan);
            if (RoomController.airExchange.isAvailable() && AutoModelSet.getAirExchangeMode().equals(SpinnerList.AIR_EXCHANGE_MODES[1])) {
                MyUtil.autoControlOperateThird(RoomController.airExchange, false);
            }
        }));
    }
}
