package com.unis.sxzcl.ui.quantity_quality;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.unis.model.BasicModel;
import com.unis.model.DepotModel;
import com.unis.model.HouseModel;
import com.unis.model.PermitConst;
import com.unis.model.ProcessModel;
import com.unis.model.UserModel;
import com.unis.model.bean.EnumBean;
import com.unis.model.bean.HouseListBean;
import com.unis.model.bean.MultiDepotBean;
import com.unis.model.bean.QuantityQualityBean;
import com.unis.model.bean.user.RoleType;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseFragment;
import com.unis.sxzcl.databinding.FragmentBaseTabPagerBinding;
import com.unis.sxzcl.ui.house.SelectHouseDialog;
import com.unis.sxzcl.widget.TabFragmentPagerAdapter;
import com.xyxsbj.mylibrary.tool.ToastTool;

import org.greenrobot.eventbus.EventBus;

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


/**
 * 库存质量首页
 * 创建人: mQ
 * 时间: 2021/2/1 14:03
 */
public class QuantityQualityHomeFragment extends BaseFragment<FragmentBaseTabPagerBinding> implements ViewPager.OnPageChangeListener {


    // 检查类型
    private List<EnumBean> mEnumList;
    // 标签名称
    private List<String> mTabs;
    // 页面
    private List<Fragment> mFragments;
    // 选仓对话框
    private SelectHouseDialog mHouseDialog;
    private String mWorkType;

    @Override
    protected FragmentBaseTabPagerBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentBaseTabPagerBinding.inflate(inflater, container, false);
    }

    @Override
    protected void init() {
        Bundle bundle = getArguments();
        if (bundle == null) {
            ToastTool.showLong("未传参数");
            onBack();
            return;
        }

        HouseListBean house = HouseModel.get().getSelectHouse();
        if (house == null) {
            ToastTool.showLong("未选择仓房");
            onBack();
            return;
        }
        mWorkType = bundle.getString("workType", "0");
        setTitle(house.houseName);

        mBinding.bar.setTitleRightDrawable(R.drawable.ic_down_white)
                .setTitleClickListener(v -> {
                    // 点击标题选择仓房
                    showHouseSelect();
                })
                .setLeftClickListener(v -> onBack())
                .setRightText("新增")
                .setRightClickListener(v -> goAdd());

    }

    /**
     * 设置标题
     */
    private void setTitle(String hoseName) {
        if (mWorkType.equals("0"))
            mBinding.bar.setTitle("库存质量(" + hoseName + ")");
        else
            mBinding.bar.setTitle("出库质量(" + hoseName + ")");
    }

    @Override
    protected void onFirstShow() {
        // 获取库存质量检查类型
        if (mWorkType.equals("0")) {
            mEnumList = BasicModel.get().getEnumList("31");
        } else {
            mEnumList = BasicModel.get().getEnumList("32");
        }
        if (mEnumList == null) {
            ToastTool.showLong("没有库存质量类型");
            return;
        }


        // 是否有权限新增
        boolean permit;
        if (mWorkType.equals("0"))
            permit = UserModel.get().containPermit(PermitConst.quantity_add);
        else {
            permit = UserModel.get().containPermit(PermitConst.out_add);
        }
        mBinding.bar.setRightVisible(permit);


        // 切换到用户角色所在的页面
//        mBinding.viewPager.setCurrentItem(getRoleCurrentPage());


        initFragment();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ProcessModel.get().removeSelectTodo();
    }

    /**
     * 创建页面
     */
    private void initFragment() {
        mTabs = new ArrayList<>();
        mFragments = new ArrayList<>();

        for (EnumBean item : mEnumList) {
            // 标签名称
            mTabs.add(item.getEnumName());
            // 页面
            QuantityQualityListFragment fragment =
                    QuantityQualityListFragment.newFragment(item.getEnumId(), mWorkType);
            mFragments.add(fragment);
        }

        // 设置标签栏、滑动控件
        mBinding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        TabFragmentPagerAdapter adapter = new TabFragmentPagerAdapter(
                getChildFragmentManager(), mTabs, mFragments);
        mBinding.viewPager.setAdapter(adapter);
        mBinding.viewPager.setOffscreenPageLimit(mFragments.size());
        mBinding.viewPager.addOnPageChangeListener(this);
    }


    /**
     * 新增
     */
    private void goAdd() {
        EnumBean enumBean = mEnumList.get(mBinding.viewPager.getCurrentItem());
        Bundle bundle = new Bundle();
        bundle.putString(QuantityQualityDetailFragment.key_page_type, "add");
        bundle.putString(QuantityQualityDetailFragment.key_work_type, mWorkType);
        bundle.putString(QuantityQualityDetailFragment.key_check_type, enumBean.getEnumId());
        bundle.putString(QuantityQualityDetailFragment.key_check_type_name, enumBean.getEnumName());
//        bundle.putString(QuantityQualityDetailFragment.key_info,);
        navigation(R.id.quantityQualityDetailFragment, bundle);
    }


    /**
     * 选择仓房
     */
    private void showHouseSelect() {
        MultiDepotBean depot = DepotModel.get().getSelectDepot();
        if (mHouseDialog == null)
            mHouseDialog = new SelectHouseDialog(getContext());
        // 监听仓房选择结果
        mHouseDialog.setCallback(bean -> {
            if (bean == null)
                return;
            setTitle(bean.houseName);
            EventBus.getDefault().post(new QuantityQualityBean());
        });
        // 弹出仓房选择对话框
        mHouseDialog.show(depot.depotCode, -1);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        /**
         * 登录人是否可以新增
         * 每个页面所需角色不同
         * 页面角色和登录角色一致时 才可以新增
         */
//        switch (position) {
//            case 0:
//                // 保管员
//                mBinding.bar.setRightVisible(UserModel.get().isUserRole(RoleType.R_BGY));
//                break;
//            case 1:
//                //科长
//                mBinding.bar.setRightVisible(UserModel.get().isUserRole(RoleType.R_KZ));
//                break;
//            case 2:
//                //副总经理
//                mBinding.bar.setRightVisible(UserModel.get().isUserRole(RoleType.R_FZJL));
//                break;
//            case 3:
//                //总经理
//                mBinding.bar.setRightVisible(UserModel.get().isUserRole(RoleType.R_ZJL));
//                break;
//        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    // 获取角色所在页面
    public int getRoleCurrentPage() {
        if (UserModel.get().isUserRole(RoleType.R_BGY))
            return 0;
        if (UserModel.get().isUserRole(RoleType.R_KZ))
            return 1;
        if (UserModel.get().isUserRole(RoleType.R_FZJL))
            return 2;
        if (UserModel.get().isUserRole(RoleType.R_ZJL))
            return 3;
        return 0;
    }
}