package com.unis.sxzcl.ui.template;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.databinding.FragmentBaseListBinding;
import com.xyxsbj.mylibrary.tool.DPTool;
import com.xyxsbj.mylibrary.tool.StringTool;

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

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

/**
 * 包名: com.unis.sxzcl.ui.template
 * 类名: TemplateListFragment
 * 作用描述: 模版-列表页
 * 创建人: mQ
 * 创建时间: 2020/11/20 14:43
 * 更新人:
 * 更新时间: 2020/11/20 14:43
 * 更新说明:
 * 版本号: 1.0
 */
public class TemplateListFragment extends BaseListFragment<FragmentBaseListBinding> implements TemplateListAdapter.Callback {

    private TemplateListAdapter mAdapter;
    private String mPageType;

    public static TemplateListFragment newFragment(String pageType) {
        Bundle bundle = new Bundle();
        bundle.putString("pageType", pageType);

        TemplateListFragment fragment = new TemplateListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onRefreshData() {
        new Handler().postDelayed(() -> {
            List<TemplateListBean> list = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                list.add(new TemplateListBean(
                        i + 1,
                        "2020-11-18 12:00:00",
                        "1_0_1,2_1_1,3_0_1,4_1_2,5_0_2,6_1_2,7_0_3,8_1_3,9_0_4,10_1_4,",
                        4,
                        "1号仓"
                ));
            }
            mAdapter.setList(list);
            loadComplete();
        }, 500);
    }

    @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();
        mPageType = bundle.getString("pageType");

        mAdapter = new TemplateListAdapter();
        mAdapter.setCallback(this);
        //底部增加 80dp的高度
        TextView footView = new TextView(getContext());
        footView.setHeight(DPTool.dp2px(getContext(), 80));
        mAdapter.addFooterView(footView);
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, false);
    }


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

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

    //列表点击事件
    @Override
    public void onItemClick(TemplateListBean item) {
        DetailTemplateFragment.startPage(getContext(), mPageType, item);
    }

    //接收刷新列表页的任务
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventRefreshTask(TemplateEvent event) {
        //没有指定类型 直接刷新
        if (StringTool.isEmpty(event.pageType)) {
            onRefresh();
            return;
        }

        //有页面类型时 页面类型一致才刷新
        if (event.pageType.equals(mPageType)) {
            onRefresh();
        }

    }

}
