package com.casic.dcms.ui;

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

import androidx.core.content.ContextCompat;

import com.casic.dcms.R;
import com.casic.dcms.base.AndroidxBaseActivity;
import com.casic.dcms.bean.CaseLargeClassBean;
import com.casic.dcms.bean.CaseSmallClassBean;
import com.casic.dcms.bean.CaseStatusBean;
import com.casic.dcms.databinding.ActivityCaseSearchBinding;
import com.casic.dcms.mvp.presenter.impl.CaseLargeClassPresenterImpl;
import com.casic.dcms.mvp.presenter.impl.CaseSmallClassPresenterImpl;
import com.casic.dcms.mvp.presenter.impl.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.Constant;
import com.casic.dcms.utils.TimeOrDateUtil;
import com.casic.dcms.widgets.AlertControlDialog;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;

import java.util.List;

public class CaseSearchActivity extends AndroidxBaseActivity<ActivityCaseSearchBinding> implements View.OnClickListener,
        ICaseLargeClassView, ICaseSmallClassView, ICaseStatusView {

    private static final String TAG = "CaseSearchActivity";
    private final Context context = this;
    private CaseLargeClassPresenterImpl caseLargeClassPresenter;
    private List<CaseLargeClassBean.DataBean> largeClassBeans;
    private CaseSmallClassPresenterImpl caseSmallClassPresenter;
    private List<CaseSmallClassBean.DataBean> smallClassBeans;
    private CaseStatusPresenterImpl caseStatusPresenter;
    private String eorc = "";
    private String typeCode = "";
    private String typeDetailCode = "";
    private String caseState = "";

    @Override
    protected void setupTopBarLayout() {
        viewBinding.titleBarLayout.titleView.setText("案卷查询");
        viewBinding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

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

    @Override
    public void initEvent() {
        viewBinding.caseClassLayout.setOnClickListener(this);
        viewBinding.caseLargeClassLayout.setOnClickListener(this);
        viewBinding.caseSmallClassLayout.setOnClickListener(this);
        viewBinding.caseStatusLayout.setOnClickListener(this);
        viewBinding.selectStartDateView.setOnClickListener(this);
        viewBinding.selectEndDateView.setOnClickListener(this);
        viewBinding.searchButton.setOnClickListener(this);
        viewBinding.resetButton.setOnClickListener(this);

        viewBinding.searchButton.setChangeAlphaWhenPress(true);
        viewBinding.resetButton.setChangeAlphaWhenPress(true);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == 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();
                            viewBinding.caseClassView.setText(tag);
                            viewBinding.caseClassView.setTextColor(ContextCompat.getColor(context, R.color.textColor));

                            eorc = String.valueOf(position + 1);
                            //获取案卷大类
                            caseLargeClassPresenter.onReadyRetrofitRequest(eorc);
                        }
                    }).build().show();
        } else if (id == 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();

                            viewBinding.caseLargeClassView.setText(tag);
                            viewBinding.caseLargeClassView.setTextColor(ContextCompat.getColor(context, R.color.textColor));

                            //获取案卷小类
                            CaseLargeClassBean.DataBean dataBean = largeClassBeans.get(position);
                            String typeId = dataBean.getId();
                            caseSmallClassPresenter.onReadyRetrofitRequest(eorc, typeId);
                            typeCode = dataBean.getTypeCode();
                        }
                    }).build().show();
                }
            }
        } else if (id == 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();

                            viewBinding.caseSmallClassView.setText(tag);
                            viewBinding.caseSmallClassView.setTextColor(ContextCompat.getColor(context, R.color.textColor));
                            typeDetailCode = smallClassBeans.get(position).getTypeDetailCode();
                        }
                    }).build().show();
                }
            }
        } else if (id == R.id.caseStatusLayout) {
            caseStatusPresenter.onReadyRetrofitRequest();
        } else if (id == R.id.selectStartDateView) {
            new TimePickerDialog.Builder().setThemeColor(ContextCompat.getColor(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.timestampToDate(millSeconds);

                        viewBinding.startTimeView.setText(date);
                        viewBinding.startTimeView.setTextColor(ContextCompat.getColor(context, R.color.textColor));
                    }).build().show(getSupportFragmentManager(), "year_month_day");
        } else if (id == R.id.selectEndDateView) {
            new TimePickerDialog.Builder().setThemeColor(ContextCompat.getColor(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.timestampToDate(millSeconds);

                        viewBinding.endTimeView.setText(date);
                        viewBinding.endTimeView.setTextColor(ContextCompat.getColor(context, R.color.textColor));
                    }).build().show(getSupportFragmentManager(), "year_month_day");
        } else if (id == R.id.searchButton) {
            Intent intent = new Intent(this, CaseListResultActivity.class);
            intent.putExtra("eorc", eorc);
            intent.putExtra("typeCode", typeCode);
            intent.putExtra("typeDetailCode", typeDetailCode);
            intent.putExtra("caseState", caseState);
            intent.putExtra("startTime", viewBinding.startTimeView.getText().toString());
            intent.putExtra("endTime", viewBinding.endTimeView.getText().toString());
            startActivity(intent);
        } else if (id == R.id.resetButton) {
            new AlertControlDialog.Builder()
                    .setContext(this)
                    .setTitle("重置")
                    .setMessage("确定要重置填写的资料吗？")
                    .setNegativeButton("取消")
                    .setPositiveButton("确定")
                    .setOnDialogButtonClickListener(new AlertControlDialog.OnDialogButtonClickListener() {
                        @Override
                        public void onConfirmClick() {
                            //将搜索条件置空
                            eorc = "";
                            typeCode = "";
                            typeDetailCode = "";
                            caseState = "";

                            //将界面显示置空
                            viewBinding.caseClassView.setText("");
                            viewBinding.caseLargeClassView.setText("");
                            viewBinding.caseSmallClassView.setText("");
                            viewBinding.caseStatusView.setText("");
                            viewBinding.startTimeView.setText("");
                            viewBinding.endTimeView.setText("");
                        }

                        @Override
                        public void onCancelClick() {

                        }
                    }).build().show();
        }
    }

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

            viewBinding.caseSmallClassView.setText("请选择");
            viewBinding.caseSmallClassView.setTextColor(ContextCompat.getColor(this, R.color.darkGray));
        }
    }

    @Override
    public void obtainSmallClassData(CaseSmallClassBean smallClassBean) {
        if (smallClassBean.isSuccess()) {
            //设置大类数据
            this.smallClassBeans = smallClassBean.getData();
            if (smallClassBeans.size() == 0) {
                viewBinding.caseSmallClassView.setText("无小类");
            } else {
                viewBinding.caseSmallClassView.setText(smallClassBeans.get(0).getTypeDetailName());//默认选择第一个
            }
            viewBinding.caseSmallClassView.setTextColor(ContextCompat.getColor(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();
                    viewBinding.caseStatusView.setText(tag);
                    viewBinding.caseStatusView.setTextColor(ContextCompat.getColor(context, R.color.textColor));
                }
            }).build().show();
        }
    }

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