package com.example.chemical.ui.plan;

import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.chemical.ChemicalApp;
import com.example.chemical.ui.plan.locker.LockerAdapter;
import com.example.chemical.ui.plan.locker.LockerFragment;
import com.example.chemical.ui.plan.room.RoomTool;
import com.example.chemical.ui.plan.room.bean.input_add.InventoryItemBean;
import com.example.chemical.ui.plan.room.bean.locker.CabinetLayerSelect;
import com.example.chemical.ui.plan.room.bean.locker.HxpCabinetDoorVo;
import com.example.chemical.ui.plan.room.bean.locker.HxpCabinetVo;
import com.example.chemical.ui.plan.room.bean.locker.HxpLabCabinetListVo;
import com.example.chemical.ui.plan.room.bean.locker.HxpLabCabinetVo;
import com.example.chemical.ui.plan.room.bean.locker.HxpLabDirector;
import com.example.chemical.ui.plan.room.bean.locker.HxpLabSafe;
import com.rc.httpcore.bean.ChemicalInfoBean;
import com.rc.httpcore.client.ApiRepository;
import com.rc.httpcore.client.HttpTool;
import com.rc.httpcore.exception.NetException;

import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.Response;

public class PlanAddActivityHelp {

    private PlanAddActivity activity;

    public PlanAddActivityHelp(PlanAddActivity planAddActivity) {
        activity = planAddActivity;
    }

    public void getLabInfo() {
        activity.showLoading("加载中...", false);
        // 实验室基本信息
        activity.addDisposable(ApiRepository.INSTANCE.getRelList(ChemicalApp.subjectId).subscribe(new Consumer<ChemicalInfoBean>() {
            @Override
            public void accept(ChemicalInfoBean chemicalInfoBean) throws Exception {
                activity.chemicalInfo = chemicalInfoBean;
                getCabinetList(chemicalInfoBean);
            }
        }, throwable -> {
            LogUtils.e(Log.getStackTraceString(throwable));
            activity.dismissLoading();
            ToastUtils.showLong("实验室基本信息获取失败,请重试!");
            activity.finish();
        }));
    }

    private void getCabinetList(ChemicalInfoBean chemicalInfoBean) {
        // 获取实验室柜子信息
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<List<HxpCabinetVo>>() {
            @Override
            public List<HxpCabinetVo> doInBackground() throws Throwable {
                Response response = HttpTool.getCabinetList(chemicalInfoBean.getSubId());
                if (response.isSuccessful()) {
                    String json = response.body().string();
                    HxpLabCabinetListVo hxpCabinetListVo = GsonUtils.fromJson(json, HxpLabCabinetListVo.class);
                    if (hxpCabinetListVo.getCode() == 200) {
                        HxpLabCabinetVo hxpLabCabinetVo = hxpCabinetListVo.getData();
                        try {
                            RoomTool.getInstance().hxpLabDirectorDAO().insertAll(hxpLabCabinetVo.getAdminList());
                            RoomTool.getInstance().hxpLabSafeDAO().insertAll(hxpLabCabinetVo.getSafeList());
                        } catch (Exception e) {
                            LogUtils.e(Log.getStackTraceString(e));
                        }
                        return hxpLabCabinetVo.getCabinetList();
                    } else {
                        throw new NetException("" + hxpCabinetListVo.getCode(), hxpCabinetListVo.getMessage());
                    }
                } else {
                    throw new NetException("" + response.code(), response.message());
                }
            }

            @Override
            public void onSuccess(List<HxpCabinetVo> hxpCabinetVoList) {
                activity.dismissLoading();
                if (null != hxpCabinetVoList && !hxpCabinetVoList.isEmpty()) {
                    activity.cabinetVoList = hxpCabinetVoList;
                    activity.hxpCabinetDAO.insertAll(activity.cabinetVoList);
                    // 录入柜子
                    for (int i = 0; i < activity.cabinetVoList.size(); i++) {
                        HxpCabinetVo hxpCabinetVo = activity.cabinetVoList.get(i);
                        List<HxpCabinetDoorVo> hxpCabinetDoorVoList = hxpCabinetVo.getCabinetDoorVoList();
                        if (null != hxpCabinetDoorVoList && !hxpCabinetDoorVoList.isEmpty()) {
                            // 录入柜门
                            activity.hxpDoorDAO.insertAll(hxpCabinetDoorVoList);
                        }
                    }

                    // 柜子初始化
                    for (int i = 0; i < activity.cabinetVoList.size(); i++) {
                        HxpCabinetVo hxpCabinetVo = activity.cabinetVoList.get(i);
                        activity.cabinetFragmentList.add(new LockerFragment(hxpCabinetVo.getCabinetId(), activity.fragmentCallBack));
                    }

                    activity.lockerAdapter = new LockerAdapter(activity, activity.cabinetFragmentList);
                    activity.binding.lockerVP2.setAdapter(activity.lockerAdapter);
                    activity.binding.lockerVP2.setOffscreenPageLimit(1);

                    updateInventoryUi();
                } else {
                    activity.showToast("未找到化学品柜!");
                }
            }

            @Override
            public void onFail(Throwable t) {
                super.onFail(t);
                ToastUtils.showLong("化学品柜获取失败,请重试!");
                activity.dismissLoading();
                activity.finish();
            }
        });
    }

    /**
     * 选择柜层
     */
    public void selectCabinetLayer(CabinetLayerSelect lastSelectInfo) {
        // 选择柜层数据库更新
        activity.hxpLayerDAO.insert(lastSelectInfo);
        activity.hxpLayerDAO.change(lastSelectInfo.getCabinetId(), lastSelectInfo.getDoorId(), lastSelectInfo.getLayer());
        // 更新待存储列表
        updateInventoryUi();
    }


    /**
     * 更新待存储列表
     */
    public void updateInventoryUi() {
        // 待存储列表更新
        CabinetLayerSelect cabinetLayerSelect = activity.hxpLayerDAO.getChanged();
        if (null != cabinetLayerSelect) {
            activity.inventoryItemBeanList.clear();
            List<InventoryItemBean> inventoryItemBeanList = activity.hxpInventoryDAO.getById(cabinetLayerSelect.getCabinetId(), cabinetLayerSelect.getDoorId(), cabinetLayerSelect.getLayer());
            // 展示待存储UI
            activity.binding.inputRL.setVisibility(View.VISIBLE);
            if (null != inventoryItemBeanList && !inventoryItemBeanList.isEmpty()) {
                activity.binding.addIndexTV.setVisibility(View.GONE);
                activity.binding.scanLL.setVisibility(View.GONE);
                activity.binding.planLV.setVisibility(View.VISIBLE);
                activity.inventoryItemBeanList.addAll(inventoryItemBeanList);
                activity.planAdapter.notifyDataSetChanged();
            } else {
                activity.binding.scanLL.setVisibility(View.VISIBLE);
                activity.binding.planLV.setVisibility(View.GONE);
            }
        }
    }
}
