package xn.xxp.main.things;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

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

import core.ui.activity.BaseActivity;
import core.ui.activity.BaseCountDownActivity;
import http.client.ApiRepository;
import http.vo.request.ControllerCMD;
import http.vo.response.LotDeviceVo;
import io.reactivex.rxjava3.functions.Consumer;
import xn.xxp.R;
import xn.xxp.databinding.ActivityThingsListBinding;
import xn.xxp.mqtt.event.ThingsEvent;
import xn.xxp.room.RoomTool;
import xn.xxp.room.bean.LabConfig;
import xn.xxp.widget.ITitleBar;
import xn.xxp.widget.NavViewCompat;

public class ThingsListActivity extends BaseCountDownActivity<ActivityThingsListBinding> {
    private ActivityThingsListBinding binding;
    private LabConfig labConfig;
    private List<LotDeviceVo> lotDeviceVoList = new ArrayList<>();
    private ThingsListAdapter thingsListAdapter;

    @Override
    protected ActivityThingsListBinding createViewBinding() {
        binding = ActivityThingsListBinding.inflate(getLayoutInflater());
        return binding;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        super.initViews(savedInstanceState);
        EventBus.getDefault().register(this);
        labConfig = RoomTool.getInstance().labConfigDao().getLabConfig();
        thingsListAdapter = new ThingsListAdapter(lotDeviceVoList);
        binding.iotThings.setAdapter(thingsListAdapter);
        binding.iotThings.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LotDeviceVo lotDeviceVo = lotDeviceVoList.get(position);
                if (!lotDeviceVo.isOnline()) {
                    showToast("当前设备不在线");
                    return;
                }
                showLoading("加载中...");
                addDisposable(ApiRepository.INSTANCE.sendControllerCMD(generateCMD(lotDeviceVo)).subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Throwable {
                        ThreadUtils.runOnUiThreadDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (!isDestroyed()) {
                                    queryControllerList();
                                }
                            }
                        }, 2000);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {
                        dismissLoading();
                        LogUtils.e(Log.getStackTraceString(throwable));
                        showNetError(throwable);
                    }
                }));
            }
        });
        queryControllerList();
    }

    private ControllerCMD generateCMD(LotDeviceVo lotDeviceVo) {
        ControllerCMD controllerCMD = new ControllerCMD();
        controllerCMD.id = lotDeviceVo.hardwareNo;
        controllerCMD.command = lotDeviceVo.isSwitchOn() ? "0" : "1";
        return controllerCMD;
    }

    private void queryControllerList() {
        showLoading("加载中...");
        addDisposable(ApiRepository.INSTANCE.controllerList(String.valueOf(labConfig.getLabId())).subscribe(new Consumer<List<LotDeviceVo>>() {
            @Override
            public void accept(List<LotDeviceVo> lotDeviceVos) throws Throwable {
                dismissLoading();
                if (null != lotDeviceVos) {
                    if (!isDestroyed()) {
                        lotDeviceVoList.clear();
                        lotDeviceVoList.addAll(lotDeviceVos);
                        thingsListAdapter.notifyDataSetChanged();
                    }
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Throwable {
                dismissLoading();
                LogUtils.e(Log.getStackTraceString(throwable));
                showNetError(throwable);
            }
        }));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onThingsEvent(ThingsEvent thingsEvent) {
        if (!isDestroyed()) {
            queryControllerList();
        }
    }

    @Override
    public ITitleBar getMTitleBar() {
        return binding.titleBar;
    }

    @Override
    public NavViewCompat getMNavView() {
        return binding.navView;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}