package com.example.iot_controlhost.utils.global;

import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.TimeUtils;
import com.example.iot_controlhost.model.Silkworm;
import com.example.iot_controlhost.model.SilkwormNew;
import com.example.iot_controlhost.utils.MMKVUtil;
import com.example.iot_controlhost.utils.database.SilkwormDBManager;

public class AIModelSet {

    /**
     * 智能方案名称
     */
    public static final String AI_SCHEME_NAME = "AI_SCHEME_NAME";
    /**
     * 智能方案ID
     */
    public static final String AI_CONTROL_PROGRAM_ID = "AI_CONTROL_PROGRAM_ID";
    /**
     * 智能方案-总天数
     */
    public static final String AI_CONTROL_PROGRAM_TOTAL_DAYS = "AI_CONTROL_PROGRAM_TOTAL_DAYS";

    /**
     * 智能模式下是否共育中
     * 智能模式下开启和停止设备
     */
    public static final String AI_PROCESSING = "AI_PROCESSING";

    /**
     * 当前所处智能方案内的所处时间
     * 与共育时间不同，此时间可能会由于具体的共育龄期不同而前后调整
     * 单位：小时
     */
    public static final String AI_BIAS_TIME = "AI_BIAS_TIME";

    public static int getAiControlProgramTotalDays(){
        return MMKVUtil.get(AI_CONTROL_PROGRAM_TOTAL_DAYS,15);
    }

    public static int getAiBiasTime() {
        return MMKVUtil.get(AI_BIAS_TIME, 0);
    }

    /**
     * 获取当前共育第几个小时
     * 可能会被农户手动调整所影响，与共育天数可能存在差异
     */
    public static int getStartToNowHours() {
        SilkwormNew silkworm = SilkwormDBManager.getLatestSilkworm();
        int result = 0;
        if (SilkwormDBManager.isStarting()) {
            result = (int) -TimeUtils.getTimeSpanByNow(silkworm.getStartDate(), TimeConstants.HOUR);
        }
        result += getAiBiasTime();
        result = result < 0 ? 0 : result;
        return result;
    }

    public static String getAiSchemeName() {
        return MMKVUtil.get().get(AI_SCHEME_NAME);
    }

    public static boolean isAiProcessing() {
        return MMKVUtil.get(AI_PROCESSING, true);
    }

    public static String getAiControlProgramId() {
        return MMKVUtil.get().get(AI_CONTROL_PROGRAM_ID);
    }

    //————————————————————————温度相关————————————————————————
    /**
     * 目标温度
     */
    public static final String AI_TARGET_TEMPERATURE = "AI_TARGET_TEMPERATURE";

    public static double getTargetTemperature() {
        return MMKVUtil.get(AI_TARGET_TEMPERATURE, 26.0);
    }

    public static void setTargetTemperature(double value) {
        MMKVUtil.put(AI_TARGET_TEMPERATURE, value);
    }

    //————————————————————————湿度相关————————————————————————
    /**
     * 目标湿度
     */
    public static final String AI_TARGET_HUMIDITY = "AI_TARGET_HUMIDITY";


    public static double getTargetHumidity() {
        return MMKVUtil.get(AI_TARGET_HUMIDITY, 50.0);
    }

    public static void setAiTargetHumidity(double value) {
        MMKVUtil.put(AI_TARGET_HUMIDITY, value);
    }

    //————————————————————————换气相关————————————————————————

    /**
     * 执行时间
     */
    public static final String AI_CYCLE_START = "AI_CYCLE_START";
    /**
     * 停止时间
     */
    public static final String AI_CYCLE_STOP = "AI_CYCLE_STOP";

    public static int getAiCycleStart() {
        return MMKVUtil.get(AI_CYCLE_START, 5);
    }

    public static void setAiCycleStart(int value) {
        MMKVUtil.put(AI_CYCLE_START, value);
    }

    public static int getAiCycleStop() {
        return MMKVUtil.get(AI_CYCLE_STOP, 5);
    }

    public static void setAiCycleStop(int value) {
        MMKVUtil.put(AI_CYCLE_STOP, value);
    }

}
