package com.unis.sxzcl.ui.work_module;

import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.unis.model.DataCallback;
import com.unis.model.DepotModel;
import com.unis.model.HouseModel;
import com.unis.model.WorkbenchModel;
import com.unis.model.bean.HouseListBean;
import com.unis.model.bean.MultiDepotBean;
import com.unis.model.bean.WorkModule;
import com.unis.sxzcl.R;
import com.unis.sxzcl.widget.BottomMultipleListDialog;
import com.unis.sxzcl.widget.MultipleBean;
import com.xyxsbj.mylibrary.tool.ToastTool;

import org.jetbrains.annotations.NotNull;

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

/**
 * 包名: com.unis.sxzcl.ui.work_module
 * 类名: WorkModuleAdapter
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/8 10:15
 * 更新人:
 * 更新时间: 2020/12/8 10:15
 * 更新说明:
 * 版本号: 1.0
 */
public class WorkModuleAdapter extends BaseQuickAdapter<WorkModule, BaseViewHolder> implements LoadMoreModule {

    private ClickCallback mCallback;
    private BottomMultipleListDialog mListDialog;
    private List<MultipleBean> mMultipleList = new ArrayList<>();
    private List<HouseListBean> mHouseList = new ArrayList<>();

    private int currentFragmentId;
    private WorkModule currentWork;


    public WorkModuleAdapter() {
        super(R.layout.item_work_module);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, WorkModule item) {
        holder.setText(R.id.tv_title, item.getTitle());

        Glide.with(getContext())
                .load(R.mipmap.ic_launcher)
                .placeholder(R.drawable.ic_img_loading)
                .error(R.drawable.no_banner)
                .into((ImageView) holder.getView(R.id.iv_show));

        holder.itemView.setTag(item);
        holder.itemView.setOnClickListener(v -> {
            currentWork = (WorkModule) v.getTag();
            startPage(currentWork);
        });
    }

    public void startPage(WorkModule item) {
        //页面跳转
        switch (item.getTitle()) {
            case WorkbenchModel.TYPE_JBXX:
                // 基本信息
                currentFragmentId = R.id.basicFragment;
                break;

            case WorkbenchModel.TYPE_SLZL:
                // 数量质量     跳转子模块
                currentFragmentId = R.id.workModuleFragment;
                break;
            case WorkbenchModel.TYPE_CFXX:
                // 仓房管理
//                currentFragmentId = R.id.houseHomeFragment;
                showHouseSelect(R.id.houseHomeFragment);
                break;
            case WorkbenchModel.TYPE_RKZL:
                // 入库质量
                break;
            case WorkbenchModel.TYPE_KCZL:
                // 库存质量
                break;
            case WorkbenchModel.TYPE_CKZL:
                // 出库质量
                break;

            case WorkbenchModel.TYPE_CLGL:
                // 储粮管理     跳转子模块
                currentFragmentId = R.id.workModuleFragment;
                break;
            case WorkbenchModel.TYPE_LQJC:
                // 粮情检查
//                currentFragmentId = R.id.grainCheckHomeFragment;
                showHouseSelect(R.id.grainCheckHomeFragment);
                break;
            case WorkbenchModel.TYPE_JXTF:
                // 机械通风
//                currentFragmentId = R.id.aerationListFragment;
                showHouseSelect(R.id.aerationListFragment);
                break;
            case WorkbenchModel.TYPE_XZGL:
                // 熏蒸管理
                break;

            case WorkbenchModel.TYPE_YJGL:
                // 药剂管理     跳转子模块
                currentFragmentId = R.id.workModuleFragment;
                break;
            case WorkbenchModel.TYPE_YJCG:
                // 药剂采购
                break;
            case WorkbenchModel.TYPE_YJBG:
                // 药剂保管
                break;
            case WorkbenchModel.TYPE_YJLY:
                // 药剂领用
                break;

            case WorkbenchModel.TYPE_SSSB:
                // 设施设备     跳转子模块
                currentFragmentId = R.id.workModuleFragment;
                break;
            case WorkbenchModel.TYPE_SSGL:
                // 设施管理
                currentFragmentId = R.id.templateTabFragment;
                break;
            case WorkbenchModel.TYPE_SBGL:
                // 设备管理
                break;

            case WorkbenchModel.TYPE_AQSC:
                // 安全生产
                break;

            case WorkbenchModel.TYPE_JDJC:
                // 监督检查
                break;

            case WorkbenchModel.TYPE_KWGL:
                // 库外管理     跳转子模块
                currentFragmentId = R.id.workModuleFragment;
                break;
            case WorkbenchModel.TYPE_ZCGL:
                // 租仓管理
                break;
            case WorkbenchModel.TYPE_WTGL:
                // 委托管理
                break;

//            case WorkbenchModel.TYPE_GZTJ:
//                // 工作统计
//                FactoryActivity.startPage(getContext(), WorkConditionFragment.class);
//                break;
//            case WorkbenchModel.TYPE_SPGL:
//                // 审批管理
//                FactoryActivity.startPage(getContext(), AuditHomeFragment.class);
//                break;
            default:
//                fragmentId = R.id.exampleFragment;
                break;
        }

        next();
    }

    /**
     * 选择仓房
     */
    private void showHouseSelect(int fragmentId) {
        if (mListDialog == null) {
            mListDialog = new BottomMultipleListDialog(getContext())
                    .hideTwoList()
                    .hideThreeList()
                    .hideRightEnter()
                    .setTitle("选择仓房")
            ;
        }
        mMultipleList.clear();
        mHouseList.clear();
        mListDialog.setListOne(mMultipleList)
                .setSelectOne(-1)
                .setOneCallback((v, b1) -> {
                    currentFragmentId = fragmentId;
                    //仓房点击事件
                    int oneSelect = mListDialog.getOneSelect();
                    if (oneSelect != -1)
                        HouseModel.get().setSelectHouse(mHouseList.get(oneSelect));
                    next();

                    return true;
                })
                .show();

        //获取数据
        MultiDepotBean depot = DepotModel.get().getSelectDepot();
        if (depot == null) {
            ToastTool.showLong("请先选择库");
            return;
        }
        HouseModel.get().getHouseList(depot.depotCode.toString(), new DataCallback<List<HouseListBean>>() {
            @Override
            public void onSuccess(List<HouseListBean> data) {
                try {
                    mHouseList.addAll(data);
                    for (HouseListBean bean : data) {
                        mMultipleList.add(new MultipleBean(bean.houseName));
                    }
                    mListDialog.setListOne(mMultipleList);
                } catch (Exception e) {
                }
            }
        });
    }

    private void next() {
        if (currentFragmentId == 0) {
//            ToastTool.showLong("功能暂未开放");
            return;
        }

        // 回调给监听者
        if (mCallback != null)
            mCallback.onClickListener(currentWork, currentFragmentId);

        // 清空存储内容
        currentWork = null;
        currentFragmentId = 0;
    }

    public void setCallback(ClickCallback callback) {
        mCallback = callback;
    }

    public interface ClickCallback {
        void onClickListener(WorkModule item, int fragmentId);
    }
}
