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

import com.example.iot_controlhost.model.controller.AirConditionInfrared;
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.control.infrared.MyInfraredUtils;
import com.example.iot_controlhost.utils.global.AutoModelSet;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.global.RxTag;
import com.example.iot_controlhost.utils.global.SpinnerList;
import com.example.iot_controlhost.utils.global.Variable;
import com.example.iot_controlhost.utils.log.MyLog;
import com.xuexiang.rxutil2.rxjava.DisposablePool;
import com.xuexiang.rxutil2.rxjava.RxJavaUtils;

/**
 * @Description 自动模式-控制逻辑-匀风扇
 * @Author DuanKaiji
 * @CreateTime 2023年12月25日 10:46:05
 */
public class EvenController extends Control {

    private static EvenController instance;

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

    @Override
    protected boolean startMethod() {
        MyLog.auto("匀风扇开始自动控制——————————————————————————————————————————————————————————————————");
        if (!RoomController.evenFan.isAvailable()) {
            return false;
        }
        pollingTask.startPollingTaskOnIOThread(RxTag.AUTO_EVEN, Variable.AUTO_CHECK_TIME, () -> {
            boolean target;
            if (AutoModelSet.isAutoEvenModeDuringHumidify() && RoomController.humidifier.isPowerSupply()) {
                //加湿器工作时匀风扇不工作
                target = false;
            } else {
                // 检测控制方式{"手动控制", "升温时跟随启动", "降温时跟随启动", "升降温时跟随启动"}
                String evenMode = AutoModelSet.getAutoEvenMode();
                if (evenMode.equals(SpinnerList.AUTO_EVEN_MODES[1])) {
                    //升温时跟随启动
                    target = RoomController.floor.isPowerSupply() ||
                            (MyInfraredUtils.getInstances().isPowerSupply() && MyInfraredUtils.getInstances().getMode() == AirConditionInfrared.HEAT);
                } else if (evenMode.equals(SpinnerList.AUTO_EVEN_MODES[2])) {
                    //降温时跟随启动
                    target = !RoomController.floor.isPowerSupply() &&
                            (MyInfraredUtils.getInstances().isPowerSupply() && MyInfraredUtils.getInstances().getMode() == AirConditionInfrared.COLD);
                } else if (evenMode.equals(SpinnerList.AUTO_EVEN_MODES[3])) {
                    //升降温时跟随启动
                    target = RoomController.floor.isPowerSupply() || MyInfraredUtils.getInstances().isPowerSupply();
                } else {
                    //手动控制
                    target = false;
                }
            }
            MyQueue.getInstance(MyQueue.TYPE_CONTROLLER).addTask(new QueueIOTask(() -> MyUtil.autoControlOperateThird(RoomController.evenFan, target)));
        });
        return true;
    }

    @Override
    protected void stopMethod() {
        MyQueue.getInstance(MyQueue.TYPE_CONTROLLER).addTask(new QueueIOTask(() -> {
            MyUtil.autoControlOperateThird(RoomController.evenFan);
        }));
        MyLog.auto("停止自动控制匀风————————————————————————————————————————————————————");
    }
}
