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

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.AddStockChooseBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.SendApplyCrucibleChooseAdapter;
import com.wd.workoffice.util.WorkUtils;

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

import androidx.recyclerview.widget.LinearLayoutManager;
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 SendApplyCrucibleDetailFromActivity extends WorkToolBarActivity {

    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_ok)
    TextView tvOk;
    @BindView(R.id.ll_bottom)
    RelativeLayout llBottom;
    @BindView(R.id.rv_product)
    RecyclerView rvProduct;
    @BindView(R.id.et_key)
    EditText etKey;
    @BindView(R.id.btn_search)
    Button btnSearch;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.tv_count_desc)
    TextView tvCountDesc;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    private List<AddStockChooseBean.RecordsBean> productList;
    private SendApplyCrucibleChooseAdapter productAdapter;
    private int page = 1;
    private Map<String, Object> param;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        llSearch.setVisibility(View.GONE);
        llTitle.setVisibility(View.GONE);
        rvProduct.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
    }

    @Override
    protected void initData() {
        productList = new ArrayList<>();
        productAdapter = new SendApplyCrucibleChooseAdapter(R.layout.item_add_stock_choose, productList, 2);
        productAdapter.bindToRecyclerView(rvProduct);
        productAdapter.notifyDataSetChanged();
        param = WorkUtils.pageKey();
        param.put("current", page);
        param.put("sendOutApplyItemId", getIntent().getStringExtra("sendOutApplyItemId"));
        getData();

    }

    private void changeChooseStatue() {
        String pro = getIntent().getStringExtra("pro");
        if (!TextUtils.isEmpty(pro)) {
            Map<Integer, Boolean> isChoose = productAdapter.getIsChoose();
            List<AddStockChooseBean.RecordsBean> chooseList = JSON.parseArray(pro, AddStockChooseBean.RecordsBean.class);
            for (AddStockChooseBean.RecordsBean chooseStock : chooseList) {
                for (int i = 0; i < productList.size(); i++) {
                    if (productList.get(i).getId() == chooseStock.getId()) {
                        isChoose.put(i, true);
                        productList.get(i).setChooseNum(chooseStock.getChooseNum());
                    }
                }
            }
            productAdapter.notifyDataSetChanged();
        }

    }

    @Override
    protected void initEvent() {
//        productAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
//                param.put("current", page);
//                getData();
//            }
//        }, rvProduct);
    }

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


    private void getData() {
        RtfUtils.getRtf().sendApplyCrucibleDetail(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<AddStockChooseBean.RecordsBean> getList = JSON.parseObject(data.getData().toString(), AddStockChooseBean.class).getRecords();
                productList.clear();
                productList.addAll(getList);
                productAdapter.notifyDataSetChanged();
                changeChooseStatue();
//                if (page == 1) {
//                    productList.clear();
//                    productList.addAll(getList);
//                    productAdapter.notifyDataSetChanged();
//                    productAdapter.loadMoreComplete();
//                } else {
//                    productAdapter.loadMoreComplete();
//                    productList.addAll(getList);
//                    productAdapter.notifyDataSetChanged();
//                }
//                if (getList.size() == 0) {
//                    productAdapter.loadMoreEnd();
//                } else {
//                    page++;
//                }
            }
        });
    }


}
