package com.unis.sxzcl.ui.todo;

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

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.unis.model.DataCallback;
import com.unis.model.DepotModel;
import com.unis.model.HouseModel;
import com.unis.model.ProcessModel;
import com.unis.model.bean.HouseListBean;
import com.unis.model.bean.MultiDepotBean;
import com.unis.model.bean.aeration.AerationBean;
import com.unis.model.bean.todo.TodoListType;
import com.unis.model.event.TodoEvent;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.databinding.FragmentBaseListBinding;

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

import java.util.List;


/**
 * 包名: com.unis.sxzcl.ui.todo
 * 类名: TodoListFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/3 11:18
 * 更新人:
 * 更新时间: 2020/12/3 11:18
 * 更新说明:
 * 版本号: 1.0
 */
public class TodoListFragment extends BaseListFragment<FragmentBaseListBinding> implements TodoListAdapter.NextCallback {


    private BaseQuickAdapter mAdapter;
    private int mListType;

    public static TodoListFragment newFragment(@TodoListType int type) {
        Bundle bundle = new Bundle();
        bundle.putInt("listType", type);
        TodoListFragment fragment = new TodoListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

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

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

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

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        Bundle bundle = getArguments();
        mListType = bundle.getInt("listType");

        switch (mListType) {
            case TodoListType.APPLY:
                // 已发起
                mAdapter = new TodoApplyListAdapter(mListType, this);
                break;
            case TodoListType.WAIT_DISPOSED:
                // 待处理
                mAdapter = new TodoListAdapter(mListType, this);
                break;
            case TodoListType.FINISH:
                // 已处理
                mAdapter = new TodoFinishListAdapter(mListType, this);
                break;
        }
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, true);
    }

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

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


    private int page = 1;

    private void getData(boolean refresh) {
        if (refresh) page = 1;
        else page++;

        if (mListType == TodoListType.APPLY) {
            ProcessModel.get().getApplyList(page, mDataCallback);
        } else if (mListType == TodoListType.WAIT_DISPOSED) {
            ProcessModel.get().getTodoList(page, mDataCallback);
        } else if (mListType == TodoListType.FINISH) {
            ProcessModel.get().getFinishList(page, mDataCallback);
        } else {
            loadError();
        }

    }

    DataCallback mDataCallback = 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();
        }
    };

    @Override
    public void onNextCallback(AerationBean item) {
        if (item.getType().equals("tf")) {
            Bundle bundle = new Bundle();
            bundle.putString("data", getGson().toJson(item));
            navigation(R.id.todoDetailFragment, bundle);
        } else {
            // 跳转粮情检查
            // 保存要处理的待办
            ProcessModel.get().saveSelectTodo(item);
            // 保存当前库
            MultiDepotBean depot = new MultiDepotBean();
            depot.depotCode = item.getDepotId();
            depot.depotName = item.getCompanyName();
            DepotModel.get().saveSelectDepot(depot);
            // 保存当前仓房
            HouseListBean house = new HouseListBean();
            house.houseName = item.getHouseName();
            house.houseCode = item.getHouseId();
            HouseModel.get().setSelectHouse(house);
            // 跳转粮情检查首页
            navigation(R.id.grainCheckHomeFragment);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(TodoEvent event) {
        onRefresh();
    }

}
