package com.example.iot_controlhost.utils.control;


import android.provider.Settings;

import com.example.iot_controlhost.model.controller.AirConditionInfrared;
import com.example.iot_controlhost.utils.MMKVUtil;
import com.example.iot_controlhost.utils.MyUtil;
import com.example.iot_controlhost.utils.control.infrared.MyInfraredUtils;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.global.RoomSetting;
import com.example.iot_controlhost.utils.global.RxTag;
import com.example.iot_controlhost.utils.log.MyLog;
import com.example.iot_controlhost.utils.log.UserLog;
import com.example.iot_controlhost.utils.old.OldSet;
import com.xuexiang.rxutil2.rxbus.RxBusUtils;

/**
 * 远程控制类
 */
public class RemoteControl {

    /**
     * 远程控制设备
     *
     * @param controlCommand 控制指令json
     * @return 操作成功与否
     */
    public static boolean remoteControl(int controlCommand, int gear, int value) {
        try {
            AirConditionInfrared air = MyInfraredUtils.getInstances();
            switch (controlCommand) {
                case 0:
                    //开始升温
                    return air.setAirCondition(0, 30)
                            && RoomController.floor.setGear(gear);
                case 1:
                    //停止升温
                    boolean res = true;
                    if (RoomController.floor.isPowerSupply()) {
                        res = RoomController.floor.setPowerSupply(false);
                    }
                    if (air.isPowerSupply() && air.getMode() == AirConditionInfrared.HEAT) {
                        res = air.setPowerSupply(false) && res;
                    }
                    return res;
                case 3:
                    // 3停止降温
                    boolean result = true;
                    if (air.isPowerSupply() && air.getMode() == AirConditionInfrared.COLD) {
                        result = air.setPowerSupply(false);
                    }
                    return result;
                case 2:
                    //开始降温
                    return air.setAirCondition(2, 17)
                            && RoomController.floor.setPowerSupply(false);
                case 4:
                    //开始加湿
                    if (RoomController.airExchange.isAvailable()) {
                        RoomController.airExchange.setPowerSupply(true);
                    }
                    return RoomController.humidifier.setPowerSupply(true);
                case 5:
                    //停止加湿
                    if (RoomController.airExchange.isAvailable()) {
                        RoomController.airExchange.setPowerSupply(false);
                    }
                    return RoomController.humidifier.setPowerSupply(false);
                case 6:
                    //开始换气
                    return RoomController.fan.setPowerSupply(true);
                case 7:
                    //停止换气
                    return RoomController.fan.setPowerSupply(false);
                case 8:
                    //切换模式
                    int newMode = OldSet.oldModeToNewMode(value);
                    if (RoomSetting.getMode() != newMode) {
                        int targetSignal = newMode == 0 ? 15 : newMode == 1 ? 13 : 16;
                        RxBusUtils.get().post(RxTag.UPDATE_MAIN, targetSignal);
                        MyLog.remote("远程设置" + MyUtil.getModeName(newMode) + "模式");
                    }
                    return true;
                case 9:
                    //开始匀风
                    return RoomController.evenFan.setPowerSupply(true);
                case 10:
                    //停止匀风
                    return RoomController.evenFan.setPowerSupply(false);
                case 11:
                    //开启灯光
                    boolean l11 = RoomController.whiteLight.setPowerSupply(true);
                    boolean l12 = RoomController.redLight.setPowerSupply(true);
                    boolean l13 = RoomController.uvLight.setPowerSupply(true);
                    return l11 || l12 || l13;
                case 12:
                    //关闭灯光
                    boolean l21 = RoomController.whiteLight.setPowerSupply(false);
                    boolean l22 = RoomController.redLight.setPowerSupply(false);
                    boolean l23 = RoomController.uvLight.setPowerSupply(false);
                    return l21 || l22 || l23;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //均不符合条件
        return false;
    }

}
