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

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.temperature.strategy.ControlStrategy;
import com.example.iot_controlhost.utils.control.temperature.strategy.FullPowerControl;
import com.example.iot_controlhost.utils.control.temperature.strategy.GearControl;
import com.example.iot_controlhost.utils.control.temperature.strategy.SavingControl;
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.RoomSensor;
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.global.Variable;
import com.example.iot_controlhost.utils.log.MyLog;

/**
 * @Description 自动模式-控制逻辑-温度 策略模式+工厂方法
 * @Author DuanKaiji
 * @CreateTime 2023年11月09日 10:26:30
 */
public class TemperatureController extends Control {
    /**
     * 目标温度
     */
    double target = 26;
    private static TemperatureController instance;
    private ControlStrategy strategy;

    private TemperatureController(ControlStrategy strategy) {
        this.strategy = strategy;
    }

    public static TemperatureController getInstance() {
        return getInstance(getStrategy());
    }

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

    private static ControlStrategy getStrategy() {
        String curMode = AutoModelSet.getAutoTemperatureMode();
        ControlStrategy strategy;
        if (curMode.equals(SpinnerList.TEMPERATURE_MODE[0])) {
            //高精度模式
            strategy = new FullPowerControl();
        } else if (curMode.equals(SpinnerList.TEMPERATURE_MODE[1])) {
            //普通模式
            strategy = new GearControl();
        } else if (curMode.equals(SpinnerList.TEMPERATURE_MODE[2])) {
            //节能模式
            strategy = new SavingControl();
        } else {
            strategy = new GearControl();
        }
        return strategy;
    }

    /**
     * 设置新自动控制温度算法
     */
    public void refreshStrategy() {
        if (start) {
            MyLog.auto("切换自动温控参数：" +
                    "\n-算法" + AutoModelSet.getAutoTemperatureMode() +
                    "\n-加热方式" + AutoModelSet.getAutoTemperatureHeat() +
                    "\n-降温方式" + AutoModelSet.getAutoTemperatureCool());
            start();
            strategy = getStrategy();
            start = startMethod();
        }
    }

    /**
     * 控制温度
     */
    private void controlTemperature(double curTemperature, double tarTemperature) {
        if (strategy != null) {
            MyQueue.getInstance(MyQueue.TYPE_CONTROLLER).addTask(new QueueIOTask(() -> strategy.control(curTemperature, tarTemperature)));
        }
    }

    @Override
    protected boolean startMethod() {
        MyLog.auto("开启温度自动控制");
        MyLog.auto("启用" + AutoModelSet.getAutoTemperatureMode() + "算法");
        pollingTask.startPollingTaskOnIOThread(RxTag.AUTO_TEMPERATURE, Variable.AUTO_CHECK_TIME, () -> {
            double curTemperature = RoomSensor.getAverageIndoorTemperature();
            if (curTemperature == 0) {
                MyLog.autoError("室内温度计为0,有误，停止该次温度自动控制操作");
                return;
            }
            if (RoomSetting.getMode() == 1) {
                target = AutoModelSet.getTargetTemperature();
            } else if (RoomSetting.getMode() == 2) {
                target = AIModelSet.getTargetTemperature();
            }
            controlTemperature(curTemperature, target);
        });
        return true;
    }

    /**
     * 停止自动控制
     */
    protected void stopMethod() {
        MyLog.auto("停止自动控制温度————————————————————————————————————————————————————");
        MyQueue.getInstance(MyQueue.TYPE_CONTROLLER).addTask(new QueueIOTask(() -> {
            MyUtil.autoControlOperateThird(RoomController.airConditionInfrared);
            if (RoomController.airConditionInfrared2.isEnable()) {
                MyUtil.autoControlOperateThird(RoomController.airConditionInfrared2);
            }
            MyUtil.autoControlOperateThird(RoomController.floor);
        }));
    }

}
