package com.casic.dcms.ui;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.bin.david.form.core.TableConfig;
import com.bin.david.form.data.style.FontStyle;
import com.casic.dcms.R;
import com.casic.dcms.base.AndroidxBaseActivity;
import com.casic.dcms.bean.EventSourceBean;
import com.casic.dcms.bean.ExcelDataBean;
import com.casic.dcms.databinding.ActivityEventResourceBinding;
import com.casic.dcms.mvp.presenter.impl.EventSourcePresenterImpl;
import com.casic.dcms.mvp.view.IEventSourceView;
import com.casic.dcms.utils.Constant;
import com.casic.dcms.utils.TimeOrDateUtil;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;

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

/**
 * @author : Pengxh
 * @time : 2021/4/16 9:24
 * @email : 290677893@qq.com
 */
public class EventSourceActivity extends AndroidxBaseActivity<ActivityEventResourceBinding> implements IEventSourceView {

    private static final String TAG = EventSourceActivity.class.getSimpleName();
    private final Context context = EventSourceActivity.this;
    private final List<ExcelDataBean> excelDataBeans = new ArrayList<>();
    private EventSourcePresenterImpl eventSourcePresenter;

    @Override
    protected void setupTopBarLayout() {
        viewBinding.titleBarLayout.titleView.setText("问题来源");
        viewBinding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

    @Override
    protected void initData() {
        //初始化表格
        initTableConfig();
        eventSourcePresenter = new EventSourcePresenterImpl(this);
        //默认显示当前月的数据
        long timeMillis = System.currentTimeMillis();
        viewBinding.titleBarLayout.dateView.setText(TimeOrDateUtil.timestampToYearMonth(timeMillis));
        String[] date = TimeOrDateUtil.timestampToMonth(timeMillis);
        eventSourcePresenter.onReadyRetrofitRequest(date[0], date[1]);
    }

    @Override
    public void obtainEventSourceResult(EventSourceBean resultBean) {
        if (resultBean.getCode() == 200) {
            List<EventSourceBean.DataBean> dataBeanList = resultBean.getData();
            for (int i = 0; i < dataBeanList.size() - 1; i++) {
                EventSourceBean.DataBean dataBean = dataBeanList.get(i);
                String caseNum = dataBean.getCaseNum();
                //表格数据需要把0的加进去，饼图不需要
                excelDataBeans.add(new ExcelDataBean(i, dataBean.getCaseSource(), Integer.parseInt(caseNum)));
            }
            viewBinding.resourceTableView.setData(excelDataBeans);
        }
    }

    private void initTableConfig() {
        TableConfig config = viewBinding.resourceTableView.getConfig();
        //首行属性
        config.setColumnTitleStyle(new FontStyle(50, Color.DKGRAY));
        config.setContentBackgroundColor(Color.LTGRAY);
        config.setColumnTitleVerticalPadding(30);
        //内容属性
        config.setContentStyle(new FontStyle(50, Color.DKGRAY));
        config.setVerticalPadding(30);
        config.setShowTableTitle(false); //不显示标题
        config.setShowXSequence(false); //不显示横向ABC....序列
        config.setShowYSequence(false); //不显示纵向123....序列
        config.setMinTableWidth(QMUIDisplayHelper.getScreenWidth(context));//不设置的话会就不会居中显示
    }

    @Override
    protected void initEvent() {
        viewBinding.titleBarLayout.rightImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new TimePickerDialog.Builder()
                        .setThemeColor(ContextCompat.getColor(context, R.color.mainThemeColor))
                        .setTitleStringId("选择日期")
                        .setWheelItemTextSize(16)
                        .setCyclic(false)
                        .setMinMillseconds(System.currentTimeMillis() - Constant.FIVE_YEARS)
                        .setMaxMillseconds(System.currentTimeMillis())
                        .setType(Type.YEAR_MONTH)
                        .setCallBack(new OnDateSetListener() {
                            @Override
                            public void onDateSet(TimePickerDialog timePickerView, long millSeconds) {
                                viewBinding.titleBarLayout.dateView.setText(TimeOrDateUtil.timestampToYearMonth(millSeconds));
                                String[] date = TimeOrDateUtil.timestampToMonth(millSeconds);
                                eventSourcePresenter.onReadyRetrofitRequest(date[0], date[1]);
                            }
                        }).build().show(getSupportFragmentManager(), "year_month");
            }
        });
    }

    @Override
    public void onDestroy() {
        eventSourcePresenter.disposeRetrofitRequest();
        super.onDestroy();
    }
}