package com.wd.workoffice.ui.fg;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseFg;
import com.wd.workoffice.bean.OrderBean;
import com.wd.workoffice.bean.WarehoustBean;
import com.wd.workoffice.bean.listener.WarehouseOrderFilterClickListener;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.contract.WorkWarehouseContract;
import com.wd.workoffice.presenter.WorkWarehousePresenter;
import com.wd.workoffice.ui.activity.bat.work.WorkWarehouseOrderDetailActivity;
import com.wd.workoffice.ui.adapter.WorkWarehouseOrderAdapter;
import com.wd.workoffice.util.WorkUtils;
import com.wd.workoffice.util.ddmfilter.WarehouseOrderFilterData;
import com.wd.workoffice.widget.DropDownMenu;

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

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkWarehouse2Fragment extends WorkBaseFg implements WorkWarehouseContract.View {

    @BindView(R.id.ddm_choose)
    DropDownMenu ddmChoose;
    private RecyclerView rvData;
    private int page = 1;
    private Map<String, Object> param;
    private SmartRefreshLayout srlRefresh;
    private WorkWarehousePresenter workProPresenter;
    private WorkWarehouseOrderAdapter workProductAdapter;
    private List<OrderBean> dataList;

    @Override
    protected void initView() {
        ButterKnife.bind(this, getContentView());
        workProPresenter = new WorkWarehousePresenter();
        workProPresenter.onAttach(this);
        WarehouseOrderFilterData filterData = new WarehouseOrderFilterData(getActivity(),ddmChoose, new WarehouseOrderFilterClickListener() {
            @Override
            public void clickType(String value) {
                param.put("orderType", value);
                page = 1;
                param.put("current", page);
                workProPresenter.getOrder(param);
            }
        });
        rvData = filterData.getRvData();
        srlRefresh = filterData.getSrlRefresh();
    }


    @Override
    protected void initData() {
        param = WorkUtils.pageKey();
        page = 1;
        param.put("current", page);
        dataList = new ArrayList<>();
        workProductAdapter = new WorkWarehouseOrderAdapter(R.layout.item_work_warehouse_order, dataList);
        workProductAdapter.bindToRecyclerView(rvData);
        workProductAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        if (!WorkUtils.hasPermission(PagePermissionType.STOCK_QUANTITY_ORDER.getPermission())){
            toast(R.string.permission);
        }else {
            workProPresenter.getOrder(param);
        }

    }


    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            if (!WorkUtils.hasPermission(PagePermissionType.STOCK_QUANTITY_ORDER.getPermission())){
                toast(R.string.permission);
            }else {
                page = 1;
                param.put("current", page);
                workProPresenter.getOrder(param);
            }
        });
        workProductAdapter.setOnLoadMoreListener(() -> {
            param.put("current", page);
            workProPresenter.getOrder(param);
        }, rvData);
        workProductAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(WorkWarehouseOrderDetailActivity.class,
                        "id", dataList.get(position).getId(),"type", dataList.get(position).getOrderType());
            }
        });
    }


    @Override
    protected int layoutId() {
        return R.layout.fg_work_warehouse;
    }


    @Override
    public void getWarehouse(List<WarehoustBean.RecordsBean> data) {

    }

    @Override
    public void getOrder(List<OrderBean> data) {
        if (page == 1) {
            dataList.clear();
            dataList.addAll(data);
            workProductAdapter.notifyDataSetChanged();
            workProductAdapter.loadMoreComplete();
            srlRefresh.finishRefresh();
        } else {
            workProductAdapter.loadMoreComplete();
            dataList.addAll(data);
            workProductAdapter.notifyDataSetChanged();
        }
        if (data.size() == 0) {
            workProductAdapter.loadMoreEnd();
        } else {
            page++;
        }
    }
}
