package com.casic.dcms.view;

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

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.casic.dcms.R;
import com.casic.dcms.databinding.ActivityCaseSearchBinding;
import com.casic.dcms.model.CaseLargeTypeBean;
import com.casic.dcms.model.CaseSmallTypeBean;
import com.casic.dcms.model.CaseStatusBean;
import com.casic.dcms.utils.LocaleConstant;
import com.casic.dcms.vm.CommonDataViewModel;
import com.gyf.immersionbar.ImmersionBar;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.hub.IntHub;
import com.pengxh.androidx.lite.hub.LongHub;
import com.pengxh.androidx.lite.utils.ImmerseStatusBarManager;
import com.pengxh.androidx.lite.widget.dialog.AlertControlDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;

import java.util.List;

public class SearchCaseActivity extends AndroidxBaseActivity<ActivityCaseSearchBinding> {

    private static final String TAG = "CaseSearchActivity";
    private final Context context = this;
    private List<CaseLargeTypeBean.DataBean> largeTypeBeans;
    private List<CaseSmallTypeBean.DataBean> smallTypeBeans;
    private CommonDataViewModel commonDataViewModel;
    private String eorc = "";
    private String typeCode = "";
    private String typeDetailCode = "";
    private String caseState = "";

    @Override
    protected void setupTopBarLayout() {
        ImmerseStatusBarManager.setColor(this, IntHub.convertColor(this, R.color.white));
        ImmersionBar.with(this).statusBarDarkFont(true).init();

        viewBinding.titleBarLayout.titleView.setText("案卷查询");
        viewBinding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    public void initData() {
        commonDataViewModel = new ViewModelProvider(this).get(CommonDataViewModel.class);
    }

    @Override
    public void initEvent() {
        viewBinding.caseClassLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new QMUIBottomSheet.BottomListSheetBuilder(context)
                        .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(IntHub.convertColor(context, R.color.mainTextColor));

                                eorc = String.valueOf(position + 1);
                                //获取案卷大类
                                commonDataViewModel.obtainLargeType(eorc);
                            }
                        }).build().show();
            }
        });
        commonDataViewModel.largeTypeModel.observe(this, new Observer<CaseLargeTypeBean>() {
            @Override
            public void onChanged(CaseLargeTypeBean typeBean) {
                if (typeBean.getCode() == 200) {
                    //设置大类数据
                    largeTypeBeans = typeBean.getData();
                    if (largeTypeBeans.size() == 0) {
                        viewBinding.caseLargeClassView.setText("无小类");
                    } else {
                        viewBinding.caseLargeClassView.setText(largeTypeBeans.get(0).getTypeName());//默认选择第一个
                    }
                    viewBinding.caseSmallClassView.setText("请选择");
                }
            }
        });

        viewBinding.caseLargeClassLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (largeTypeBeans.size() > 1) {
                    QMUIBottomSheet.BottomListSheetBuilder sheetBuilder = new QMUIBottomSheet.BottomListSheetBuilder(context);
                    sheetBuilder.setTitle("案卷大类");
                    for (CaseLargeTypeBean.DataBean dataBean : largeTypeBeans) {
                        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(IntHub.convertColor(context, R.color.mainTextColor));

                            //获取案卷小类
                            CaseLargeTypeBean.DataBean dataBean = largeTypeBeans.get(position);
                            String typeId = dataBean.getId();
                            commonDataViewModel.obtainSmallType(eorc, typeId);
                            typeCode = dataBean.getTypeCode();
                        }
                    }).build().show();
                }
            }
        });
        commonDataViewModel.smallTypeModel.observe(this, new Observer<CaseSmallTypeBean>() {
            @Override
            public void onChanged(CaseSmallTypeBean typeBean) {
                if (typeBean.getCode() == 200) {
                    //设置大类数据
                    smallTypeBeans = typeBean.getData();
                    if (smallTypeBeans.size() == 0) {
                        viewBinding.caseSmallClassView.setText("无小类");
                    } else {
                        viewBinding.caseSmallClassView.setText(smallTypeBeans.get(0).getTypeDetailName());//默认选择第一个
                    }
                }
            }
        });

        viewBinding.caseSmallClassLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (smallTypeBeans.size() > 1) {
                    QMUIBottomSheet.BottomListSheetBuilder sheetBuilder = new QMUIBottomSheet.BottomListSheetBuilder(context);
                    sheetBuilder.setTitle("案卷小类");
                    for (CaseSmallTypeBean.DataBean dataBean : smallTypeBeans) {
                        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(IntHub.convertColor(context, R.color.mainTextColor));
                            typeDetailCode = smallTypeBeans.get(position).getTypeDetailCode();
                        }
                    }).build().show();
                }
            }
        });

        viewBinding.caseStatusLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commonDataViewModel.obtainCaseStatus();
            }
        });
        commonDataViewModel.caseStatusModel.observe(this, new Observer<CaseStatusBean>() {
            @Override
            public void onChanged(CaseStatusBean resultBean) {
                if (resultBean.getCode() == 200) {
                    List<CaseStatusBean.DataBean> beans = resultBean.getData();
                    QMUIBottomSheet.BottomListSheetBuilder sheetBuilder = new QMUIBottomSheet.BottomListSheetBuilder(context);
                    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(IntHub.convertColor(context, R.color.mainTextColor));
                        }
                    }).build().show();
                }
            }
        });

        viewBinding.selectStartDateView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TimePickerDialog.Builder().setThemeColor(IntHub.convertColor(context, R.color.mainThemeColor))
                        .setTitleStringId("请选择处理时间")
                        .setWheelItemTextSize(16)
                        .setCyclic(false)
                        .setMinMillseconds(System.currentTimeMillis() - LocaleConstant.FIVE_YEARS)
                        .setMaxMillseconds(System.currentTimeMillis())
                        .setType(Type.YEAR_MONTH_DAY)
                        .setCallBack((timePickerView, millSeconds) -> {
                            String date = LongHub.timestampToDate(millSeconds);

                            viewBinding.startTimeView.setText(date);
                            viewBinding.startTimeView.setTextColor(IntHub.convertColor(context, R.color.mainTextColor));
                        }).build().show(getSupportFragmentManager(), "year_month_day");
            }
        });

        viewBinding.selectEndDateView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TimePickerDialog.Builder().setThemeColor(IntHub.convertColor(context, R.color.mainThemeColor))
                        .setTitleStringId("请选择截止时间")
                        .setWheelItemTextSize(16)
                        .setCyclic(false)
                        .setMinMillseconds(System.currentTimeMillis() - LocaleConstant.FIVE_YEARS)
                        .setMaxMillseconds(System.currentTimeMillis())
                        .setType(Type.YEAR_MONTH_DAY)
                        .setCallBack((timePickerView, millSeconds) -> {
                            String date = LongHub.timestampToDate(millSeconds);

                            viewBinding.endTimeView.setText(date);
                            viewBinding.endTimeView.setTextColor(IntHub.convertColor(context, R.color.mainTextColor));
                        }).build().show(getSupportFragmentManager(), "year_month_day");
            }
        });

        viewBinding.resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertControlDialog.Builder()
                        .setContext(context)
                        .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();
            }
        });

        viewBinding.searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, CaseSearchResultActivity.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);
            }
        });
    }
}
