package com.casic.dcms.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.casic.dcms.R;
import com.casic.dcms.base.BaseActivity;
import com.casic.dcms.bean.CaseLargeClassBean;
import com.casic.dcms.bean.CaseSmallClassBean;
import com.casic.dcms.bean.CaseStatusBean;
import com.casic.dcms.mvp.presenter.CaseLargeClassPresenterImpl;
import com.casic.dcms.mvp.presenter.CaseSmallClassPresenterImpl;
import com.casic.dcms.mvp.presenter.CaseStatusPresenterImpl;
import com.casic.dcms.mvp.view.ICaseLargeClassView;
import com.casic.dcms.mvp.view.ICaseSmallClassView;
import com.casic.dcms.mvp.view.ICaseStatusView;
import com.casic.dcms.utils.ColorHelper;
import com.casic.dcms.utils.Constant;
import com.casic.dcms.utils.TimeOrDateUtil;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CaseSearchActivity extends BaseActivity implements View.OnClickListener,
        ICaseLargeClassView, ICaseSmallClassView, ICaseStatusView {

    private static final String TAG = "CaseSearchActivity";
    private Context context = this;
    @BindView(R.id.caseTopLayout)
    QMUITopBarLayout caseTopLayout;
    @BindView(R.id.caseClassView)
    TextView caseClassView;
    @BindView(R.id.caseLargeClassView)
    TextView caseLargeClassView;
    @BindView(R.id.caseSmallClassView)
    TextView caseSmallClassView;
    @BindView(R.id.caseStatusView)
    TextView caseStatusView;
    @BindView(R.id.startTimeView)
    TextView startTimeView;
    @BindView(R.id.endTimeView)
    TextView endTimeView;
    @BindView(R.id.searchButton)
    QMUIRoundButton searchButton;
    @BindView(R.id.resetButton)
    QMUIRoundButton resetButton;

    private CaseLargeClassPresenterImpl caseLargeClassPresenter;
    private List<CaseLargeClassBean.DataBean> largeClassBeans;
    private CaseSmallClassPresenterImpl caseSmallClassPresenter;
    private List<CaseSmallClassBean.DataBean> smallClassBeans;
    private CaseStatusPresenterImpl caseStatusPresenter;
    private String typeCode;
    private String typeDetailCode;
    private String caseState;
    private String eorc;

    @Override
    public int initLayoutView() {
        return R.layout.activity_case_search;
    }

    @Override
    protected void setupTopBarLayout() {
        caseTopLayout.setTitle("案卷查询").setTextColor(ContextCompat.getColor(this, R.color.white));
        caseTopLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.mainThemeColor));
        caseTopLayout.addLeftImageButton(R.drawable.ic_left_white, 1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData() {
        caseLargeClassPresenter = new CaseLargeClassPresenterImpl(this);
        caseSmallClassPresenter = new CaseSmallClassPresenterImpl(this);
        caseStatusPresenter = new CaseStatusPresenterImpl(this);
    }

    @Override
    public void initEvent() {
        searchButton.setChangeAlphaWhenPress(true);
        resetButton.setChangeAlphaWhenPress(true);
    }

    @OnClick({R.id.caseClassLayout, R.id.caseLargeClassLayout, R.id.caseSmallClassLayout, R.id.caseStatusLayout,
            R.id.selectStartDateView, R.id.selectEndDateView, R.id.searchButton, R.id.resetButton})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.caseClassLayout:
                new QMUIBottomSheet.BottomListSheetBuilder(this)
                        .setTitle("案卷类型")
                        .addItem("事件")
                        .addItem("部件")
                        .setGravityCenter(true)
                        .setOnSheetItemClickListener(new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                            @Override
                            public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
                                dialog.dismiss();
                                caseClassView.setText(tag);
                                caseClassView.setTextColor(ColorHelper.getResourcesColor(context, R.color.textColor));

                                //获取案卷大类
                                caseLargeClassPresenter.onReadyRetrofitRequest(String.valueOf(position + 1));
                            }
                        }).build().show();
                break;
            case R.id.caseLargeClassLayout:
                if (largeClassBeans != null) {
                    if (largeClassBeans.size() > 1) {
                        QMUIBottomSheet.BottomListSheetBuilder sheetBuilder = new QMUIBottomSheet.BottomListSheetBuilder(this);
                        sheetBuilder.setTitle("案卷大类");
                        for (CaseLargeClassBean.DataBean dataBean : largeClassBeans) {
                            sheetBuilder.addItem(dataBean.getTypeName());
                        }
                        sheetBuilder.setGravityCenter(true).setOnSheetItemClickListener(new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                            @Override
                            public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
                                dialog.dismiss();

                                caseLargeClassView.setText(tag);
                                caseLargeClassView.setTextColor(ColorHelper.getResourcesColor(context, R.color.textColor));

                                //获取案卷小类
                                CaseLargeClassBean.DataBean dataBean = largeClassBeans.get(position);
                                eorc = dataBean.getEorc();
                                String typeId = dataBean.getId();
                                caseSmallClassPresenter.onReadyRetrofitRequest(eorc, typeId);
                                typeCode = dataBean.getTypeCode();
                            }
                        }).build().show();
                    }
                }
                break;
            case R.id.caseSmallClassLayout:
                if (smallClassBeans != null) {
                    if (smallClassBeans.size() > 1) {
                        QMUIBottomSheet.BottomListSheetBuilder sheetBuilder = new QMUIBottomSheet.BottomListSheetBuilder(this);
                        sheetBuilder.setTitle("案卷小类");
                        for (CaseSmallClassBean.DataBean dataBean : smallClassBeans) {
                            sheetBuilder.addItem(dataBean.getTypeDetailName());
                        }
                        sheetBuilder.setGravityCenter(true).setOnSheetItemClickListener(new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                            @Override
                            public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
                                dialog.dismiss();

                                caseSmallClassView.setText(tag);
                                caseSmallClassView.setTextColor(ColorHelper.getResourcesColor(context, R.color.textColor));
                                typeDetailCode = smallClassBeans.get(position).getTypeDetailCode();
                            }
                        }).build().show();
                    }
                }
                break;
            case R.id.caseStatusLayout:
                caseStatusPresenter.onReadyRetrofitRequest();
                break;
            case R.id.selectStartDateView:
                new TimePickerDialog.Builder().setThemeColor(ColorHelper.getResourcesColor(this, R.color.mainThemeColor))
                        .setTitleStringId("请选择处理时间")
                        .setWheelItemTextSize(16)
                        .setCyclic(false)
                        .setMinMillseconds(System.currentTimeMillis() - Constant.FIVE_YEARS)
                        .setMaxMillseconds(System.currentTimeMillis())
                        .setType(Type.YEAR_MONTH_DAY)
                        .setCallBack((timePickerView, millSeconds) -> {
                            String date = TimeOrDateUtil.rTimestampToDate(millSeconds);

                            startTimeView.setText(date);
                            startTimeView.setTextColor(ColorHelper.getResourcesColor(context, R.color.textColor));
                        }).build().show(getSupportFragmentManager(), "year_month_day");
                break;
            case R.id.selectEndDateView:
                new TimePickerDialog.Builder().setThemeColor(ColorHelper.getResourcesColor(this, R.color.mainThemeColor))
                        .setTitleStringId("请选择截止时间")
                        .setWheelItemTextSize(16)
                        .setCyclic(false)
                        .setMinMillseconds(System.currentTimeMillis() - Constant.FIVE_YEARS)
                        .setMaxMillseconds(System.currentTimeMillis())
                        .setType(Type.YEAR_MONTH_DAY)
                        .setCallBack((timePickerView, millSeconds) -> {
                            String date = TimeOrDateUtil.rTimestampToDate(millSeconds);

                            endTimeView.setText(date);
                            endTimeView.setTextColor(ColorHelper.getResourcesColor(context, R.color.textColor));
                        }).build().show(getSupportFragmentManager(), "year_month_day");
                break;
            case R.id.searchButton:
                Intent intent = new Intent(this, CaseListResultActivity.class);
                intent.putExtra("typeCode", typeCode);
                intent.putExtra("typeDetailCode", typeDetailCode);
                intent.putExtra("caseState", caseState);
                intent.putExtra("startTime", startTimeView.getText().toString());
                intent.putExtra("endTime", endTimeView.getText().toString());
                intent.putExtra("eorc", eorc);
                startActivity(intent);
                break;
            case R.id.resetButton:
                new QMUIDialog.MessageDialogBuilder(context)
                        .setTitle("重置")
                        .setMessage("确定要重置填写的资料吗？")
                        .setCanceledOnTouchOutside(false)
                        .addAction("取消", (dialog, index) -> dialog.dismiss())
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();

                                caseClassView.setText("");
                                caseLargeClassView.setText("");
                                caseSmallClassView.setText("");
                                caseStatusView.setText("");
                                startTimeView.setText("");
                                endTimeView.setText("");
                            }
                        }).create().show();
                break;
            default:
                break;
        }
    }

    @Override
    public void obtainLargeClassData(CaseLargeClassBean largeClassBean) {
        if (largeClassBean.isSuccess()) {
            //设置大类数据
            this.largeClassBeans = largeClassBean.getData();
            if (largeClassBeans.size() == 0) {
                caseLargeClassView.setText("无小类");
            } else {
                caseLargeClassView.setText(largeClassBeans.get(0).getTypeName());//默认选择第一个
            }
            caseLargeClassView.setTextColor(ColorHelper.getResourcesColor(this, R.color.darkGray));

            caseSmallClassView.setText("请选择");
            caseSmallClassView.setTextColor(ColorHelper.getResourcesColor(this, R.color.darkGray));
        }
    }

    @Override
    public void obtainSmallClassData(CaseSmallClassBean smallClassBean) {
        if (smallClassBean.isSuccess()) {
            //设置大类数据
            this.smallClassBeans = smallClassBean.getData();
            if (smallClassBeans.size() == 0) {
                caseSmallClassView.setText("无小类");
            } else {
                caseSmallClassView.setText(smallClassBeans.get(0).getTypeDetailName());//默认选择第一个
            }
            caseSmallClassView.setTextColor(ColorHelper.getResourcesColor(this, R.color.darkGray));
        }
    }

    @Override
    public void obtainCaseStatusData(CaseStatusBean resultBean) {
//        Log.d(TAG, "obtainCaseStatusData: " + new Gson().toJson(resultBean));
        if (resultBean.isSuccess()) {
            List<CaseStatusBean.DataBean> beans = resultBean.getData();
            QMUIBottomSheet.BottomListSheetBuilder sheetBuilder = new QMUIBottomSheet.BottomListSheetBuilder(this);
            sheetBuilder.setTitle("处理状态");
            for (CaseStatusBean.DataBean dataBean : beans) {
                sheetBuilder.addItem(dataBean.getName());
            }
            sheetBuilder.setGravityCenter(true).setOnSheetItemClickListener(new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                @Override
                public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
                    dialog.dismiss();

                    caseState = beans.get(position).getValue();
                    caseStatusView.setText(tag);
                    caseStatusView.setTextColor(ColorHelper.getResourcesColor(context, R.color.textColor));
                }
            }).build().show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (caseLargeClassPresenter != null) {
            caseLargeClassPresenter.disposeRetrofitRequest();
        }
        if (caseSmallClassPresenter != null) {
            caseSmallClassPresenter.disposeRetrofitRequest();
        }
        if (caseStatusPresenter != null) {
            caseStatusPresenter.disposeRetrofitRequest();
        }
    }
}
