package com.unis.sxzcl.ui.aeration;

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

import androidx.core.content.ContextCompat;

import com.google.gson.Gson;
import com.unis.model.DataCallback;
import com.unis.model.DepotModel;
import com.unis.model.HouseModel;
import com.unis.model.ProcessModel;
import com.unis.model.UserModel;
import com.unis.model.bean.HouseListBean;
import com.unis.model.bean.MultiDepotBean;
import com.unis.model.bean.aeration.AerationBean;
import com.unis.model.bean.user.RoleType;
import com.unis.model.bean.user.UserBean;
import com.unis.model.event.AerationEvent;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.databinding.FragmentBaseListTitleBinding;
import com.unis.sxzcl.ui.house.SelectHouseDialog;
import com.xyxsbj.mylibrary.tool.ToastTool;

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

import java.util.List;

/**
 * 机械通风列表
 * 创建人: mQ
 * 时间: 2020/12/14 17:50
 */
public class AerationListFragment extends BaseListFragment<FragmentBaseListTitleBinding> implements AerationListAdapter.Callback {


    private AerationListAdapter mAdapter;
    private SelectHouseDialog mHouseDialog;

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

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        HouseListBean house = HouseModel.get().getSelectHouse();
        if (house == null) {
            ToastTool.showLong("未选择仓房");
            onBack();
            return;
        }

        mBinding.bar.setTitle("机械通风(" + house.houseName + ")")
                .setTitleRightDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_down_white))
                .setTitleClickListener(v -> {
                    // 点击标题选择仓房
                    showHouseSelect();
                })
                .setLeftClickListener(v -> onBack());

        // 保管员显示新增按钮
        UserBean user = UserModel.get().getUser();
        if (user != null && user.getRoleIds().equals(RoleType.R_BGY)) {
            mBinding.bar.setRightText("新增").setRightClickListener(v -> goAdd());
        }


        mAdapter = new AerationListAdapter();
        mAdapter.setCallback(this);
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, true);
    }

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

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

    @Override
    public void onRefreshData() {
        getData(true);
    }

    @Override
    public void onMoreData() {
        super.onMoreData();
        getData(false);
    }

    private int page = 1;

    public void getData(boolean refresh) {

        // 修改页数
        if (refresh) page = 1;
        else page++;

        // 请求接口
        ProcessModel.get().getProcessList(page, "tf", new DataCallback<List<AerationBean>>() {
            @Override
            public void onSuccess(List<AerationBean> data) {
                if (!isActive()) return;

                // 填充数据
                if (page == 1) mAdapter.setList(data);
                else mAdapter.addData(data);

                // 修改页面状态
                if (data.size() == 0) loadEnd();
                else loadComplete();

            }

            @Override
            public void onError(String error) {
                super.onError(error);
                if (!isActive()) return;
                loadError();
            }
        });
    }

    private void goAdd() {
        Bundle bundle = new Bundle();
        bundle.putString("pageType", "add");
        navigation(R.id.aerationDetailFragment, bundle);
    }


    @Override
    public void onItemClickListener(AerationBean item) {
        Bundle bundle = new Bundle();
        bundle.putString("data", new Gson().toJson(item));
        navigation(R.id.aerationDetailFragment, bundle);
    }


    /**
     * 选择仓房
     */
    private void showHouseSelect() {
        MultiDepotBean depot = DepotModel.get().getSelectDepot();
        if (depot == null) {
            ToastTool.showLong("请先选择库");
            return;
        }

        if (mHouseDialog == null)
            mHouseDialog = new SelectHouseDialog(getContext());
        // 监听仓房选择结果
        mHouseDialog.setCallback(bean -> {
            if (bean == null)
                return;
            mBinding.bar.setTitle("机械通风(" + bean.houseName + ")");
            onRefresh();
        });
        // 弹出仓房选择对话框
        mHouseDialog.show(depot.depotCode, -1);
    }


    @Subscribe
    public void onEvent(AerationEvent event) {
        onRefresh();
    }
}
