package com.example.iot_controlhost.ui.fragment;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.example.iot_controlhost.base.BasePresenter;
import com.example.iot_controlhost.model.SingleLiveEvent;
import com.example.iot_controlhost.utils.control.disinfect.DisinfectController;
import com.example.iot_controlhost.utils.control.even.EvenController;
import com.example.iot_controlhost.utils.control.humidity.HumidityController;
import com.example.iot_controlhost.utils.control.temperature.TemperatureController;
import com.example.iot_controlhost.utils.control.ventilator.VentilatorController;
import com.example.iot_controlhost.utils.global.AutoModelSet;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.global.RoomSensor;
import com.example.iot_controlhost.utils.global.RxTag;
import com.example.iot_controlhost.utils.log.MyLog;
import com.example.iot_controlhost.utils.network.TopicClass;
import com.xuexiang.rxutil2.rxbus.RxBusUtils;
import com.xuexiang.rxutil2.rxjava.DisposablePool;
import com.xuexiang.rxutil2.rxjava.RxJavaUtils;
import com.xuexiang.rxutil2.rxjava.task.RxIOTask;

public class AutoFragmentPresenter extends BasePresenter {

    public MutableLiveData<String> floorT = new MutableLiveData<>("");
    public MutableLiveData<String> targetTemperature = new MutableLiveData<>();
    public MutableLiveData<String> targetHumidity = new MutableLiveData<>();
    public MutableLiveData<String> targetStart = new MutableLiveData<>();
    public MutableLiveData<String> targetStop = new MutableLiveData<>();
    public MutableLiveData<Integer> setAutoParamLiveData = new SingleLiveEvent<>();

    public AutoFragmentPresenter(@NonNull Application application) {
        super(application);
        //接收消息
        RxJavaUtils.doInIOThread(new RxIOTask<Object>(null) {
            @Override
            public Void doInIOThread(Object o) {
                //更新自动控制相关参数显示
                RxBusUtils.get().onMainThread(RxTag.UPDATE_AUTO, Integer.class, i -> {
                    switch (i) {
                        case 2 -> updateUI();
                        case 3 -> {
                            startAutoControl();
                            updateParamUI();
                            updateUI();
                        }
                    }
                });
                return null;
            }
        });
        pollingTask.startPollingTaskOnIOThread(RxTag.AUTO_MODE_UI_REFRESH, 5, ()->{
            floorT.postValue(RoomSensor.getFloorTemperature() + "℃");
            if (RoomController.relay.getModel().equals(RoomController.relay.getModels()[2])) {
                setAutoParamLiveData.postValue(6);
            }
        });
    }

    /**
     * 更新UI
     */
    public void updateUI() {
        setAutoParamLiveData.setValue(6);
    }

    /**
     * 更新自动模式
     */
    public void startAutoControl() {
        if (AutoModelSet.isAutoTemperature()) {
            TemperatureController.getInstance().start();
        } else {
            TemperatureController.getInstance().stop();
        }
        if (AutoModelSet.isAutoHumidity()) {
            HumidityController.getInstance().start();
        } else {
            HumidityController.getInstance().stop();
        }
        if (AutoModelSet.isAutoVentilator()) {
            VentilatorController.getInstance().start();
        } else {
            VentilatorController.getInstance().stop();
        }
        if (AutoModelSet.isAutoDisinfect()) {
            DisinfectController.getInstance().start();
        } else {
            DisinfectController.getInstance().stop();
        }
        if (AutoModelSet.isAutoEven()) {
            EvenController.getInstance().start();
        } else {
            EvenController.getInstance().stop();
        }
    }

    /**
     * 更新自动模式参数显示
     */
    public void updateParamUI() {
        targetTemperature.setValue(String.valueOf(AutoModelSet.getTargetTemperature()));
        targetHumidity.setValue(String.valueOf(AutoModelSet.getTargetHumidity()));
        targetStart.setValue(String.valueOf(AutoModelSet.getCycleStart()));
        targetStop.setValue(String.valueOf(AutoModelSet.getCycleStop()));
    }

    public MutableLiveData<Integer> getSetAutoParamLiveData() {
        return setAutoParamLiveData;
    }

    /**
     * 停止自动控制
     */
    public void stopAutoControl() {
        TemperatureController.getInstance().stop();
        HumidityController.getInstance().stop();
        VentilatorController.getInstance().stop();
        DisinfectController.getInstance().stop();
        EvenController.getInstance().stop();
    }

    public void setAutoParam(int setType) {
        setAutoParamLiveData.setValue(setType);
    }

}