package com.base.locationsharewhite.ads.admob

import com.base.locationsharewhite.ads.AdsMgr
import com.base.locationsharewhite.ads.AdsType
import com.base.locationsharewhite.helper.EventUtils
import com.base.locationsharewhite.utils.AppPreferences
import com.base.locationsharewhite.utils.KotlinExt.toFormatTime4


/**
 * 控制广告计数与判断显示条件
 *
 */
object LimitUtils {
    const val NUM_DISPLAY = "local_numDisplayLimit"

    const val NUM_REQUEST = "local_numRequestLimit"

    const val NUM_CLICK = "local_numClickLimit"

    const val SAVE_DATE = "local_SAVE_DATE"

    /**
     * 保存的时间，用来判断是否是当天，不是当天要重置计数次数
     */
    private var saveDate
        get() = AppPreferences.getInstance()
            .getString(SAVE_DATE, System.currentTimeMillis().toFormatTime4())
        set(value) = AppPreferences.getInstance().put(SAVE_DATE, value)


    /**
     * 广告请求是否到达限制
     */
    private inline val isRequestLimited: Boolean
        get() {
            val maxCount = AdsMgr.adsConfigBean?.numRequestLimit ?: 100
            return maxCount > -1 && AppPreferences.getInstance()
                .getInt(NUM_REQUEST, 0) >= maxCount
        }

    /**
     * 广告展示是否到达限制
     */
    private inline val isDisplayLimited: Boolean
        get() {
            val maxCount = AdsMgr.adsConfigBean?.numDisplayLimit ?: 100
            return maxCount > -1 && AppPreferences.getInstance()
                .getInt(NUM_DISPLAY, 0) >= maxCount
        }

    /**
     * 广告点击是否到达限制
     */
    private inline val isClickLimited: Boolean
        get() {
            val maxCount = AdsMgr.adsConfigBean?.numClickLimit ?: 100
            return maxCount > -1 && AppPreferences.getInstance()
                .getInt(NUM_CLICK, 0) >= maxCount
        }


    private fun getAdEventMsg(adsType: AdsType): String {
        return when (adsType) {
            AdsType.OPEN -> "Open"
            AdsType.INSERT -> "Inter"
            AdsType.NATIVE -> "Native"
            else -> "Banner"
        }
    }

    /**
     * 是否显示广告
     *
     * @return true or false
     */
    fun isAdShow(adsType: AdsType): Boolean {
        val currentDate = System.currentTimeMillis().toFormatTime4()
        if (saveDate != currentDate) {
            //如果已经不是今天了，就重置个数
            saveDate = currentDate
            AppPreferences.getInstance().put(NUM_DISPLAY, 0)
            AppPreferences.getInstance().put(NUM_REQUEST, 0)
            AppPreferences.getInstance().put(NUM_CLICK, 0)
        }
        return !(isDisplayLimited.also {
            if (it) EventUtils.event(
                "ad_limit",
                "current${getAdEventMsg(adsType)}Show=${
                    AppPreferences.getInstance()
                        .getInt(NUM_DISPLAY, 0)
                } ${getAdEventMsg(adsType).lowercase()}_max_show=${AdsMgr.adsConfigBean?.numDisplayLimit}"
            )
        } || isClickLimited.also {
            if (it) EventUtils.event(
                "ad_limit",
                "current${getAdEventMsg(adsType)}Click=${
                    AppPreferences.getInstance()
                        .getInt(NUM_CLICK, 0)
                } ${getAdEventMsg(adsType).lowercase()}_max_click=${AdsMgr.adsConfigBean?.numClickLimit}"
            )
        } || isRequestLimited.also {
            if (it) EventUtils.event(
                "ad_limit",
                "current${getAdEventMsg(adsType)}Request=${
                    AppPreferences.getInstance()
                        .getInt(NUM_REQUEST, 0)
                } ${getAdEventMsg(adsType).lowercase()}_max_request=${AdsMgr.adsConfigBean?.numRequestLimit}"
            )
        })
    }


    private fun addNum(key: String) {
        val currentDate = System.currentTimeMillis().toFormatTime4()
        if (saveDate != currentDate) {
            //如果已经不是今天了，就重置个数
            saveDate = currentDate
            AppPreferences.getInstance().put(key, 1)
            return
        }
        AppPreferences.getInstance()
            .put(key, (AppPreferences.getInstance().getInt(key, 0) + 1))
    }

    fun addDisplayNum() {
        addNum(NUM_DISPLAY)
    }

    fun addRequestNum() {
        addNum(NUM_REQUEST)
    }

    fun addClickNum() {
        addNum(NUM_CLICK)
    }

    /**
     * 开屏和插页广告的显示间隔限制
     *
     * @param lastTime 上一次显示的时间
     */
    fun isIntervalLimited(lastTime: Long) =
        ((System.currentTimeMillis() - lastTime) / 1000 / 60).toInt() < (AdsMgr.adsConfigBean?.timeInterval
            ?: 1)
}