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

import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
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.AnalysisBean;
import com.wd.workoffice.bean.mesBean.SearchBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.ReportAnalysisBadAdapter;
import com.wd.workoffice.ui.adapter.ReportTopAdapter;
import com.wd.workoffice.util.WorkUtils;

import java.util.ArrayList;
import java.util.HashMap;
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 MesAnalysisActivity extends WorkToolBarActivity {
    @BindView(R.id.rv_top)
    RecyclerView rvTop;
    @BindView(R.id.rv_pro_bad)
    RecyclerView rvProBad;
    @BindView(R.id.ll_report)
    LinearLayout llReport;
    private int page = 1;
    private Map<String, Object> param;
    private ReportTopAdapter topAdapter;
    private ReportAnalysisBadAdapter proBadAdapter;
    private List<AnalysisBean.ListBean> proBadList;

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


    @Override
    protected void initData() {
        param = WorkUtils.pageMesKey();
        page = 1;
        param.put("pageNum", page);
        Map<String ,Object> paramMap =new HashMap<>();
        param.put("paramMap", paramMap);
//        List<Object> topList = new ArrayList<>();
//        topList.add(null);
//        topList.add(null);
//        topList.add(null);
        proBadList = new ArrayList<>();
//        topAdapter = new ReportTopAdapter(R.layout.item_mes_report_top, topList);
        proBadAdapter = new ReportAnalysisBadAdapter(R.layout.item_mes_analysis, proBadList);
//        topAdapter.bindToRecyclerView(rvTop);
        proBadAdapter.bindToRecyclerView(rvProBad);

        getData();
    }

    private void getData() {
        RtfUtils.getMesRtf().defectdictList("1", WorkUtils.convertMapToBody(param)).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<AnalysisBean.ListBean> getList = JSON.parseObject(data.getData().toString(), AnalysisBean.class).getList();
                if (page == 1) {
                    proBadList.clear();
                    proBadList.addAll(getList);
                    proBadAdapter.notifyDataSetChanged();
                    proBadAdapter.loadMoreComplete();
                } else {
                    proBadAdapter.loadMoreComplete();
                    proBadList.addAll(getList);
                    proBadAdapter.notifyDataSetChanged();
                }
                if (getList.size() == 0) {
                    proBadAdapter.loadMoreEnd();
                } else {
                    page++;
                }
            }
        }); }


    @Override
    protected void initEvent() {
        proBadAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(MesAnalysisDetailActivity.class,"info",JSON.toJSONString(proBadList.get(position)));
            }
        });
    }

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

}
