package com.example.iot_controlhost.utils.old;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;

import com.example.iot_controlhost.model.Device;
import com.example.iot_controlhost.model.MyTask;
import com.example.iot_controlhost.model.controller.Controller;
import com.example.iot_controlhost.model.controller.Floor;
import com.example.iot_controlhost.model.sensor.FloorTSensor;
import com.example.iot_controlhost.model.sensor.GasSensor;
import com.example.iot_controlhost.model.sensor.THSensor;
import com.example.iot_controlhost.utils.MMKVUtil;
import com.example.iot_controlhost.utils.database.dynamicSensor.IndoorCO2SensorUtil;
import com.example.iot_controlhost.utils.database.dynamicSensor.IndoorFloorTSensorUtil;
import com.example.iot_controlhost.utils.database.dynamicSensor.IndoorTHSensorUtil;
import com.example.iot_controlhost.utils.timer.UVTaskUtil;
import com.example.iot_controlhost.utils.global.AutoModelSet;
import com.example.iot_controlhost.utils.global.HardwareSetting;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.global.RoomSensor;
import com.example.iot_controlhost.utils.global.RoomSetting;
import com.example.iot_controlhost.utils.global.SpinnerList;

/**
 * @Description 旧版本配置导入
 * @Author DuanKaiji
 * @CreateTime 2023年11月21日 11:36:04
 */
public class OldSet {
    /**
     * 第一次旧版本到新版本转换
     */
    public static final String FIRST_TIME = "FIRST_TIME";

    /**
     * 地暖地址
     */
    public static String[] ADDRESS = {"不启用", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F",
            "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89"};

    /**
     * 新老配置兼容
     */
    @SuppressLint("Range")
    public static void importOldSet(Context context) {
        DataBaseUtil dataBaseUtil = new DataBaseUtil(context);
        Cursor cursor = dataBaseUtil.Select("select * from areas where flag = 1");
        String roomId = null;
        if (cursor.moveToNext()) {
            //有配置，则说明此机器注册过
            HardwareSetting.setRegistered();
            int type = cursor.getInt(cursor.getColumnIndex("AreaType"));
            if (0 != type || 1 != type) {
                //分辨
//                continue;
            }
            //基本信息
            roomId = cursor.getString(cursor.getColumnIndex("RoomId"));
            MMKVUtil.put(RoomSetting.ROOM_NAME, cursor.getString(cursor.getColumnIndex("RoomName")));
            MMKVUtil.put(RoomSetting.MODE, oldModeToNewMode(cursor.getInt(cursor.getColumnIndex("YXMS"))));
            //自动模式配置项
            MMKVUtil.put(AutoModelSet.AUTO_TEMPERATURE_HEAT, SpinnerList.TEMPERATURE_HEAT[cursor.getInt(cursor.getColumnIndex("HeatUpType"))]);//默认自动模式均改为“分档位控制”
            MMKVUtil.put(AutoModelSet.AUTO_TEMPERATURE_MODE, SpinnerList.TEMPERATURE_MODE[1]);
            MMKVUtil.put(AutoModelSet.TARGET_HUMIDITY, cursor.getDouble(cursor.getColumnIndex("TargetHumidity")));
            MMKVUtil.put(AutoModelSet.TARGET_TEMPERATURE, cursor.getDouble(cursor.getColumnIndex("TargetTemperature")));
            MMKVUtil.put(AutoModelSet.CYCLE_START, cursor.getInt(cursor.getColumnIndex("ventilator_Open_interval")));
            MMKVUtil.put(AutoModelSet.CYCLE_STOP, cursor.getInt(cursor.getColumnIndex("ventilator_Close_interval")));
            MMKVUtil.put(AutoModelSet.AUTO_TEMPERATURE, cursor.getInt(cursor.getColumnIndex("Temperature_mode")) == 1);
            MMKVUtil.put(AutoModelSet.AUTO_HUMIDITY, cursor.getInt(cursor.getColumnIndex("Humidity_mode")) == 1);
            MMKVUtil.put(AutoModelSet.AUTO_VENTILATOR, cursor.getInt(cursor.getColumnIndex("ventilator_mode")) == 1);
            MMKVUtil.put(AutoModelSet.MAX_TEMPERATURE, AutoModelSet.getTargetTemperature() + cursor.getDouble(cursor.getColumnIndex("Temp_Warning")));
            MMKVUtil.put(AutoModelSet.MIN_TEMPERATURE, AutoModelSet.getTargetTemperature() - cursor.getDouble(cursor.getColumnIndex("Temp_Warning")));
            MMKVUtil.put(AutoModelSet.MAX_HUMIDITY, AutoModelSet.getTargetHumidity() + cursor.getInt(cursor.getColumnIndex("Hum_Warning")));
            MMKVUtil.put(AutoModelSet.MIN_HUMIDITY, AutoModelSet.getTargetHumidity() - cursor.getInt(cursor.getColumnIndex("Hum_Warning")));
            MMKVUtil.put(AutoModelSet.MAX_DENSITY_CO2, cursor.getInt(cursor.getColumnIndex("CO2MAX")));
            //传感器
            int temp = cursor.getInt(cursor.getColumnIndex("wsd1Type"));
            if (temp != 0) {
                THSensor thSensor1 = new THSensor("室内温湿度传感器1");
                thSensor1.setAddress(ADDRESS[temp]);
                thSensor1.setEnable(true);
                IndoorTHSensorUtil.insert(thSensor1);
            }
            temp = cursor.getInt(cursor.getColumnIndex("wsd2Type"));
            if (temp != 0) {
                THSensor thSensor2 = new THSensor("室内温湿度传感器2");
                thSensor2.setAddress(ADDRESS[temp]);
                thSensor2.setEnable(true);
                IndoorTHSensorUtil.insert(thSensor2);
            }
            temp = cursor.getInt(cursor.getColumnIndex("nhType"));
            if (temp != 0) {
                RoomSensor.consumptionSensor.setEnable(true);
                RoomSensor.consumptionSensor.setAddress(ADDRESS[temp]);
            }
            temp = cursor.getInt(cursor.getColumnIndex("CO2Type"));
            if (temp != 0) {
                GasSensor gasSensor = new GasSensor("室内CO2传感器1");
                gasSensor.setAddress(ADDRESS[temp]);
                gasSensor.setEnable(true);
                IndoorCO2SensorUtil.insert(gasSensor);
            }
            temp = cursor.getInt(cursor.getColumnIndex("DMWDType"));
            if (temp != 0) {
                FloorTSensor floorTSensor = new FloorTSensor("地面温度传感器");
                floorTSensor.setAddress(ADDRESS[temp]);
                floorTSensor.setEnable(true);
                IndoorFloorTSensorUtil.insert(floorTSensor);
            }
            temp = cursor.getInt(cursor.getColumnIndex("GZType"));
            if (temp != 0) {
                RoomSensor.lightSensor.setEnable(true);
                RoomSensor.lightSensor.setAddress(ADDRESS[temp]);
            }
            temp = cursor.getInt(cursor.getColumnIndex("SWWSDType"));
            if (temp != 0) {
                RoomSensor.outdoorTHSensor.setEnable(true);
                RoomSensor.outdoorTHSensor.setAddress(ADDRESS[temp]);
            }
            //空调
            temp = cursor.getInt(cursor.getColumnIndex("HWYKType"));
            if (temp != 0) {
                RoomController.airConditionInfrared.setEnable(true);
                RoomController.airConditionInfrared.setAddress(ADDRESS[temp]);
            }
            //地暖
            temp = cursor.getInt(cursor.getColumnIndex("HeatUpType_Arr"));
            if (temp != 0) {
                RoomController.floor.setEnable(true);
                RoomController.floor.setAddress(ADDRESS[temp]);
                RoomController.floor.setModel(RoomController.floor.getModels()[cursor.getInt(cursor.getColumnIndex("HeatUpModel"))]);
                Floor.importDefaultPWMToFloor();
            }
            int maxGear = cursor.getInt(cursor.getColumnIndex("HeatingMaxGear"));
            RoomController.floor.setMaxGear(maxGear);
            //继电器
            temp = cursor.getInt(cursor.getColumnIndex("JDQType"));
            if (temp != 0) {
                RoomController.relay.setEnable(true);
                RoomController.relay.setAddress(ADDRESS[temp]);
                RoomController.relay.setModel(RoomController.relay.getModels()[cursor.getInt(cursor.getColumnIndex("DeviceModel"))]);
                int[] ports = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                ports[0] = cursor.getInt(cursor.getColumnIndex("Port_1"));
                ports[1] = cursor.getInt(cursor.getColumnIndex("Port_2"));
                ports[2] = cursor.getInt(cursor.getColumnIndex("Port_3"));
                ports[3] = cursor.getInt(cursor.getColumnIndex("Port_4"));
                ports[4] = cursor.getInt(cursor.getColumnIndex("Port_5"));
                ports[5] = cursor.getInt(cursor.getColumnIndex("Port_6"));
                ports[6] = cursor.getInt(cursor.getColumnIndex("Port_7"));
                ports[7] = cursor.getInt(cursor.getColumnIndex("Port_8"));
                //0不启用，1地暖，2空调，3加湿器，4高进换气扇，5低进换气扇，6红光灯，7白光灯，8匀风扇，9紫外灯
                for (int i = 0; i < 8; i++) {
                    if (ports[i] == 1) {
                        //之前为地暖，为古老版本，现以不需要
                    } else if (ports[i] == 2) {
                        RoomController.airCondition.clearAndSetPort(i + 1);
                    } else if (ports[i] == 3) {
                        RoomController.humidifier.clearAndSetPort(i + 1);
                    } else if (ports[i] == 4) {
                        RoomController.intakeFan.clearAndSetPort(i + 1);
                    } else if (ports[i] == 5) {
                        RoomController.exhaustFan.clearAndSetPort(i + 1);
                    } else if (ports[i] == 6) {
                        RoomController.redLight.clearAndSetPort(i + 1);
                    } else if (ports[i] == 7) {
                        RoomController.whiteLight.clearAndSetPort(i + 1);
                    } else if (ports[i] == 8) {
                        RoomController.evenFan.clearAndSetPort(i + 1);
                    } else if (ports[i] == 9) {
                        RoomController.uvLight.clearAndSetPort(i + 1);
                    }
                }
            }
        }
        //控制器配置
        cursor = dataBaseUtil.Select("select * from DevicesSetting");
        Device.setControllerModel(Controller.CONTROLLER_MODEL[0]);
        HardwareSetting.setWorkMode(HardwareSetting.CONTROLLER_WORK_MODE[0]);
        if (cursor.moveToNext()) {
            HardwareSetting.setSensorSerialCom(SpinnerList.SERIAL_ADDRESS[cursor.getInt(cursor.getColumnIndex("SensorCOMName"))]);
            HardwareSetting.setControllerSerialCom(SpinnerList.SERIAL_ADDRESS[cursor.getInt(cursor.getColumnIndex("ControlCOMName"))]);
        }
        //消毒计划配置
        UVTaskUtil.removeAllUvTask();
        cursor = dataBaseUtil.Select("select * from ScheduledTasks where areacode='" + roomId + "' and flag=1 order by id asc");
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("TaskNum"));
            MyTask myTask = null;
            for (MyTask temp : UVTaskUtil.getUvTask()) {
                if (temp.getId() == id) {
                    myTask = temp;
                    break;
                }
            }
            if (myTask == null) {
                myTask = new MyTask();
                myTask.setId(id);
            }
            if (cursor.getInt(cursor.getColumnIndex("Execute")) == 1) {
                myTask.setStartTimeHour(cursor.getInt(cursor.getColumnIndex("HOUR")));
                myTask.setStartTimeMinute(cursor.getInt(cursor.getColumnIndex("MINUTE")));
                UVTaskUtil.addUvTask(myTask);
            } else {
                myTask.setEndTimeHour(cursor.getInt(cursor.getColumnIndex("HOUR")));
                myTask.setEndTimeMinute(cursor.getInt(cursor.getColumnIndex("MINUTE")));
                UVTaskUtil.updateUvTask(myTask);
            }
        }
        cursor.close();
    }

    /**
     * 是否为第一次旧系统到新系统转换
     */
    public static boolean isFirstTime() {
        boolean result = MMKVUtil.get(FIRST_TIME, true);
        if (result) {
            MMKVUtil.put(FIRST_TIME, false);
        }
        return result;
    }
    /**
     * 旧系统模式转换为新系统模式
     * 1 智能模式，2自动模式，3手动模式
     * 转为  0 手动模式，1自动模式，2智能模式
     */
    public static int oldModeToNewMode(int oldMode) {
        switch (oldMode) {
            case 1:
                return 2;
            case 2:
                return 1;
            case 3:
                return 0;
        }
        return -1;
    }

    /**
     * 新系统模式转换为旧系统模式
     * 0 手动模式，1自动模式，2智能模式
     * 转为   1 智能模式，2自动模式，3手动模式
     */
    public static int newModeToOldMode(int newMode) {
        switch (newMode) {
            case 0:
                return 3;
            case 1:
                return 2;
            case 2:
                return 1;
        }
        return -1;
    }
}
