package com.example.iot_controlhost.utils.database;

import com.example.iot_controlhost.model.AITHMode;
import com.example.iot_controlhost.utils.MMKVUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

/**
 * 农户模板工具类
 */
public class MyAIModeUtil {

    public static final String MY_AI_MODE = "MY_AI_MODE";

    public static void initMyAiMode(List<AITHMode> temp) {
        MMKVUtil.put(MY_AI_MODE, new Gson().toJson(temp));
    }

    public static List<AITHMode> getMyAiMode() {
        String json = MMKVUtil.get(MY_AI_MODE, "[]");
        List<AITHMode> temp = new Gson().fromJson(json, new TypeToken<List<AITHMode>>() {
        }.getType());
        return temp;
    }

    public static void update(AITHMode aithMode) {
        String json = MMKVUtil.get(MY_AI_MODE, "[]");
        List<AITHMode> temp = new Gson().fromJson(json, new TypeToken<List<AITHMode>>() {
        }.getType());
        for (AITHMode mode : temp) {
            if (mode.getSort() == aithMode.getSort()) {
                mode.setTargetTemp(aithMode.getTargetTemp());
                mode.setTargetHumi(aithMode.getTargetHumi());
                mode.setTargetVentOpen(aithMode.getTargetVentOpen());
                mode.setTargetVentClose(aithMode.getTargetVentClose());
                break;
            }
        }
        MMKVUtil.put(MY_AI_MODE, new Gson().toJson(temp));
    }

    public static void removeMyAiMode() {
        MMKVUtil.put(MY_AI_MODE, "[]");
    }

}
