package com.casic.dcms.view.statistics;

import android.content.Context;
import android.view.View;
import android.widget.RadioGroup;

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

import com.casic.dcms.R;
import com.casic.dcms.databinding.ActivityHighEventBinding;
import com.casic.dcms.model.HighEventBean;
import com.casic.dcms.utils.ChartViewHelper;
import com.casic.dcms.utils.ColorHub;
import com.casic.dcms.utils.DialogHelper;
import com.casic.dcms.utils.LocaleConstant;
import com.casic.dcms.utils.TimeOrDateUtil;
import com.casic.dcms.vm.DataAnalysisViewModel;
import com.casic.dcms.widgets.RangeDateActionSheet;
import com.github.mikephil.charting.data.PieEntry;
import com.gyf.immersionbar.ImmersionBar;
import com.pengxh.androidx.lite.adapter.NormalRecyclerAdapter;
import com.pengxh.androidx.lite.adapter.ViewHolder;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.divider.ItemDecoration;
import com.pengxh.androidx.lite.hub.FloatHub;
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.vm.LoadState;

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

/**
 * @author Pengxh
 * @time : 2021/4/16 8:44
 * @email : 290677893@qq.com
 */
public class HighEventActivity extends AndroidxBaseActivity<ActivityHighEventBinding> {

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

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

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

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

    @Override
    protected void initData() {
        dataAnalysisViewModel = new ViewModelProvider(this).get(DataAnalysisViewModel.class);
        dataAnalysisViewModel.highEventModel.observe(this, new Observer<HighEventBean>() {
            @Override
            public void onChanged(HighEventBean resultBean) {
                if (resultBean.getCode() == 200) {
                    int caseCount = 0;
                    List<HighEventBean.DataModel> dataModels = resultBean.getData();
                    List<PieEntry> pieEntries = new ArrayList<>();
                    List<HighEventBean.DataModel> effectiveModels = new ArrayList<>();
                    for (HighEventBean.DataModel dataModel : dataModels) {
                        if (!dataModel.getCaseMajorType().equals("")) {
                            effectiveModels.add(dataModel);

                            caseCount += Integer.parseInt(dataModel.getCaseNum());

                            if (!dataModel.getCaseNum().equals("0")) {
                                pieEntries.add(new PieEntry(Float.parseFloat(dataModel.getCaseNum()), dataModel.getCaseType()));
                            }
                        }
                    }
                    viewBinding.caseCountView.setText("案卷数：" + caseCount);

                    //数据绑定饼图
                    ChartViewHelper.setPieChartData(viewBinding.resourcePieChart, pieEntries);

                    //绑定列表
                    NormalRecyclerAdapter<HighEventBean.DataModel> recyclerAdapter = new NormalRecyclerAdapter<HighEventBean.DataModel>(R.layout.item_case_resource_lv_l, effectiveModels) {
                        @Override
                        public void convertView(ViewHolder viewHolder, int position, HighEventBean.DataModel item) {
                            //设置随机颜色
                            int[] color = ColorHub.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.getCaseType())
                                    .setText(R.id.caseNumView, item.getCaseNum());
                        }
                    };
                    viewBinding.highEventCaseView.addItemDecoration(new ItemDecoration(FloatHub.dp2px(context, 75), FloatHub.dp2px(context, 15)));
                    viewBinding.highEventCaseView.setAdapter(recyclerAdapter);
                }
            }
        });
    }

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

    @Override
    protected void initEvent() {
        viewBinding.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.getHighFrequencyCase(startDate, endDate);
                    }
                }).build().show();
            }
        });

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