package com.casic.dcms.view;

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

import androidx.annotation.Nullable;
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.utils.ViewGroupKit;
import com.casic.dcms.vm.CommonDataViewModel;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.kit.IntKit;
import com.pengxh.androidx.lite.kit.LongKit;
import com.pengxh.androidx.lite.kit.StringKit;
import com.pengxh.androidx.lite.widget.dialog.AlertControlDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;

import java.util.ArrayList;
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 = new ArrayList<>();
    private List<CaseSmallTypeBean.DataBean> smallTypeBeans = new ArrayList<>();
    private CommonDataViewModel commonDataViewModel;
    private String eorc = "";
    private String typeCode = "";
    private String typeDetailCode = "";
    private String caseState = "";

    @Override
    protected void setupTopBarLayout() {
        ViewGroupKit.initImmersionBar(binding.rootView, this, true, R.color.white);

        binding.titleBarLayout.titleView.setText("案卷查询");
        binding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
        binding.titleBarLayout.rightTextView.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 = "";

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

                            @Override
                            public void onCancelClick() {

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

    @Override
    protected void observeRequestState() {

    }

    @Override
    public void initOnCreate(@Nullable Bundle savedInstanceState) {
        commonDataViewModel = new ViewModelProvider(this).get(CommonDataViewModel.class);
        commonDataViewModel.largeTypeModel.observe(this, new Observer<CaseLargeTypeBean>() {
            @Override
            public void onChanged(CaseLargeTypeBean typeBean) {
                if (typeBean.getCode() == 200) {
                    //设置大类数据
                    largeTypeBeans = typeBean.getData();
                }
            }
        });
        commonDataViewModel.smallTypeModel.observe(this, new Observer<CaseSmallTypeBean>() {
            @Override
            public void onChanged(CaseSmallTypeBean typeBean) {
                if (typeBean.getCode() == 200) {
                    //设置大类数据
                    smallTypeBeans = typeBean.getData();
                }
            }
        });
        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();
                            binding.caseStatusView.setText(tag);
                            binding.caseStatusView.setTextColor(IntKit.convertColor(context, R.color.mainTextColor));
                        }
                    }).build().show();
                }
            }
        });
    }

    @Override
    public void initEvent() {
        binding.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();
                                binding.caseClassView.setText(tag);
                                binding.caseClassView.setTextColor(IntKit.convertColor(context, R.color.mainTextColor));

                                eorc = String.valueOf(position + 1);
                                //获取案卷大类
                                commonDataViewModel.getLargeType(context, eorc);
                            }
                        }).build().show();
            }
        });

        binding.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();

                            binding.caseLargeClassView.setText(tag);
                            binding.caseLargeClassView.setTextColor(IntKit.convertColor(context, R.color.mainTextColor));

                            //获取案卷小类
                            CaseLargeTypeBean.DataBean dataBean = largeTypeBeans.get(position);
                            String typeId = dataBean.getId();
                            commonDataViewModel.getSmallType(context, eorc, typeId);
                            typeCode = dataBean.getTypeCode();
                        }
                    }).build().show();
                } else {
                    StringKit.show(context, "请先选择案卷类型");
                }
            }
        });

        binding.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();

                            binding.caseSmallClassView.setText(tag);
                            binding.caseSmallClassView.setTextColor(IntKit.convertColor(context, R.color.mainTextColor));
                            typeDetailCode = smallTypeBeans.get(position).getTypeDetailCode();
                        }
                    }).build().show();
                } else {
                    StringKit.show(context, "请先选择案卷大类");
                }
            }
        });

        binding.caseStatusLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commonDataViewModel.getCaseStatus(context);
            }
        });


        binding.selectStartDateView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TimePickerDialog.Builder().setThemeColor(IntKit.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 = LongKit.timestampToDate(millSeconds);

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

        binding.selectEndDateView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TimePickerDialog.Builder().setThemeColor(IntKit.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 = LongKit.timestampToDate(millSeconds);

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

        binding.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", binding.startTimeView.getText().toString());
                intent.putExtra("endTime", binding.endTimeView.getText().toString());
                startActivity(intent);
            }
        });
    }
}
