package com.example.iot_controlhost.utils.global;

import com.example.iot_controlhost.utils.MMKVUtil;

/**
 * 自动模式下的相关配置
 * 配合MMKV使用
 *
 * @author DuanKaiji
 */
public class AutoModelSet {

    //————————————————————————总控相关————————————————————————
    /**
     * 自动模式-升温方式
     * {"均不启用", "地暖", "空调", "地暖和空调"}
     */
    public static final String AUTO_TEMPERATURE_HEAT = "AUTO_TEMPERATURE_HEAT";
    /**
     * 自动模式-降温方式
     */
    public static final String AUTO_TEMPERATURE_COOL = "AUTO_TEMPERATURE_COOL";
    /**
     * 自动模式-控制方式
     * {"比例控制", "偏差控制", "PID控制", "分挡位控制", "全功率控制"};
     */
    public static final String AUTO_TEMPERATURE_MODE = "AUTO_TEMPERATURE_MODE";
    /**
     * 自动模式-匀风扇控制算法
     * {"手动控制", "升温时跟随启动", "降温时跟随启动", "升降温时跟随启动"}
     */
    public static final String AUTO_EVEN_MODE = "AUTO_EVEN_MODE";
    /**
     * 换气扇控制算法
     * {"全部开启", "仅开进气扇", "仅开排气扇"}
     */
    public static final String VENTILATOR_MODE = "VENTILATOR_MODE";

    public static String getVentilatorMode() {
        return MMKVUtil.get(VENTILATOR_MODE, SpinnerList.VENTILATION_MODES[0]);
    }

    public static String getAutoEvenMode() {
        return MMKVUtil.get(AUTO_EVEN_MODE, SpinnerList.AUTO_EVEN_MODES[3]);
    }

    /**
     * 匀风扇在加湿期间不开启
     */
    public static final String AUTO_EVEN_MODE_DURING_HUMIDIFY = "AUTO_EVEN_MODE_DURING_HUMIDIFY";

    public static boolean isAutoEvenModeDuringHumidify() {
        return MMKVUtil.get(AUTO_EVEN_MODE_DURING_HUMIDIFY, true);
    }

    /**
     * 自动模式-新风控制控制算法
     * {"总是关闭","随换气扇启动", "随加湿器启动"}
     */
    public static final String AIR_EXCHANGE_MODE = "AIR_EXCHANGE_MODE";

    public static String getAirExchangeMode() {
        return MMKVUtil.get(AIR_EXCHANGE_MODE, SpinnerList.AIR_EXCHANGE_MODES[0]);
    }
    /**
     * 升温方式
     *
     * @return {"均不启用", "地暖", "空调", "地暖和空调"}
     */
    public static String getAutoTemperatureHeat() {
        return MMKVUtil.get(AUTO_TEMPERATURE_HEAT, SpinnerList.TEMPERATURE_HEAT[3]);
    }

    /**
     * 降温方式
     * @return {"均不启用", "空调"}
     */
    public static String getAutoTemperatureCool() {
        return MMKVUtil.get(AUTO_TEMPERATURE_COOL, SpinnerList.TEMPERATURE_COOL[1]);
    }


    public static String getAutoTemperatureMode() {
        return MMKVUtil.get(AUTO_TEMPERATURE_MODE, SpinnerList.TEMPERATURE_MODE[1]);
    }

    //————————————————————————温度相关————————————————————————
    /**
     * 温控是否自动
     */
    public static final String AUTO_TEMPERATURE = "AUTO_TEMPERATURE";
    /**
     * 目标温度
     */
    public static final String TARGET_TEMPERATURE = "TARGET_TEMPERATURE";
    /**
     * 温度预警最大值
     */
    public static final String MAX_TEMPERATURE = "MAX_TEMPERATURE";
    /**
     * 温度预警最小值
     */
    public static final String MIN_TEMPERATURE = "MIN_TEMPERATURE";

    public static boolean isAutoTemperature() {
        return MMKVUtil.get(AUTO_TEMPERATURE, true);
    }

    public static double getTargetTemperature() {
        String formattedValue = String.format("%.2f", MMKVUtil.get(TARGET_TEMPERATURE, 26.0));
        return Double.parseDouble(formattedValue);
    }

    public static double getMaxTemperature() {
        String formattedValue = String.format("%.1f", MMKVUtil.get(MAX_TEMPERATURE, 27.0));
        return Double.parseDouble(formattedValue);
    }

    public static double getMinTemperature() {
        String formattedValue = String.format("%.1f", MMKVUtil.get(MIN_TEMPERATURE, 25.0));
        return Double.parseDouble(formattedValue);
    }

    //————————————————————————湿度相关————————————————————————
    /**
     * 湿度控制是否自动
     */
    public static final String AUTO_HUMIDITY = "AUTO_HUMIDITY";
    /**
     * 目标湿度
     */
    public static final String TARGET_HUMIDITY = "TARGET_HUMIDITY";
    /**
     * 湿度预警最大值
     */
    public static final String MAX_HUMIDITY = "MAX_HUMIDITY";
    /**
     * 湿度预警最小值
     */
    public static final String MIN_HUMIDITY = "MIN_HUMIDITY";

    public static double getMaxHumidity() {
        String formattedValue = String.format("%.1f", MMKVUtil.get(MAX_HUMIDITY, 70.0));
        return Double.parseDouble(formattedValue);
    }

    public static double getMinHumidity() {
        String formattedValue = String.format("%.1f", MMKVUtil.get(MIN_HUMIDITY, 50.0));
        return Double.parseDouble(formattedValue);
    }

    public static double getTargetHumidity() {
        String formattedValue = String.format("%.1f", MMKVUtil.get(TARGET_HUMIDITY, 60.0));
        return Double.parseDouble(formattedValue);
    }

    public static boolean isAutoHumidity() {
        return MMKVUtil.get(AUTO_HUMIDITY, true);
    }


    //————————————————————————换气相关————————————————————————
    /**
     * 换气控制是否自动
     */
    public static final String AUTO_VENTILATOR = "AUTO_VENTILATOR";
    /**
     * 执行时间
     */
    public static final String CYCLE_START = "CYCLE_START";
    /**
     * 停止时间
     */
    public static final String CYCLE_STOP = "CYCLE_STOP";
    /**
     * CO2最大浓度
     */
    public static final String MAX_DENSITY_CO2 = "MAX_DENSITY_CO2";

    public static boolean isAutoVentilator() {
        return MMKVUtil.get(AUTO_VENTILATOR, true);
    }

    public static int getCycleStart() {
        return MMKVUtil.get(CYCLE_START, 3);
    }

    public static int getCycleStop() {
        return MMKVUtil.get(CYCLE_STOP, 60);
    }

    public static int getMaxDensityCO2() {
        int maxDensityCO2 = MMKVUtil.get(MAX_DENSITY_CO2, 1000);
        if (maxDensityCO2 < 1000) {
            return 1000;
        }
        return maxDensityCO2;
    }


    //————————————————————————消毒相关————————————————————————
    /**
     * 消毒控制是否自动
     */
    public static final String AUTO_DISINFECT = "AUTO_DISINFECT";

    public static boolean isAutoDisinfect() {
        return MMKVUtil.get(AUTO_DISINFECT, true);
    }


    //————————————————————————匀风相关————————————————————————
    /**
     * 消毒控制是否自动
     */
    public static final String AUTO_EVEN = "AUTO_EVEN";

    public static boolean isAutoEven() {
        return !AutoModelSet.getAutoEvenMode().equals(SpinnerList.AUTO_EVEN_MODES[0]);
    }
    /***
     * 自动模式下地暖最大温度限制
     * double
     */
    public static final String AUTO_FLOOR_MAX_TEMP = "AUTO_FLOOR_MAX_TEMP";
    public static double getAutoFlorMaxTemp() {
        return MMKVUtil.get(AUTO_FLOOR_MAX_TEMP, Variable.FLOOR_MAX_TEMPERATURE_LIMIT);
    }

}
