package net.huansi.app.hssummarizingboard.activity.main

import baseform.android.huansi.net.util.RxTimerManager
import net.huansi.app.hssummarizingboard.base.SutureClothBasePresenter
import net.huansi.app.hssummarizingboard.entity.BrandBean
import net.huansi.app.hssummarizingboard.entity.ListBean
import net.huansi.app.hssummarizingboard.entity.ProDetailsBean
import net.huansi.app.hssummarizingboard.entity.StatisticsDataBean
import net.huansi.app.hssummarizingboard.utils.RequestUtils

class MainPresenter : SutureClothBasePresenter() {
    private var mDateTimeRefreshTimer: RxTimerManager? = null

    fun startDateTimeRefreshTimer(callback: (l: Long) -> Unit) {
        stopDateTimeRefreshTimer()
        mDateTimeRefreshTimer = RxTimerManager()
        mDateTimeRefreshTimer?.intervalStart1(5, 1000L) {
            callback(it)
        }
    }

    fun stopDateTimeRefreshTimer() {
        mDateTimeRefreshTimer?.cancel()
        mDateTimeRefreshTimer = null
    }

    //请求优秀区和潜能区
    suspend fun requestAreaData(reportType: String): ListBean? {
        showLoadDialog()
        val result = try {
            RequestUtils.requestAreaData(reportType)
        } catch (e: Exception) {
            iForm?.showErrorMessage(e.message ?: "操作失败")
            null
        }
        dismissDialog()
        return result
    }

    /**
     * 请求节点汇总详情
     */
    suspend fun requestProDetails(): List<ProDetailsBean> {
        showLoadDialog()
        val result = try {
            RequestUtils.requestProDetails()
        } catch (e: Exception) {
            iForm?.showErrorMessage(e.message ?: "操作失败")
            listOf()
        }
        dismissDialog()
        return result
    }

    /**
     * 请求各品牌堵点
     */
    suspend fun requestBrandList(): List<BrandBean> {
        showLoadDialog()
        val result = try {
            RequestUtils.requestBrandList()
        } catch (e: Exception) {
            iForm?.showErrorMessage(e.message ?: "操作失败")
            listOf()
        }
        dismissDialog()
        return result
    }

    /**
     * 中心统计数据
     */
    suspend fun requestStatisticsData(): StatisticsDataBean? {
        showLoadDialog()
        val result = try {
            RequestUtils.requestStatisticsData()
        } catch (e: Exception) {
            iForm?.showErrorMessage(e.message ?: "操作失败")
            null
        }
        dismissDialog()
        return result
    }

    /**
     * 处理数据
     */
    suspend fun getSummaryDetails(list: List<ProDetailsBean>): ListBean {
        return RequestUtils.getSummaryDetails(list)
    }
}