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

import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
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.WorkWarehouseAdapter;
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 WorkWarehouseSearchActivity 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.srl_refresh)
    SmartRefreshLayout srlRefresh;
    private int page = 1;
    private Map<String, Object> param;
    private WorkWarehousePresenter workProPresenter;
    private WorkWarehouseAdapter workProductAdapter;
    private List<WarehoustBean.RecordsBean> dataList;

    @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);
        param.put("searchKey", "name");
        dataList = new ArrayList<>();
        workProductAdapter = new WorkWarehouseAdapter(R.layout.item_work_warehouse, dataList);
        workProductAdapter.bindToRecyclerView(rvData);
        workProductAdapter.setEmptyView(R.layout.view_empty_content, rvData);
//        workProPresenter.getWarehouse(param);
    }

    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            page = 1;
            param.put("current", page);
            workProPresenter.getWarehouse(param);
        });
        workProductAdapter.setOnLoadMoreListener(() -> {
            param.put("current", page);
            workProPresenter.getWarehouse(param);
        }, rvData);
        workProductAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(WorkProDetailActivity.class, "pro", JSON.toJSONString(dataList.get(position)));
            }
        });
    }

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


    @OnClick(R.id.iv_search)
    public void onViewClicked() {
        String key = etKey.getText().toString();
        if (TextUtils.isEmpty(key)) {
            toast("请输入搜索关键字");
            return;
        }
        page = 1;
        param.put("current", page);
        param.put("searchValue", key);
        workProPresenter.getWarehouse(param);
    }


    @Override
    public void getWarehouse(List<WarehoustBean.RecordsBean> 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++;
        }
    }

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

    }
}
