package com.example.iot_controlhost.utils.database.dynamicSensor;

import com.example.iot_controlhost.model.sensor.GasSensor;
import com.example.iot_controlhost.model.sensor.Sensor;
import com.example.iot_controlhost.model.sensor.THSensor;
import com.example.iot_controlhost.utils.MMKVUtil;
import com.example.iot_controlhost.utils.MyUtil;
import com.example.iot_controlhost.utils.global.RoomSensor;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

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

    public static final String INDOOR_TH_SENSOR = "INDOOR_SENSOR_TS2";

    public static String[] getAllNames() {
        String json = MMKVUtil.get(INDOOR_TH_SENSOR, "[]");
        List<THSensor> temp = new Gson().fromJson(json, new TypeToken<List<THSensor>>() {
        }.getType());
        String[] names = new String[temp.size()];
        for (int i = 0; i < temp.size(); i++) {
            names[i] = temp.get(i).getName();
        }
        return names;
    }

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

    public static boolean insert(THSensor sensor) {
        String json = MMKVUtil.get(INDOOR_TH_SENSOR, "[]");
        List<THSensor> temp = new Gson().fromJson(json, new TypeToken<List<THSensor>>() {
        }.getType());
        //判断是否已经存在同名传感器
        if (MyUtil.checkDeviceNameAvailable(sensor.getName())) {
            temp.add(sensor);
            MMKVUtil.put(INDOOR_TH_SENSOR, new Gson().toJson(temp));
            return true;
        }
        return false;
    }

    public static void update(Sensor sensor) {
        String json = MMKVUtil.get(INDOOR_TH_SENSOR, "[]");
        List<THSensor> temp = new Gson().fromJson(json, new TypeToken<List<THSensor>>() {
        }.getType());
        if (temp != null) {
            for (THSensor mode : temp) {
                if (mode.getName().equals(sensor.getName())) {
                    mode.setEnable(sensor.isEnable());
                    mode.setModel(sensor.getModel());
                    mode.setAddress(sensor.getAddress());
                    break;
                }
            }
        }
        MMKVUtil.put(INDOOR_TH_SENSOR, new Gson().toJson(temp));
    }

    public static void deleteByName(String name) {
        String json = MMKVUtil.get(INDOOR_TH_SENSOR, "[]");
        List<THSensor> temp = new Gson().fromJson(json, new TypeToken<List<THSensor>>() {
        }.getType());
        if (temp != null) {
            for (THSensor sensor : temp) {
                if (sensor.getName().equals(name)) {
                    sensor.clearSetting();
                    temp.remove(sensor);
                    MMKVUtil.put(INDOOR_TH_SENSOR, new Gson().toJson(temp));
                    return;
                }
            }
        }
    }

    public static boolean hasSensor(String name) {
        String json = MMKVUtil.get(INDOOR_TH_SENSOR, "[]");
        List<THSensor> temp = new Gson().fromJson(json, new TypeToken<List<THSensor>>() {
        }.getType());
        if (temp != null) {
            for (THSensor sensor : temp) {
                if (sensor.getName().equals(name)) {
                    return true;
                }
            }
        }
        return false;
    }
}
