package com.casic.dcms.ui.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;

import com.bin.david.form.core.TableConfig;
import com.bin.david.form.data.style.FontStyle;
import com.casic.dcms.base.AndroidxBaseFragment;
import com.casic.dcms.bean.EventSourceBean;
import com.casic.dcms.bean.ExcelDataBean;
import com.casic.dcms.bean.HighEventBean;
import com.casic.dcms.databinding.FragmentExcelBinding;
import com.casic.dcms.mvp.presenter.impl.EventSourcePresenterImpl;
import com.casic.dcms.mvp.presenter.impl.HighEventPresenterImpl;
import com.casic.dcms.mvp.view.IEventSourceView;
import com.casic.dcms.mvp.view.IHighEventView;
import com.casic.dcms.utils.SaveKeyValues;
import com.casic.dcms.utils.ToastHelper;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;

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

/**
 * @author : Pengxh
 * @time : 2021/4/16 9:42
 * @email : 290677893@qq.com
 */
public class ExcelFragment extends AndroidxBaseFragment<FragmentExcelBinding> implements IEventSourceView, IHighEventView {

    private final List<ExcelDataBean> excelDataBeans = new ArrayList<>();
    private Context context;
    private EventSourcePresenterImpl eventSourcePresenter;
    private HighEventPresenterImpl highEventPresenter;
    private String begTime;
    private String endTime;

    @Override
    protected void setupTopBarLayout() {
        context = getContext();
    }

    @Override
    protected void initData() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            String pageTitle = arguments.getString("tag");
        }
        begTime = (String) SaveKeyValues.getValue("begTime", "");
        endTime = (String) SaveKeyValues.getValue("endTime", "");
        eventSourcePresenter = new EventSourcePresenterImpl(this);
        highEventPresenter = new HighEventPresenterImpl(this);
    }

    @Override
    protected void initEvent() {
        if (TextUtils.isEmpty(begTime) || TextUtils.isEmpty(endTime)) {
            ToastHelper.showToast("起始时间或者结束时间不对", ToastHelper.ERROR);
            return;
        }
        if (getTag().equals("EventSourceActivity")) {
            eventSourcePresenter.onReadyRetrofitRequest(begTime, endTime);
        } else {
            highEventPresenter.onReadyRetrofitRequest(begTime, endTime);
        }
    }

    @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.dataTableView.setData(excelDataBeans);
            initTableConfig();
        }
    }

    @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.dataTableView.setData(excelDataBeans);
            initTableConfig();
        }
    }

    private void initTableConfig() {
        TableConfig config = viewBinding.dataTableView.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 onDestroyView() {
        eventSourcePresenter.disposeRetrofitRequest();
        highEventPresenter.disposeRetrofitRequest();
        super.onDestroyView();
    }

    public static ExcelFragment newInstance(String tag) {
        ExcelFragment excelFragment = new ExcelFragment();
        Bundle bundle = new Bundle();
        bundle.putString("tag", tag);
        excelFragment.setArguments(bundle);
        return excelFragment;
    }
}