package com.wd.workoffice.ui.fg;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseFg;
import com.wd.workoffice.bean.OrderBean;
import com.wd.workoffice.bean.event.CheckOrderEvent;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.contract.SaleSaleContract;
import com.wd.workoffice.presenter.SaleSalePresenter;
import com.wd.workoffice.ui.activity.bat.order.SaleOrderDetailActivity;
import com.wd.workoffice.ui.adapter.ProSaleAdapter;
import com.wd.workoffice.util.WorkUtils;

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

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

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;


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

    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private String state;
    private SaleSalePresenter saleSalePresenter;
    private Map<String, Object> param;
    private List<OrderBean> orderList;
    private ProSaleAdapter orderAdapter;
    private int page = 1;
    private String identity;
    @Override
    protected void initView() {
        ButterKnife.bind(this, getContentView());
        saleSalePresenter = new SaleSalePresenter();
        saleSalePresenter.onAttach(this);
        rvData.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
    }


    @Override
    protected void initData() {
        orderList = new ArrayList<>();
        orderAdapter = new ProSaleAdapter(R.layout.item_sale, orderList);
        orderAdapter.bindToRecyclerView(rvData);
        param = WorkUtils.pageKey();
        param.put("orderStatus", state);
        param.put("current", page);
        param.put("identity",  identity);
        page = 1;
        param.put("current", page);
        saleSalePresenter.getOrder(param);
    }


    @Override
    protected void initEvent() {
        orderAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                param.put("current", page);
                saleSalePresenter.getOrder(param);
            }
        }, rvData);
        orderAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!WorkUtils.hasPermission(PagePermissionType.SALE_ORDER_DETAIL.getPermission())){
                    toast(R.string.permission);
                    return;
                }
                startActivity(SaleOrderDetailActivity.class,
                        "identity",identity,
                        "id", orderList.get(position).getId() + "",
                        "status", orderList.get(position).getStatus() + "");
            }
        });
    }

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

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            state = arguments.getString("state");
            identity = arguments.getString("identity");
        }
    }

    @Override
    public void saleList(List<OrderBean> dataList) {
        if (page == 1) {
            orderList.clear();
            orderList.addAll(dataList);
            orderAdapter.notifyDataSetChanged();
            orderAdapter.loadMoreComplete();
        } else {
            orderAdapter.loadMoreComplete();
            orderList.addAll(dataList);
            orderAdapter.notifyDataSetChanged();
        }
        if (dataList.size() == 0) {
            orderAdapter.loadMoreEnd();
        } else {
            page++;
        }
    }

    @Override
    public void outList(List<OrderBean> dataList) {

    }

    @Override
    public void insideList(List<OrderBean> dataList) {

    }

    @Override
    public void supplierList(List<OrderBean> dataList) {

    }
    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

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

    @Subscribe
    public void refresh(CheckOrderEvent event) {
        page = 1;
        param.put("current", page);
        saleSalePresenter.getOrder(param);
    }
}
