package com.unis.sxzcl.ui.work_module;

import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseNodeAdapter;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.unis.model.DepotModel;
import com.unis.model.bean.MultiDepotBean;
import com.unis.sxzcl.R;
import com.unis.sxzcl.ui.house.SelectHouseDialog;
import com.xyxsbj.mylibrary.tool.LogTool;
import com.xyxsbj.mylibrary.tool.ToastTool;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * 新版作业列表适配器
 * 创建人: mQ
 * 创建时间: 2021/1/7 15:31
 */
public class WorkModuleAdapter extends BaseNodeAdapter implements LoadMoreModule {


    private ModuleCallback mCallback;

    private int currentFragmentId;
    private WorkModuleBean currentWork;
    private SelectHouseDialog mHouseDialog;

    public WorkModuleAdapter() {
        super();
        // 标题占满一行
        addFullSpanNodeProvider(new WorkTitleProvider());
        // 功能多列
        addNodeProvider(new WorkModuleProvider());
    }

    @Override
    protected int getItemType(@NotNull List<? extends BaseNode> list, int i) {
        WorkModuleBean bean = (WorkModuleBean) list.get(i);
        // 0=标题 1=作业
        return bean.isWork() ? 1 : 0;
    }

    /**
     * 标题提供者
     */
    public class WorkTitleProvider extends BaseNodeProvider {

        @Override
        public int getItemViewType() {
            return 0;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_work_module_title_list;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, BaseNode node) {
            WorkModuleBean item = (WorkModuleBean) node;
            // 标题内容
            holder.setText(R.id.tv_name, item.getName());
            holder.setImageResource(R.id.iv_flag,
                    item.isExpanded() ? R.drawable.ic_up_normal : R.drawable.ic_down_normal);
        }

        @Override
        public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
            // 展开收缩
            getAdapter().expandOrCollapse(position);
        }
    }

    /**
     * 作业功能提供者
     */
    public class WorkModuleProvider extends BaseNodeProvider {

        @Override
        public int getItemViewType() {
            return 1;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_new_work_module;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, BaseNode node) {
            WorkModuleBean item = (WorkModuleBean) node;
            holder.setText(R.id.tv_title, item.getName());
            Glide.with(getContext())
                    .load(item.getImg())
                    .placeholder(R.drawable.ic_img_loading)
                    .error(R.drawable.no_banner)
                    .into((ImageView) holder.getView(R.id.iv_show));
        }

        @Override
        public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
            WorkModuleBean item = (WorkModuleBean) data;
            currentWork = item;
            startPage(item);
        }

    }

    public void startPage(WorkModuleBean item) {
        //页面跳转
        switch (item.getName()) {
            case WorkbenchModel.TYPE_RYJG:
                // 人员结构
                currentFragmentId = R.id.basicFragment;
                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_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_YJCG:
                // 药剂采购
                break;
            case WorkbenchModel.TYPE_YJBG:
                // 药剂保管
                break;
            case WorkbenchModel.TYPE_YJLY:
                // 药剂领用
                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_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) {
        MultiDepotBean depot = DepotModel.get().getSelectDepot();
        if (depot == null) {
            ToastTool.showLong("请先选择库");
            return;
        }
        LogTool.d("所选库:" + depot.depotCode);
        if (mHouseDialog == null)
            mHouseDialog = new SelectHouseDialog(getContext());
        // 监听仓房选择结果
        mHouseDialog.setCallback(bean -> {
            if (bean == null)
                return;
            currentFragmentId = fragmentId;
            // 选择仓房 进行下一步
            next();
        });
        // 弹出仓房选择对话框
        mHouseDialog.show(depot.depotCode, -1);
    }

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

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

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

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

    public interface ModuleCallback {
        void onModuleListener(WorkModuleBean item, int fragmentId);
    }

}
