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

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.WorkBaseActivity;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.mesBean.ReportProBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.ReportProAdapter;
import com.wd.workoffice.ui.adapter.ReportProTopAdapter;
import com.wd.workoffice.util.WorkUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
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 MesReportProActivity 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 ReportProTopAdapter topAdapter;
    private ReportProAdapter proBadAdapter;
    private List<ReportProBean.DataBean> proBadList;
    private List<ReportProBean.DataBean> topList;
    private Map<String, Object> param;

    @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() {
        topList = new ArrayList<>();
        proBadList = new ArrayList<>();
        topAdapter = new ReportProTopAdapter(R.layout.item_mes_report_top, topList);
        proBadAdapter = new ReportProAdapter(R.layout.item_mes_report_pro_bad, proBadList);
        topAdapter.bindToRecyclerView(rvTop);
        proBadAdapter.bindToRecyclerView(rvProBad);
        param = WorkUtils.simpleParam();
        param.put("beginDate", getIntent().getStringExtra("beginDate"));
        param.put("endDate", getIntent().getStringExtra("endDate"));
        param.put("seriesId", getIntent().getStringExtra("id"));
        param.put("scapReason", getIntent().getStringExtra("scapReason"));
        getData();
    }

    private void getData() {
        RtfUtils.getMesRtf().scrapAnalysisDetailList("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<ReportProBean.DataBean> getList = JSON.parseArray(data.getData().toString(), ReportProBean.DataBean.class);
                Collections.sort(getList, new Comparator<ReportProBean.DataBean>() {
                    @Override
                    public int compare(ReportProBean.DataBean o1, ReportProBean.DataBean o2) {
                        return new BigDecimal(o2.getScrapCount()).compareTo(new BigDecimal(o1.getScrapCount()));
                    }
                });
                topList.clear();
                topList.addAll(getList);
                proBadList.clear();
                proBadList.addAll(getList);
                topAdapter.notifyDataSetChanged();
                proBadAdapter.notifyDataSetChanged();
            }
        });

    }

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

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

}
