package com.casic.dcms.view.statistics;

import android.content.Context;
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.databinding.ActivityTrendAnalysisBinding;
import com.casic.dcms.model.CaseTendencyBean;
import com.casic.dcms.utils.ChartViewHelper;
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.Entry;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
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 TrendAnalysisActivity extends AndroidxBaseActivity<ActivityTrendAnalysisBinding> {

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

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

        binding.leftBackView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

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

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        dataAnalysisViewModel = new ViewModelProvider(this).get(DataAnalysisViewModel.class);
        dataAnalysisViewModel.caseTendencyData.observe(this, new Observer<CaseTendencyBean>() {
            @Override
            public void onChanged(CaseTendencyBean caseTendencyBean) {
                if (caseTendencyBean.getCode() == 200) {
                    List<CaseTendencyBean.DataModel> dataModels = caseTendencyBean.getData();
                    List<String> dates = new ArrayList<>();
                    List<Entry> registeredEntries = new ArrayList<>();
                    List<Entry> distributeEntries = new ArrayList<>();
                    List<Entry> processEntries = new ArrayList<>();
                    List<Entry> closedEntries = new ArrayList<>();
                    for (int i = 0; i < dataModels.size(); i++) {
                        CaseTendencyBean.DataModel dataModel = dataModels.get(i);

                        dates.add(dataModel.getStatisticsTime());
                        registeredEntries.add(new Entry(i, Float.parseFloat(dataModel.getRegisteredNum()), "Registered"));
                        distributeEntries.add(new Entry(i, Float.parseFloat(dataModel.getDistributeNum()), "Distribute"));
                        processEntries.add(new Entry(i, Float.parseFloat(dataModel.getProcessNum()), "Process"));
                        closedEntries.add(new Entry(i, Float.parseFloat(dataModel.getClosedNum()), "Closed"));
                    }

                    ChartViewHelper.setLineChartData(binding.lineChart, dates, registeredEntries, distributeEntries, processEntries, closedEntries);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        //默认加载当前时间往前一个月的
        dataAnalysisViewModel.getCaseTendency(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.getCaseTendency(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.getCaseTendency(context, TimeOrDateUtil.getOneMonthDate(), LongKit.timestampToDate(System.currentTimeMillis()));
                } else if (checkedId == R.id.threeMonthRadioButton) {
                    dataAnalysisViewModel.getCaseTendency(context, TimeOrDateUtil.getThreeMonthDate(), LongKit.timestampToDate(System.currentTimeMillis()));
                } else {
                    dataAnalysisViewModel.getCaseTendency(context, TimeOrDateUtil.getSixMonthDate(), LongKit.timestampToDate(System.currentTimeMillis()));
                }
            }
        });
    }
}
