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.SmartTable;
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.BaseFragment;
import com.casic.dcms.bean.EventSourceBean;
import com.casic.dcms.bean.ExcelDataBean;
import com.casic.dcms.bean.HighEventBean;
import com.casic.dcms.mvp.presenter.EventSourcePresenterImpl;
import com.casic.dcms.mvp.presenter.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;

import butterknife.BindView;

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

    @BindView(R.id.dataTableView)
    SmartTable dataTableView;

    private String tag = "";
    private String begTime;
    private String endTime;
    private EventSourcePresenterImpl eventSourcePresenter;
    private HighEventPresenterImpl highEventPresenter;
    private Context context;
    private List<ExcelDataBean> excelDataBeans;

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

    @Override
    protected int initLayoutView() {
        return R.layout.fragment_excel;
    }

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

    @Override
    protected void initData() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            tag = arguments.getString("tag");
        }
        begTime = (String) SaveKeyValues.getValue("begTime", "");
        endTime = (String) SaveKeyValues.getValue("endTime", "");
        excelDataBeans = new ArrayList<>();
        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 (tag.equals("EventSourceActivity")) {
            eventSourcePresenter.onReadyRetrofitRequest(begTime, endTime);
        } else {
            highEventPresenter.onReadyRetrofitRequest(begTime, endTime);
        }
    }

    @Override
    public void obtainEventSourceResult(EventSourceBean resultBean) {
        if (resultBean.isSuccess()) {
            List<EventSourceBean.DataBean> dataBeanList = resultBean.getData();
            for (int i = 1; i < dataBeanList.size(); i++) {
                EventSourceBean.DataBean dataBean = dataBeanList.get(i);
                String eventNum = dataBean.getEventNum();
                excelDataBeans.add(new ExcelDataBean(i, dataBean.getCaseSource(), Integer.parseInt(eventNum)));
            }
            dataTableView.setData(excelDataBeans);
            initTableConfig();
        }
    }

    private void initTableConfig() {
        TableConfig config = dataTableView.getConfig();
        //首行属性
        config.setColumnTitleStyle(new FontStyle(50, Color.DKGRAY));//设置列标题文字样式
        config.setColumnTitleBackgroundColor(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.isSuccess()) {
            List<HighEventBean.DataBean> dataBeanList = resultBean.getData();
            for (int i = 1; i < dataBeanList.size(); i++) {
                HighEventBean.DataBean dataBean = dataBeanList.get(i);
                String eventNum = dataBean.getCaseNum();
                excelDataBeans.add(new ExcelDataBean(i, dataBean.getCaseType(), Integer.parseInt(eventNum)));
            }
            dataTableView.setData(excelDataBeans);
            initTableConfig();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (eventSourcePresenter != null) {
            eventSourcePresenter.disposeRetrofitRequest();
        }
        if (highEventPresenter != null) {
            highEventPresenter.disposeRetrofitRequest();
        }
    }
}
