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 androidx.recyclerview.widget.LinearLayoutManager;

import com.unis.model.DepotModel;
import com.unis.model.WorkbenchModel;
import com.unis.model.bean.MultiDepotBean;
import com.unis.model.bean.WorkModule;
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.xyxsbj.mylibrary.tool.ToastTool;

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

/**
 * 包名: com.unis.sxzcl.ui.work_module
 * 类名: WorkModuleFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/8 10:11
 * 更新人:
 * 更新时间: 2020/12/8 10:11
 * 更新说明:
 * 版本号: 1.0
 */
public class WorkModuleFragment extends BaseListFragment<FragmentWorkModuleBinding>
        implements WorkModuleAdapter.ClickCallback {

    private WorkModule mModule;
    private WorkModuleAdapter mAdapter;
    private boolean mIsHome;


    public static WorkModuleFragment newFragment(boolean home, String data) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("home", home);
        bundle.putString("data", data);
        WorkModuleFragment fragment = new WorkModuleFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

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

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        Bundle bundle = getArguments();
        if (bundle == null)
            bundle = new Bundle();
        String data = bundle.getString("data", "");
        mModule = getGson().fromJson(data, WorkModule.class);
        mIsHome = bundle.getBoolean("home", false);
        initTitle();

        mAdapter = new WorkModuleAdapter();
        mAdapter.setCallback(this);
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, false);
        mBinding.recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
    }

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

    private void initTitle() {
        mBinding.vStatus.setHeight(getStatusHeight());
        mBinding.barLeft.setOnClickListener(v -> onBack());
        mBinding.barCompany.setOnClickListener(v -> {
            // 切换组织架构
            navigation(R.id.companyListFragment);
        });
        mBinding.barRight.setOnClickListener(v -> {
            MultiDepotBean selectDepot = DepotModel.get().getSelectDepot();
            if (selectDepot == null) {
                ToastTool.showLong("请先选择库");
                return;
            }
            // 企业信息统计
            navigation(R.id.companyStatsFragment);
        });

        // 功能标题展示
        if (mModule == null) mBinding.barTitle.setText("工作台");
        else mBinding.barTitle.setText(mModule.getTitle());

        // 企业名称展示
        MultiDepotBean selectDepot = DepotModel.get().getSelectDepot();
        if (selectDepot == null)
            mBinding.barCompany.setText("请选择库");
        else
            mBinding.barCompany.setText(selectDepot.depotName);


        if (mIsHome) {
            // 首页
            // 隐藏后退
            mBinding.barLeft.setVisibility(View.GONE);
            // 隐藏功能标题
            mBinding.barTitle.setVisibility(View.GONE);
        } else {
            // 非首页
            // 隐藏企业标题
            mBinding.barCompany.setVisibility(View.GONE);
            // 隐藏统计信息
            mBinding.barRight.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onFirstShow() {
        onRefresh();
    }

    @Override
    public void onRefreshData() {

        MultiDepotBean selectDepot = DepotModel.get().getSelectDepot();
        if (selectDepot == null) {
            mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            loadComplete();
            return;
        } else {
            mBinding.recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        }

        if (mModule == null) {
            mAdapter.setList(WorkbenchModel.get().mHomeList);
        } else {
            mAdapter.setList(WorkbenchModel.get().mChildMap.get(mModule.getTitle()));
        }
        loadComplete();
    }

    @Override
    public void onClickListener(WorkModule item, int fragmentId) {
        Bundle bundle = new Bundle();
        bundle.putString("data", getGson().toJson(item));
        navigation(fragmentId, bundle);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(SelectDepotEvent bean) {
        if (mIsHome) {
            MultiDepotBean selectDepot = DepotModel.get().getSelectDepot();
            mBinding.barCompany.setText(selectDepot.depotName);
            onRefresh();
        }
    }


}
