package xn.hxp.ui.main;

import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ThreadUtils;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;

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

import xn.hxp.databinding.IncludeMainLBinding;
import xn.hxp.http.HttpData;
import xn.hxp.http.api.GetCabinetListApi;
import xn.hxp.room.bean.LabInfo;
import xn.hxp.room.bean.cabinet.Cabinet;
import xn.hxp.room.bean.cabinet.CabinetDoor;
import xn.hxp.room.bean.cabinet.CabinetDoorAdmin;
import xn.hxp.room.bean.cabinet.CabinetDoorLock;
import xn.hxp.room.tool.LabInfoTool;
import xn.hxp.room.tool.cabinet.CabinetDoorAdminTool;
import xn.hxp.room.tool.cabinet.CabinetDoorLockTool;
import xn.hxp.room.tool.cabinet.CabinetDoorTool;
import xn.hxp.room.tool.cabinet.CabinetTool;
import xn.hxp.ui.main.adapter.CabinetAdapter;

public class MainActivityLHelper {
    private final MainActivity mainActivity;
    private final IncludeMainLBinding bindingL;
    private final LabInfo labInfo;
    private final List<Cabinet> cabinetList = new ArrayList<>();

    public MainActivityLHelper(MainActivity mainActivity, IncludeMainLBinding includeMainLBinding) {
        this.mainActivity = mainActivity;
        this.bindingL = includeMainLBinding;
        labInfo = LabInfoTool.INSTANCE.getLabInfo();
        bindingL.cabinetVP.registerLifecycleObserver(mainActivity.getLifecycle()).setAdapter(new CabinetAdapter()).create(cabinetList);
        updateCabinet();

        bindingL.last.setOnClickListener(v -> bindingL.cabinetVP.previousPage());
        bindingL.next.setOnClickListener(v -> bindingL.cabinetVP.nextPage());
    }

    /**
     * 更新柜子
     */
    private void updateCabinet() {
        mainActivity.showLoading("正在查询实验室柜信息...");
        EasyHttp.get(mainActivity).api(new GetCabinetListApi(labInfo.getSubjectId())).request(new OnHttpListener<HttpData<List<Cabinet>>>() {
            @Override
            public void onHttpSuccess(@NonNull HttpData<List<Cabinet>> result) {
                if (result.isRequestSuccess()) {
                    List<Cabinet> postCabinetList = result.getData();
                    if (null != postCabinetList) {
                        cabinetList.clear();
                        cabinetList.addAll(postCabinetList);
                        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
                            @Override
                            public Object doInBackground() throws Throwable {
                                // 化学品柜入库
                                CabinetTool.INSTANCE.insert(cabinetList);
                                for (int i = 0; i < cabinetList.size(); i++) {
                                    Cabinet cabinet = cabinetList.get(i);
                                    List<CabinetDoor> cabinetDoorList = cabinet.getCabinetDoorVoList();
                                    if (null != cabinetDoorList) {
                                        // 化学品柜门入库
                                        CabinetDoorTool.INSTANCE.insert(cabinetDoorList);
                                        for (int j = 0; j < cabinetDoorList.size(); j++) {
                                            CabinetDoor cabinetDoor = cabinetDoorList.get(j);
                                            // 化学品柜门管理员入库
                                            List<CabinetDoorAdmin> cabinetAdminVoList = cabinetDoor.getCabinetAdminVoList();
                                            if (cabinetAdminVoList != null) {
                                                CabinetDoorAdminTool.INSTANCE.insert(cabinetAdminVoList);
                                            }
                                            // 化学品柜锁入库
                                            List<CabinetDoorLock> cabinetDoorLockList = cabinetDoor.getCabinetLockVoList();
                                            if (cabinetDoorLockList != null) {
                                                CabinetDoorLockTool.INSTANCE.insert(cabinetDoorLockList);
                                            }
                                        }
                                    }
                                }
                                return null;
                            }

                            @Override
                            public void onSuccess(Object result) {
                                if (cabinetList.size() > 1) {
                                    bindingL.last.setVisibility(View.VISIBLE);
                                    bindingL.next.setVisibility(View.VISIBLE);
                                } else {
                                    bindingL.last.setVisibility(View.GONE);
                                    bindingL.next.setVisibility(View.GONE);
                                }
                                bindingL.cabinetVP.refreshData(cabinetList);
                                mainActivity.dismissLoading();
                            }

                            @Override
                            public void onFail(Throwable t) {
                                super.onFail(t);
                                mainActivity.dismissLoading();
                            }
                        });
                    }
                } else {
                    mainActivity.dismissLoading();
                }
            }

            @Override
            public void onHttpFail(@NonNull Throwable throwable) {
                mainActivity.dismissLoading();
                mainActivity.showErr(throwable.getMessage());
            }
        });
    }
}
