package com.example.iot_controlhost.ui.activity;

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.net.CommonResponse;
import com.example.iot_controlhost.model.sensor.GasSensor;
import com.example.iot_controlhost.model.sensor.Sensor;
import com.example.iot_controlhost.model.sensor.SensorInfo;
import com.example.iot_controlhost.utils.database.EnergyDBManager;
import com.example.iot_controlhost.utils.database.SilkwormDBManager;
import com.example.iot_controlhost.utils.global.RoomSensor;
import com.example.iot_controlhost.utils.global.RoomSetting;
import com.example.iot_controlhost.utils.global.RxTag;
import com.example.iot_controlhost.utils.global.Variable;
import com.example.iot_controlhost.utils.log.MyLog;
import com.example.iot_controlhost.utils.network.Url;
import com.example.iot_controlhost.utils.network.XHttpManager;
import com.example.iot_controlhost.utils.network.XHttpShorthand;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 信息页面业务逻辑
 * @Author DuanKaiji
 * @CreateTime 2024年01月15日 15:14
 */
public class InfoActivityPresenter extends BasePresenter {

    private MutableLiveData<Integer> noticeLiveData = new MutableLiveData<>();
    public MutableLiveData<Double> todayConsumption = new MutableLiveData<>();
    public MutableLiveData<Double> seasonConsumption = new MutableLiveData<>();
    public MutableLiveData<Double> allConsumption = new MutableLiveData<>();
    public MutableLiveData<Double> indoorTemperature = new MutableLiveData<>();
    public MutableLiveData<Double> indoorHumidity = new MutableLiveData<>();
    public MutableLiveData<String> qualityAir = new MutableLiveData<>("-");
    public MutableLiveData<String> qualityAirOut = new MutableLiveData<>("-");
    public MutableLiveData<Double> outdoorHumidity = new MutableLiveData<>();
    public MutableLiveData<Double> outdoorTemperature = new MutableLiveData<>();
    public MutableLiveData<List<SensorInfo>> sensorInfoList = new MutableLiveData<>();

    public MutableLiveData<List<SensorInfo>> getSensorInfoList() {
        return sensorInfoList;
    }

    public InfoActivityPresenter(@NonNull Application application) {
        super(application);
        // 读取服务到期时间
//        loadServerExpirationDate();
        // 初始化温湿度数据
        pollingTask.startPollingTaskOnIOThread(RxTag.INFO_GET_SENSOR_DATA, Variable.SENSOR_REFRESH_TIME, () -> {
            todayConsumption.postValue(RoomSensor.consumptionSensor.getTodayEnergyConsumption());
            seasonConsumption.postValue(RoomSensor.consumptionSensor.getSeasonEnergyConsumption());
            allConsumption.postValue(EnergyDBManager.queryMaxDate());
            indoorTemperature.postValue(RoomSensor.getAverageIndoorTemperature());
            indoorHumidity.postValue(RoomSensor.getAverageIndoorHumidity());
            qualityAir.postValue(GasSensor.getQualityAir());
            qualityAirOut.postValue(GasSensor.getQualityAir());
            outdoorTemperature.postValue(RoomSensor.outdoorTHSensor.temperature);
            outdoorHumidity.postValue(RoomSensor.outdoorTHSensor.humidity);
            List<SensorInfo> infoList = new ArrayList<>();
            // 所有温湿度传感器的数值
            for (Sensor sensor : RoomSensor.getAllAvailableSensors()) {
                infoList.add(new SensorInfo(sensor.getName(), sensor.toString()));
            }
            sensorInfoList.postValue(infoList);
        });
    }

    public MutableLiveData<Integer> getNoticeLiveData() {
        return noticeLiveData;
    }

    public void notice(int type) {
        noticeLiveData.setValue(type);
    }


    /**
     * 读取服务到期时间
     */
    private void loadServerExpirationDate() {
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.serverInfo);
        params.addParameter("areaCode", RoomSetting.getAreaCode());
        x.http().get(params, new XHttpShorthand<CommonResponse>() {
            @Override
            public void success(CommonResponse model) {
            }

            @Override
            public void error(Throwable e) {
                MyLog.appError("服务信息获取失败");
            }
        });
    }

}
