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.ExcelDataBean;
import com.casic.dcms.bean.HighEventBean;
import com.casic.dcms.databinding.ActivityHighEventBinding;
import com.casic.dcms.mvp.presenter.impl.HighEventPresenterImpl;
import com.casic.dcms.mvp.view.IHighEventView;
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 8:44
 * @email : 290677893@qq.com
 */
public class HighEventListActivity extends AndroidxBaseActivity<ActivityHighEventBinding> implements IHighEventView {

    private static final String TAG = "HighEventListActivity";
    private final List<ExcelDataBean> excelDataBeans = new ArrayList<>();
    private final Context context = HighEventListActivity.this;
    private HighEventPresenterImpl highEventPresenter;

    @Override
    protected void setupTopBarLayout() {

        viewBinding.titleBarLayout.titleView.setText("高发问题");
        viewBinding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

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

    private void initTableConfig() {
        TableConfig config = viewBinding.eventTableView.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
    public void obtainHighEventResult(HighEventBean resultBean) {
        if (resultBean.getCode() == 200) {
            List<HighEventBean.DataBean> dataBeanList = resultBean.getData();
            for (int i = 0; i < dataBeanList.size() - 1; i++) {
                HighEventBean.DataBean dataBean = dataBeanList.get(i);
                String caseNum = dataBean.getCaseNum();
                excelDataBeans.add(new ExcelDataBean(i, dataBean.getCaseType(), Integer.parseInt(caseNum)));
            }
            viewBinding.eventTableView.setData(excelDataBeans);
        }
    }

    @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);
                                highEventPresenter.onReadyRetrofitRequest(date[0], date[1]);
                            }
                        }).build().show(getSupportFragmentManager(), "year_month");
            }
        });
    }

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