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

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.SpinnerList;
import com.example.iot_controlhost.utils.global.Variable;
import com.example.iot_controlhost.utils.log.MyLog;

/**
 * @Description 高精度模式，与GearControl相同，只是温差范围不同，以用户自定义优先（普通模式不可设置过小的温差）
 * @Author DuanKaiji
 * @CreateTime 2023年10月24日 09:36:38
 */
public class FullPowerControl extends ControlStrategy {
    private static boolean needHeatToTarget = false;
    private static boolean needColdToTarget = false;

    @Override
    public void controlAlgorithm(double curTemperature, double tarTemperature) {
        double max = AutoModelSet.getMaxTemperature();
        double min = AutoModelSet.getMinTemperature();
        String autoTemperatureHeat = AutoModelSet.getAutoTemperatureHeat();
        String autoTemperatureCool = AutoModelSet.getAutoTemperatureCool();
        double outDoorTemperature = RoomSensor.outdoorTHSensor.temperature;
        if (curTemperature < tarTemperature - Variable.AUTO_DO_NOTHING_TEMPERATURE_NORMAL
                || curTemperature < min //用户自定义最低温度
                || curTemperature < tarTemperature && needHeatToTarget) {
            MyLog.auto("高精度模式：当前温度已低于目标值，开启最大档位升温");
            needHeatToTarget = true;
            if (autoTemperatureHeat.equals(SpinnerList.TEMPERATURE_HEAT[1])) {
                //地暖
                gear = 5;
                MyLog.auto("高精度模式：开启地暖最大档位");
            } else if (autoTemperatureHeat.equals(SpinnerList.TEMPERATURE_HEAT[2])) {
                //空调
                mode = 0;
                temp = 30;
                MyLog.auto("高精度模式：开启空调制热30度");
            } else if (autoTemperatureHeat.equals(SpinnerList.TEMPERATURE_HEAT[3])) {
                //空调和地暖
                mode = 0;
                temp = 30;
                gear = 5;
                MyLog.auto("高精度模式：开启空调制热30度与地暖5档");
            } else {
                //均不可用
                MyLog.auto("高精度模式：理应升温，但升温设备设置为‘均不可用’");
            }
        } else if (curTemperature > tarTemperature + Variable.AUTO_DO_NOTHING_TEMPERATURE_NORMAL
                || curTemperature > max // 用户自定义最大温度
                || curTemperature > tarTemperature && needColdToTarget) {
            MyLog.auto("高精度模式：当前温度已高于目标值，即将开启最大档位降温");
            needColdToTarget = true;
            if (autoTemperatureCool.equals(SpinnerList.TEMPERATURE_COOL[1])) {
                //空调
                mode = 2;
                temp = 17;
                MyLog.auto("高精度模式：开启空调制冷17度");
            } else {
                //均不可用
                MyLog.auto("高精度模式：理应降温，但降温设备设置为‘均不可用’");
            }
        } else {
            //是目标范围温度
            needHeatToTarget = false;
            needColdToTarget = false;
            //地暖逻辑
            if ((autoTemperatureHeat.equals(SpinnerList.TEMPERATURE_HEAT[1]) || autoTemperatureHeat.equals(SpinnerList.TEMPERATURE_HEAT[3]))//可以用地暖
                    && curTemperature < tarTemperature) {//并且：当前温度低于目标温度
                //保温-重定向地暖档位-根据与目标温差
                double bias = tarTemperature - curTemperature;
                if (bias > 0.7) {
                    gear = 4;
                } else if (bias >= 0.5) {
                    gear = 3;
                } else if (bias >= 0.4) {
                    gear = 2;
                } else if (bias >= 0.3) {
                    gear = 1;
                }
                //保温-重定向地暖档位-根据室内外温差
                if (RoomSensor.outdoorTHSensor.isAvailable()) {
                    if (curTemperature > outDoorTemperature + 3) {
                        if (curTemperature > outDoorTemperature + 10 && gear <= 1) {
                            //室外温度 < 室内温度10度及以上，地暖2档保温
                            gear = 2;
                        } else if (curTemperature > outDoorTemperature + 3 && gear == 0) {
                            //室外温度 < 室内温度3度及以上，地暖1档保温
                            gear = 1;
                            MyLog.auto("高精度模式：室外温度与室内温度偏差3度及以上，" + RoomSetting.getFloorName() + "1档保温");
                        }
                    } else if (curTemperature < outDoorTemperature - 2) {
                        //室外温度 > 室内温度2度及以上，地暖不保温
                        gear = 0;
                        MyLog.auto("高精度模式：室外温度比室内高2度或以上，" + RoomSetting.getFloorName() + "关闭");
                    }
                }
            }
        }
    }
}