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

import com.example.iot_controlhost.model.sensor.Sensor;
import com.example.iot_controlhost.model.sensor.FloorTSensor;
import com.example.iot_controlhost.utils.MMKVUtil;
import com.example.iot_controlhost.utils.MyUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

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

    public static final String INDOOR_FLOOR_T_SENSOR = "INDOOR_FLOOR_T_SENSOR";

    public static String[] getAllNames() {
        String json = MMKVUtil.get(INDOOR_FLOOR_T_SENSOR, "[]");
        List<FloorTSensor> temp = new Gson().fromJson(json, new TypeToken<List<FloorTSensor>>() {
        }.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<FloorTSensor> getAllSensors() {
        String json = MMKVUtil.get(INDOOR_FLOOR_T_SENSOR, "[]");
        List<FloorTSensor> temp = new Gson().fromJson(json, new TypeToken<List<FloorTSensor>>() {
        }.getType());
        return temp;
    }

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

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

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

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