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

import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

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.WorkToolBarActivity;
import com.wd.workoffice.bean.InStockBean;
import com.wd.workoffice.bean.ProPriceBean;
import com.wd.workoffice.bean.ProductBean;
import com.wd.workoffice.bean.listener.ThreeMenuClickListener;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.bat.work.WorkProDetailActivity;
import com.wd.workoffice.ui.adapter.WorkInStockAdapter;
import com.wd.workoffice.ui.adapter.WorkProPriceAdapter;
import com.wd.workoffice.util.WorkUtils;
import com.wd.workoffice.util.ddmfilter.InSotckFilterData;
import com.wd.workoffice.util.ddmfilter.ProFilterData;
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 WorkInStockActivity extends WorkToolBarActivity {

    @BindView(R.id.ddm_choose)
    DropDownMenu ddmChoose;
    private RecyclerView rvData;
    private int page = 1;
    private Map<String, Object> param;
    private SmartRefreshLayout srlRefresh;
    private List<InStockBean.RecordsBean> dataList;
    private WorkInStockAdapter dataAdapter;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        InSotckFilterData filterData = new InSotckFilterData(ddmChoose, new ThreeMenuClickListener() {
            @Override
            public void clickOne(String value) {
                if (TextUtils.equals(value, "全部类型")) {
                    param.put("level", "");
                } else if (TextUtils.equals(value, "一级库")) {
                    param.put("level", "1");
                } else {
                    param.put("level", "2");
                }
                page = 1;
                param.put("current", page);
                getData();
            }

            @Override
            public void clickTwo(String value) {
                if (TextUtils.equals(value, "全部类型")) {
                    param.put("type", "");
                } else if (TextUtils.equals(value, "厂内库")) {
                    param.put("type", "IN");
                } else {
                    param.put("type", "OUT");
                }
                page = 1;
                param.put("current", page);
                getData();
            }

            @Override
            public void clickThree(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();
            }
        });
        rvData = filterData.getRvData();
        srlRefresh = filterData.getSrlRefresh();
    }


    @Override
    protected void initData() {
        dataList = new ArrayList<>();
        dataAdapter = new WorkInStockAdapter(R.layout.item_work_in_stock, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        dataAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        param = WorkUtils.pageKey();
        param.put("current", page);
        getData();
    }

    private void getData() {
        if (!WorkUtils.hasPermission(PagePermissionType.STOCK_TYPE_LIST.getPermission())){
            toast(R.string.permission);
            return;
        }
        RtfUtils.getRtf().getStockType(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<InStockBean.RecordsBean> getList = JSON.parseObject(data.getData().toString(), InStockBean.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);
        dataAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (dataList.get(position).getStockInstanceCount()==0){
                    toast("暂时没有仓库");
                    return;
                }
                startActivity(WorkInStockDetailActivity.class, "id", dataList.get(position).getId()+""
                ,"name",dataList.get(position).getName()
                        +"/"+dataList.get(position).getCode(),"num",String.valueOf(dataList.get(position).getStockInstanceCount()));
            }
        });
    }


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

    @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(WorkInStockSearchActivity.class);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
