package com.base.pdfreaderallpdfreader.ads

import android.app.Activity
import com.base.pdfreaderallpdfreader.ads.admob.AdmobInterstitialUtils
import com.base.pdfreaderallpdfreader.ads.admob.AdmobNativeUtils
import com.base.pdfreaderallpdfreader.helper.EventUtils
import com.base.pdfreaderallpdfreader.helper.MyApplication
import com.base.pdfreaderallpdfreader.utils.AppPreferences
import com.google.android.gms.ads.MobileAds
import java.util.concurrent.atomic.AtomicBoolean

object AdmobHelper {

    //开屏限制
    const val open_limit_request = "open_limit_request"
    const val open_limit_show = "open_limit_show"
    const val open_limit_click = "open_limit_click"

    //插页限制
    const val inter_limit_request = "inter_limit_request"
    const val inter_limit_show = "inter_limit_show"
    const val inter_limit_click = "inter_limit_click"

    //原生广告限制
    const val native_limit_request = "native_limit_request"
    const val native_limit_show = "native_limit_show"
    const val native_limit_click = "native_limit_click"


    //是否展示多语言
    val showLanPage = "showLanPage"

    //开屏加载ad时间
    val open_ad_loading = "open_ad_loading"


    var isAdInit = AtomicBoolean(false)
    fun initAdmobAd(activity: Activity) {
        MobileAds.initialize(MyApplication.context) { initializationStatus ->
            isAdInit.set(true)
            EventUtils.event("AdmobInit", "AdmobInit")
            AdmobNativeUtils.loadNativeAd()
            AdmobInterstitialUtils.loadInterstitialAd(activity)
        }
    }

    //上次展示广告时间关闭赋值,通用开屏和插页
    var lastShowedOnHiddenTime = 0L
        get() {
            return AppPreferences.getInstance().getLong("lastShowedOnHiddenTime", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("lastShowedOnHiddenTime", value, true)
        }


    /**
     * 通用广告条件判断
     */
    fun canCommonShowAd(): Boolean {
        val interval = AppPreferences.getInstance().getString("ad_interval", "10").toInt()
        if (System.currentTimeMillis() - lastShowedOnHiddenTime < interval * 1000L) {
            return false
        }
        return true
    }

    //上次scan展示ad时间
    var lastScanShowAd = 0L
        get() {
            return AppPreferences.getInstance().getLong("lastScanShowAd", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("lastScanShowAd", value, true)
        }

    //是否显示扫描功能ad
    fun isShowScanInter(): Boolean {
        val interval = AppPreferences.getInstance().getString("scan_ad_interval", "10").toInt()
        return System.currentTimeMillis() - lastScanShowAd > interval * 1000L
    }

    //上次打开文档展示ad时间
    var lastOpenDocumentShowAd = 0L
        get() {
            return AppPreferences.getInstance().getLong("lastOpenDocumentShowAd", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("lastOpenDocumentShowAd", value, true)
        }

    //打开文档是否展示广告
    fun isShowOpenDocumentInter(): Boolean {
        val interval = AppPreferences.getInstance().getString("open_document_ad_interval", "10").toInt()
        return System.currentTimeMillis() - lastOpenDocumentShowAd > interval * 1000L
    }

    var lastCloseDocumentShowAd = 0L
        get() {
            return AppPreferences.getInstance().getLong("lastCloseDocumentShowAd", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("lastCloseDocumentShowAd", value, true)
        }

    fun isShowCloseDocumentInter(): Boolean {
        val interval = AppPreferences.getInstance().getString("close_document_ad_interval", "10").toInt()
        return System.currentTimeMillis() - lastCloseDocumentShowAd > interval * 1000L
    }

    fun isShowCloseDocument(): Boolean {
        val status = AppPreferences.getInstance().getString("close_document_ad_show", "0").toInt()
        return status == 1
    }

    fun isShowRvNativeAd(): Boolean {
        val status = AppPreferences.getInstance().getString("rv_native_ad_show", "0").toInt()
        return status == 1
    }

    fun isBackShowAd(): Boolean {
        val status = AppPreferences.getInstance().getString("is_back_show_ad", "0").toInt()
        return status == 1
    }


}