package com.casic.dcms.view.statistics;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.casic.dcms.R;
import com.casic.dcms.adapter.NormalRecyclerAdapter;
import com.casic.dcms.databinding.ActivityPunishAnalysisBinding;
import com.casic.dcms.model.PunishRankingBean;
import com.casic.dcms.model.PunishTypeStatisticBean;
import com.casic.dcms.utils.ChartViewHelper;
import com.casic.dcms.utils.ColorKit;
import com.casic.dcms.utils.LocaleConstant;
import com.casic.dcms.utils.TimeOrDateUtil;
import com.casic.dcms.utils.ViewGroupKit;
import com.casic.dcms.vm.DataAnalysisViewModel;
import com.casic.dcms.widgets.RangeDateActionSheet;
import com.github.mikephil.charting.data.BarEntry;
import com.pengxh.androidx.lite.adapter.ViewHolder;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.divider.RecyclerViewItemDivider;
import com.pengxh.androidx.lite.kit.LongKit;
import com.pengxh.androidx.lite.utils.LoadState;
import com.pengxh.androidx.lite.utils.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

public class PunishAnalysisActivity extends AndroidxBaseActivity<ActivityPunishAnalysisBinding> {

    private static final String TAG = "PunishAnalysisActivity";
    private final Context context = PunishAnalysisActivity.this;
    private DataAnalysisViewModel dataAnalysisViewModel;

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

        binding.titleBarLayout.titleView.setText(LocaleConstant.ANALYSIS_ITEMS.get(6));
        binding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        dataAnalysisViewModel = new ViewModelProvider(this).get(DataAnalysisViewModel.class);
        dataAnalysisViewModel.typeStatisticData.observe(this, new Observer<PunishTypeStatisticBean>() {
            @Override
            public void onChanged(PunishTypeStatisticBean typeStatisticBean) {
                if (typeStatisticBean.getCode() == 200) {
                    List<BarEntry> barEntries = new ArrayList<>();
                    PunishTypeStatisticBean.DataModel dataModel = typeStatisticBean.getData();
                    barEntries.add(new BarEntry(0, dataModel.getPunish()));
                    barEntries.add(new BarEntry(1, dataModel.getWarn()));
                    barEntries.add(new BarEntry(2, dataModel.getPersuade()));
                    ChartViewHelper.setBarChartData(binding.barChart, barEntries);
                }
            }
        });

        dataAnalysisViewModel.rankingData.observe(this, new Observer<PunishRankingBean>() {
            @Override
            public void onChanged(PunishRankingBean rankingBean) {
                if (rankingBean.getCode() == 200) {
                    NormalRecyclerAdapter<PunishRankingBean.DataModel> recyclerAdapter = new NormalRecyclerAdapter<PunishRankingBean.DataModel>(R.layout.item_case_resource_lv_l, rankingBean.getData()) {
                        @Override
                        public void convertView(ViewHolder viewHolder, int position, PunishRankingBean.DataModel item) {
                            //设置随机颜色
                            int[] color = ColorKit.generateColor(context);

                            viewHolder.setText(R.id.numberTagView, String.valueOf(position + 1))
                                    .setBackgroundColor(R.id.numberTagView, color[0])
                                    .setTextColor(R.id.numberTagView, color[1])
                                    .setText(R.id.caseSourceView, item.getMerchantName())
                                    .setText(R.id.caseNumView, item.getNum());
                        }
                    };
                    binding.rankingRecyclerView.addItemDecoration(new RecyclerViewItemDivider(1, Color.LTGRAY));
                    binding.rankingRecyclerView.setAdapter(recyclerAdapter);
                }
            }
        });
    }

    @Override
    protected void observeRequestState() {
        dataAnalysisViewModel.loadState.observe(this, new Observer<LoadState>() {
            @Override
            public void onChanged(LoadState loadState) {
                if (loadState == LoadState.Loading) {
                    LoadingDialog.show(PunishAnalysisActivity.this, "数据加载中，请稍后");
                } else {
                    LoadingDialog.dismiss();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        //默认加载当前时间往前一个月的
        dataAnalysisViewModel.getPunishTypeStatistic(this, TimeOrDateUtil.getOneMonthDate(), LongKit.timestampToDate(System.currentTimeMillis()));
        dataAnalysisViewModel.getPunishRanking(this, TimeOrDateUtil.getOneMonthDate(), LongKit.timestampToDate(System.currentTimeMillis()));
    }

    @Override
    protected void initEvent() {
        binding.selectCalendarButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new RangeDateActionSheet.Builder().setContext(context).setOnActionSheetListener(new RangeDateActionSheet.OnDateRangeSelectedListener() {
                    @Override
                    public void onDateRangeSelected(String startDate, String endDate) {
                        dataAnalysisViewModel.getPunishTypeStatistic(context, startDate, endDate);
                        dataAnalysisViewModel.getPunishRanking(context, startDate, endDate);
                    }
                }).build().show();
            }
        });

        binding.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.oneMonthRadioButton) {
                    dataAnalysisViewModel.getPunishTypeStatistic(context, TimeOrDateUtil.getOneMonthDate(), LongKit.timestampToDate(System.currentTimeMillis()));
                    dataAnalysisViewModel.getPunishRanking(context, TimeOrDateUtil.getOneMonthDate(), LongKit.timestampToDate(System.currentTimeMillis()));
                } else if (checkedId == R.id.threeMonthRadioButton) {
                    dataAnalysisViewModel.getPunishTypeStatistic(context, TimeOrDateUtil.getThreeMonthDate(), LongKit.timestampToDate(System.currentTimeMillis()));
                    dataAnalysisViewModel.getPunishRanking(context, TimeOrDateUtil.getThreeMonthDate(), LongKit.timestampToDate(System.currentTimeMillis()));
                } else {
                    dataAnalysisViewModel.getPunishTypeStatistic(context, TimeOrDateUtil.getSixMonthDate(), LongKit.timestampToDate(System.currentTimeMillis()));
                    dataAnalysisViewModel.getPunishRanking(context, TimeOrDateUtil.getSixMonthDate(), LongKit.timestampToDate(System.currentTimeMillis()));
                }
            }
        });
    }
}
