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

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.mesBean.WorkLinkBean;
import com.wd.workoffice.bean.mesBean.WorkMachineBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.WorkLinkAdapter;
import com.wd.workoffice.ui.adapter.WorkMarchineAdapter;
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 butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

/**
 * 关联单号
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkOrderNoSearchActivity extends WorkToolBarActivity {

    @BindView(R.id.et_key)
    EditText etKey;
    @BindView(R.id.btn_search)
    Button btnSearch;
    @BindView(R.id.rv_client)
    RecyclerView rvClient;
    private Map<String, Object> param;
    private List<JSONObject> clientList;
    private WorkLinkAdapter clientAdapter;
    private int page = 1;
    private Map<String, Object> paramMap;
    private String type;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        rvClient.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));

    }

    @Override
    protected void initData() {
        type = getIntent().getStringExtra("type");


        clientList = new ArrayList<>();
        clientAdapter = new WorkLinkAdapter(R.layout.item_fault_search, clientList);
        clientAdapter.bindToRecyclerView(rvClient);
        clientAdapter.setEmptyView(R.layout.view_empty_content, rvClient);
        param = WorkUtils.pageMesKey();
        paramMap = WorkUtils.simpleParam();
        paramMap.put("chooseLocation", getIntent().getStringExtra("id"));
        param.put("paramMap", paramMap);
        param.put("pageNum", 1);
        getData();
    }

    private void getData() {
        Observable<BaseMesBean> modinspo = RtfUtils.getMesRtf().modinspo("1", WorkUtils.convertMapToBody(param));
        switch (type) {
            case "1":
                modinspo = RtfUtils.getMesRtf().modinspo("1", WorkUtils.convertMapToBody(param));
                break;
            case "2":
                modinspo = RtfUtils.getMesRtf().spotCheckBillList("1", WorkUtils.convertMapToBody(param));
                break;
            case "4":
                modinspo = RtfUtils.getMesRtf().opmreportErrorList("1", WorkUtils.convertMapToBody(param));
                break;
            case "5":
                modinspo = RtfUtils.getMesRtf().pjmproject("1", WorkUtils.convertMapToBody(param));
                break;
        }
        modinspo.compose(Transformer.schedule()).subscribe(new WorkObserver<BaseMesBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

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

            @Override
            public void onSuccess(BaseMesBean data) {
                hideLoading();
                if (!data.isSuccess()) {
                    toast(data.getMessage());
                    return;
                }
                List<JSONObject> covertList = new ArrayList<>();
                switch (type) {
                    case "1":
                        List<WorkLinkBean.ListBean> oneLinkList = JSON.parseObject(data.getData().toString(), WorkLinkBean.class).getList();
                        for (WorkLinkBean.ListBean linkBean : oneLinkList) {
                            JSONObject link = new JSONObject();
                            link.put("name", linkBean.getDescription());
                            link.put("code", linkBean.getInspoCode());
                            link.put("id", linkBean.getId());
                            covertList.add(link);
                        }
                        break;
                    case "2":
                        List<WorkLinkBean.ListBean> twoList = JSON.parseObject(data.getData().toString(), WorkLinkBean.class).getList();
                        for (WorkLinkBean.ListBean linkBean : twoList) {
                            JSONObject link = new JSONObject();
                            link.put("name", linkBean.getBillDescription());
                            link.put("code", linkBean.getBillNum());
                            link.put("id", linkBean.getId());
                            covertList.add(link);
                        }
                        break;
                    case "4":
                        List<WorkLinkBean.ListBean> fourList = JSON.parseObject(data.getData().toString(), WorkLinkBean.class).getList();
                        for (WorkLinkBean.ListBean linkBean : fourList) {
                            JSONObject link = new JSONObject();
                            link.put("name", linkBean.getDescription());
                            link.put("code", linkBean.getReportCode());
                            link.put("id", linkBean.getId());
                            covertList.add(link);
                        }
                        break;
                    case "5":
                        List<WorkLinkBean.ListBean> fiveList = JSON.parseObject(data.getData().toString(), WorkLinkBean.class).getList();
                        for (WorkLinkBean.ListBean linkBean : fiveList) {
                            JSONObject link = new JSONObject();
                            link.put("name", linkBean.getDescription());
                            link.put("code", linkBean.getProjectCode());
                            link.put("id", linkBean.getId());
                            covertList.add(link);
                        }  break;
                }


                if (page == 1) {
                    clientList.clear();
                    clientList.addAll(covertList);
                    clientAdapter.notifyDataSetChanged();
                    clientAdapter.loadMoreComplete();
                } else {
                    clientAdapter.loadMoreComplete();
                    clientList.addAll(covertList);
                    clientAdapter.notifyDataSetChanged();
                }
                if (covertList.size() == 0) {
                    clientAdapter.loadMoreEnd();
                } else {
                    page++;
                }
                toast("搜索成功");
            }
        });
    }

    @Override
    protected void initEvent() {
        clientAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                param.put("pageNum", page);
                getData();
            }
        }, rvClient);
        clientAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();
                intent.putExtra("data", JSON.toJSONString(clientList.get(position)));
                setResult(10001, intent);
                finish();
            }
        });
    }

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


    @OnClick(R.id.btn_search)
    public void onViewClicked() {
        paramMap.put("description", etKey.getText().toString());
        param.put("pageNum", 1);
        page = 1;
        getData();
    }
}
