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

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
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.AnalysisProBean;
import com.wd.workoffice.bean.mesBean.MesFactoryBean;
import com.wd.workoffice.bean.mesBean.OrderBadBean;
import com.wd.workoffice.bean.mesBean.ProBadBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.mes.report.MesReportProBadDetailActivity;
import com.wd.workoffice.ui.adapter.AnalysisProBadAdapter;
import com.wd.workoffice.ui.adapter.AnalysisTopAdapter;
import com.wd.workoffice.ui.adapter.ReportBadListAdapter;
import com.wd.workoffice.ui.adapter.ReportProBadAdapter;
import com.wd.workoffice.ui.adapter.ReportTopAdapter;
import com.wd.workoffice.util.DialogUtils;
import com.wd.workoffice.util.PickTimeUtils;
import com.wd.workoffice.util.WorkUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
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 flexible.xd.android_base.utils.TimeUtils;
import io.reactivex.disposables.Disposable;

/**
 * 质量分析
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MesAnalysisActivity extends WorkToolBarActivity {

    @BindView(R.id.tv_first_start)
    TextView tvFirstStart;
    @BindView(R.id.tv_first_end)
    TextView tvFirstEnd;
    @BindView(R.id.ll_time)
    LinearLayout llTime;
    @BindView(R.id.rv_top)
    RecyclerView rvTop;
    @BindView(R.id.rv_pro_bad)
    RecyclerView rvProBad;
    @BindView(R.id.ll_report)
    LinearLayout llReport;
    private AnalysisTopAdapter topAdapter;
    private AnalysisProBadAdapter proBadAdapter;
    private List<AnalysisProBean> proBadList;
    private List<AnalysisProBean> 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 AnalysisTopAdapter(R.layout.item_mes_report_top, topList);
        proBadAdapter = new AnalysisProBadAdapter(R.layout.item_mes_analysis_pro_bad, proBadList);
        topAdapter.bindToRecyclerView(rvTop);
        proBadAdapter.bindToRecyclerView(rvProBad);
        param = WorkUtils.simpleParam();
        getData();
    }


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

    }


    @Override
    protected void initEvent() {
    }

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


    @OnClick({ R.id.tv_first_start, R.id.tv_first_end})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_first_start:
                PickTimeUtils.showPickThree(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        String time = TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM-dd"));
                        param.put("startDateTime", time);
                        tvFirstStart.setText(time);
                        getData();
                    }
                }, false).show();
                break;
            case R.id.tv_first_end:
                PickTimeUtils.showPickThree(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        String time = TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM-dd"));
                        param.put("lastDateTime", time);
                        tvFirstEnd.setText(time);
                        getData();
                    }
                }, false).show();
                break;

        }
    }


}
