package com.wd.workoffice.ui.activity.bat.work;

import android.content.Context;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopups;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.Config;
import com.wd.workoffice.app.WorkBaseActivity;
import com.wd.workoffice.bean.OrderBean;
import com.wd.workoffice.bean.WarehoustBean;
import com.wd.workoffice.contract.WorkWarehouseContract;
import com.wd.workoffice.presenter.WorkWarehousePresenter;
import com.wd.workoffice.ui.adapter.FilterBaseAdapter;
import com.wd.workoffice.ui.adapter.WorkWarehouseOrderAdapter;
import com.wd.workoffice.util.WorkUtils;

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

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 仓库订单搜索
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkWarehouseOrderSearchActivity extends WorkBaseActivity implements WorkWarehouseContract.View {

    @BindView(R.id.et_key)
    EditText etKey;
    @BindView(R.id.tb_title)
    Toolbar tbTitle;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    private int page = 1;
    private Map<String, Object> param;
    private WorkWarehousePresenter workProPresenter;
    private WorkWarehouseOrderAdapter workProductAdapter;
    private List<OrderBean> dataList;
    private QMUIPopup popup;
    private FilterBaseAdapter filterBaseAdapter;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        workProPresenter = new WorkWarehousePresenter();
        workProPresenter.onAttach(this);
        setSupportActionBar(tbTitle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        rvData.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @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);
        filterBaseAdapter = new FilterBaseAdapter(Config.WAREHOUSE_STATUS_LIST);
        popup = topPopInit(this);
    }


    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            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.activity_warehouse_order_search;
    }


    @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++;
        }
    }


    @OnClick({R.id.tv_type, R.id.tv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_type:
                popup.show(tbTitle);
                break;
            case R.id.tv_search:
                String key = etKey.getText().toString();
                if (TextUtils.isEmpty(key)) {
                    toast("请输入搜索关键字");
                    return;
                }
                page = 1;
                param.put("current", page);
                param.put("id", key);
                workProPresenter.getOrder(param);
                break;
        }
    }

    public QMUIPopup topPopInit(Context context) {
        popup = QMUIPopups.listPopup(context,
                ViewGroup.LayoutParams.MATCH_PARENT,
                QMUIDisplayHelper.dp2px(context, 300), filterBaseAdapter
                ,
                (parent, view, position, id) -> {
                    filterBaseAdapter.setChoosePos(position);
                    filterBaseAdapter.notifyDataSetChanged();
                    tvType.setText(Config.WAREHOUSE_STATUS_LIST.get(position));
                    popup.dismiss();
                    page = 1;
                    param.put("current", page);
                    param.put("orderType", Config.WAREHOUSE_CODE_LIST.get(position));
                    workProPresenter.getOrder(param);
                })
                .animStyle(QMUIPopup.ANIM_GROW_FROM_CENTER)
                .preferredDirection(QMUIPopup.DIRECTION_TOP)
                .shadow(true)
                .edgeProtection(QMUIDisplayHelper.dp2px(context, 10))
                .offsetYIfTop(QMUIDisplayHelper.dp2px(context, 5));
        return popup;
    }
}
