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

import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

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.BaseBean;
import com.wd.workoffice.app.WorkBaseActivity;
import com.wd.workoffice.bean.InStockBean;
import com.wd.workoffice.bean.WarehoustBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.WorkInStockAdapter;
import com.wd.workoffice.ui.adapter.WorkInStockDetailAdapter;
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;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;


/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkInStockDetailSearchActivity extends WorkBaseActivity {

    @BindView(R.id.et_key)
    EditText etKey;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @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 List<WarehoustBean.RecordsBean> dataList;
    private WorkInStockDetailAdapter dataAdapter;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        rvData.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));
        setSupportActionBar(tbTitle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    @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() {
        dataList = new ArrayList<>();
        dataAdapter = new WorkInStockDetailAdapter(R.layout.item_work_in_stock_detail, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        dataAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        param = WorkUtils.pageKey();
        param.put("current", page);
        param.put("stockTypeId", getIntent().getStringExtra("id"));
        param.put("searchKey", "name");
        getData();
    }

    private void getData() {
        RtfUtils.getRtf().getWarehouse(param).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                List<WarehoustBean.RecordsBean> getList = JSON.parseObject(data.getData().toString(), WarehoustBean.class).getRecords();

                if (page == 1) {
                    srlRefresh.finishRefresh();
                    dataList.clear();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                    dataAdapter.loadMoreComplete();
                } else {
                    dataAdapter.loadMoreComplete();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                }
                if (getList.size() == 0) {
                    dataAdapter.loadMoreEnd();
                } else {
                    page++;
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            page = 1;
            param.put("current", page);
            getData();
        });
        dataAdapter.setOnLoadMoreListener(() -> {
            param.put("current", page);
            getData();
        }, rvData);
    }


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


    @OnClick(R.id.tv_search)
    public void onViewClicked() {
        page = 1;
        param.put("current", page);
        param.put("searchValue", etKey.getText().toString());
        getData();
    }
}
