package com.example.iot_controlhost.ui.dialog;

import android.content.Context;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.iot_controlhost.adapter.SensorInfoAdapter;
import com.example.iot_controlhost.base.BaseDialog;
import com.example.iot_controlhost.databinding.DialogEnergyBinding;
import com.example.iot_controlhost.databinding.ItemSensorInfoBinding;
import com.example.iot_controlhost.model.controller.relay.Relay;
import com.example.iot_controlhost.model.sensor.SensorInfo;
import com.example.iot_controlhost.utils.PollingTask;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.global.RxTag;
import com.xuexiang.rxutil2.rxjava.DisposablePool;
import com.xuexiang.rxutil2.rxjava.RxJavaUtils;

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

/**
 * BBIT能耗详情弹窗
 */
public class EnergyDialog extends BaseDialog<DialogEnergyBinding> {
    SensorInfoAdapter adapter;
    PollingTask pollingTask;

    public EnergyDialog(Context mContext) {
        super(mContext);
        pollingTask = PollingTask.getInstance("EnergyDialog");
        adapter = new SensorInfoAdapter(null, ItemSensorInfoBinding.class);
        //初始化界面控件
        binding.rvEnergy.setLayoutManager(new LinearLayoutManager(mContext));
        binding.rvEnergy.setAdapter(adapter);
        // 初始化温湿度数据
        pollingTask.startPollingTaskOnIOThread(RxTag.GET_RELAY_SENSOR_DATA,5,()->{
            List<SensorInfo> infoList = new ArrayList<>();
            for (Relay relay : RoomController.getAllRelayDevice()) {
                infoList.add(new SensorInfo(relay.getName(), relay.getSensorData()));
            }
            adapter.setDataList(infoList);
        });
    }

    @Override
    public void dismiss() {
        super.dismiss();
        pollingTask.stopAllPollingTasks();
    }
}
