package com.unis.sxzcl.ui.work_module;

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

import androidx.recyclerview.widget.GridLayoutManager;

import com.unis.model.DepotModel;
import com.unis.model.UserModel;
import com.unis.model.bean.MultiDepotBean;
import com.unis.model.bean.user.UserBean;
import com.unis.model.event.SelectDepotEvent;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.databinding.FragmentWorkModuleBinding;
import com.unis.sxzcl.widget.dialog.SystemDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 新作业列表页
 * 创建人: mQ
 * 时间: 2021/1/7 14:51
 */
public class WorkModuleFragment extends BaseListFragment<FragmentWorkModuleBinding> {

    private WorkModuleAdapter mAdapter;
    private SystemDialog mSelectDepotDialog;

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

    @Override
    protected void init() {
        EventBus.getDefault().register(this);

        mBinding.vStatus.setHeight(getStatusHeight());
        mBinding.barTitle.setText("");
        mBinding.barCompany.setOnClickListener(v -> {
            // 切换组织架构
            navigation(R.id.companyListFragment);
        });
        mBinding.barRight.setOnClickListener(v -> {
            MultiDepotBean selectDepot = DepotModel.get().getSelectDepot();
            if (selectDepot == null) {
                showSelectDepotDialog();
                return;
            }
            // 查看企业信息统计
            navigation(R.id.companyStatsFragment);
        });

        //下拉刷新
        initRefreshView(mBinding.refreshLayout, () -> {
            onRefresh();
        });

        mAdapter = new WorkModuleAdapter();
        mAdapter.setCallback((item, fragmentId) -> {
            Bundle bundle = new Bundle();
            bundle.putString("data", getGson().toJson(item));
            navigation(fragmentId, bundle);
        });
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, false, new GridLayoutManager(getContext(), 3));

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    protected void onFirstShow() {

        // 1.分公司领导可选择所有的直属库和分库
        // 2.直属库领导 可选择直属库和 组织下的分库
        // 3.分库全部人员 及 直属库员工, 不显示粮库名称 和 统计

        // 获取登录人信息
        UserBean user = UserModel.get().getUser();
        // 分公司
        boolean isBranchCompany = user.getDepotId().equals("1");
        // 分库
        boolean isBranchDepot = user.getLibraryLevel() != null && user.getLibraryLevel().equals("1");
        // 领导
        boolean isLeader = user.getHomeType() != null && !user.getHomeType().equals("3");

        if (isBranchCompany) {
            // 1.分公司领导可选择所有的直属库和分库

        } else if (!isBranchDepot && isLeader) {
            // 2.直属库领导 可选择直属库和 组织下的分库
            // 设置登录人所在库
            MultiDepotBean bean = DepotModel.get().getSelectDepot();
            if (bean == null) {
                bean = new MultiDepotBean();
                bean.depotCode = user.getDepotId();
                bean.depotName = user.getDepotName();
                DepotModel.get().saveSelectDepot(bean);
            }
            mBinding.barCompany.setText(bean.depotName);

        } else {
            // 3.分库全部人员 及 直属库员工, 不显示粮库名称和统计 , 带出自己库
            // 隐藏库名 和 统计
            mBinding.barCompany.setVisibility(View.GONE);
            mBinding.barRight.setVisibility(View.GONE);
            // 显示标题
            mBinding.barTitle.setText("作业");
            // 设置登录人所在库
            MultiDepotBean bean = DepotModel.get().getSelectDepot();
            if (bean == null) {
                bean = new MultiDepotBean();
                bean.depotCode = user.getDepotId();
                bean.depotName = user.getDepotName();
                DepotModel.get().saveSelectDepot(bean);
            }
            mBinding.barCompany.setText(bean.depotName);
        }
        onRefresh();
    }


    @Override
    protected void onShowFragment() {
        super.onShowFragment();
        // 作业功能列表时 未选库弹出提示
        MultiDepotBean selectDepot = DepotModel.get().getSelectDepot();
        if (selectDepot == null) {
            showSelectDepotDialog();
        }
    }

    // 弹出选择粮库对话框
    public void showSelectDepotDialog() {
        if (mSelectDepotDialog == null)
            mSelectDepotDialog = new SystemDialog(getContext())
                    .setHideTitle()
                    .setMsg("请先选择粮库")
                    .setLeftButton("取消", null)
                    .setRightButton("去选择", dialog -> {
                        mSelectDepotDialog.dismiss();
                        // 切换组织架构
                        navigation(R.id.companyListFragment);
                    });
        mSelectDepotDialog.show();
    }

    public void onRefreshData() {

        MultiDepotBean selectDepot = DepotModel.get().getSelectDepot();
        if (selectDepot == null) {
            showSelectDepotDialog();
            loadEnd();
            return;
        }

        mAdapter.setList(WorkbenchModel.get().getModuleList());
        loadComplete();
    }


    /**
     * 监听选库的事件 刷新作业功能
     *
     * @param bean
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(SelectDepotEvent bean) {
        MultiDepotBean selectDepot = DepotModel.get().getSelectDepot();
        mBinding.barCompany.setText(selectDepot.depotName);
        onRefresh();
    }


}
