package com.swiftcleaner.chovey.business
import com.swiftcleaner.chovey.util.SPUtils
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

object AdMaxCountUtils {
    // 本地字段
    private const val LAST_USE_DATE_KEY = "lastUseDateKey"
    private const val OPEN_AD_COUNT_KEY = "openAdCountKey"
    private const val INTER_AD_COUNT_KEY = "interAdCountKey"
    private const val NATIVE_AD_COUNT_KEY = "nativeAdCountKey"

    private var lastUseDate = ""
    private var currentUseDate = getCurrentDate()

    private var openAdCount = SPUtils.getInstance().getInt(OPEN_AD_COUNT_KEY, 0)
    private var openAdMaxCount = SPUtils.getInstance().getInt("openadShow", 10) // 配置字段，开屏展示最大值

    private var interAdCount = SPUtils.getInstance().getInt(INTER_AD_COUNT_KEY, 0)
    private var interAdMaxCount = SPUtils.getInstance().getInt("interadShow", 10) // 配置字段，插屏展示最大值

    private var nativeAdCount = SPUtils.getInstance().getInt(NATIVE_AD_COUNT_KEY, 0)
    private var nativeAdMaxCount = SPUtils.getInstance().getInt("nativeadShow", 20) // 配置字段，原生展示最大值

    init {
        if (lastUseDate.isEmpty()) {
            lastUseDate = SPUtils.getInstance().getString(LAST_USE_DATE_KEY, "")
        }
        if (lastUseDate != currentUseDate){ //不是同一天，初始化值
            lastUseDate = currentUseDate
            SPUtils.getInstance().putString(LAST_USE_DATE_KEY, currentUseDate)
            openAdCount = 0
            interAdCount = 0
            nativeAdCount = 0
            SPUtils.getInstance().putInt(OPEN_AD_COUNT_KEY, 0)
            SPUtils.getInstance().putInt(INTER_AD_COUNT_KEY, 0)
            SPUtils.getInstance().putInt(NATIVE_AD_COUNT_KEY, 0)
        }
    }


    fun isOpenAdCountMax(): Boolean{
        return openAdCount >= openAdMaxCount
    }

    //增加开屏次数
    fun addOpenAdCount() {
        openAdCount++
        SPUtils.getInstance().putInt(OPEN_AD_COUNT_KEY, openAdCount)
    }

    fun isInterAdCountMax(): Boolean{
        return interAdCount >= interAdMaxCount
    }

    //增加插屏屏次数
    fun addInterAdCount() {
        interAdCount++
        SPUtils.getInstance().putInt(INTER_AD_COUNT_KEY, interAdCount)
    }

    fun isNativeAdCountMax(): Boolean{
        return nativeAdCount >= nativeAdMaxCount
    }

    //增加原生次数
    fun addNativeAdCount() {
        nativeAdCount++
        SPUtils.getInstance().putInt(NATIVE_AD_COUNT_KEY, nativeAdCount)
    }

    private fun getCurrentDate(): String {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        val currentDate = Calendar.getInstance().time
        return dateFormat.format(currentDate)
    }
}