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

import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.WarehoustBean;
import com.wd.workoffice.bean.listener.ThreeMenuClickListener;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.WorkInStockDetailAdapter;
import com.wd.workoffice.util.WorkUtils;
import com.wd.workoffice.util.ddmfilter.InStockDetailFilterData;
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;
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 WorkInStockDetailActivity extends WorkToolBarActivity {

    @BindView(R.id.ddm_choose)
    DropDownMenu ddmChoose;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_num)
    TextView tvNum;
    private RecyclerView rvData;
    private int page = 1;
    private Map<String, Object> param;
    private SmartRefreshLayout srlRefresh;
    private List<WarehoustBean.RecordsBean> dataList;
    private WorkInStockDetailAdapter dataAdapter;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tvName.setText(getIntent().getStringExtra("name"));
        tvNum.setText("实例数量 "+getIntent().getStringExtra("num"));
        InStockDetailFilterData filterData = new InStockDetailFilterData(this,ddmChoose, new ThreeMenuClickListener() {
            @Override
            public void clickOne(String value) {
                if (TextUtils.equals(value, "全部类型")) {
                    param.put("siteType", "");
                } else if (TextUtils.equals(value, "厂内库")) {
                    param.put("siteType", "IN");
                } else {
                    param.put("siteType", "OUT");
                }
//                param.put("type", value);
                page = 1;
                param.put("current", page);
                getData();
            }

            @Override
            public void clickTwo(String value) {
                if (TextUtils.equals(value, "全部状态")) {
                    param.put("status", "");
                } else if (TextUtils.equals(value, "未启用")) {
                    param.put("status", "0");
                } else {
                    param.put("status", "1");
                }
                page = 1;
                param.put("current", page);
                getData();
            }

            @Override
            public void clickThree(String value) {

            }
        });
        rvData = filterData.getRvData();
        srlRefresh = filterData.getSrlRefresh();
    }


    @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"));
        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_stock_detail;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search:
                startActivity(WorkInStockDetailSearchActivity.class,"id",getIntent().getStringExtra("id"));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
