package com.base.pdfviewerscannerwhite.ads

import android.app.Activity
import com.base.pdfviewerscannerwhite.ads.admob.AdmobInterstitialUtils
import com.base.pdfviewerscannerwhite.ads.admob.AdmobNativeUtils
import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.helper.EventUtils
import com.base.pdfviewerscannerwhite.helper.MyApplication
import com.base.pdfviewerscannerwhite.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"

    //创建pdf加载ad时间
    val create_pdf_loading = "create_pdf_loading"

    //删除文件
    val delete_loading = "delete_loading"

    //打开文件
    val open_file_loading = "open_file_loading"

    //合并pdf
    val merge_loading = "merge_loading"

    //拆分pdf
    val split_loading = "split_loading"

    //可请求时间段（由 x 定义)
    const val ad_request_period = "ad_request_interval"

    //可请求时间段内允许的最大请求数（由 y 定义)
    const val ad_period_max_request = "ad_period_max_request"

    //可请求时间段间隔时间
    const val ad_request_period_interval = "ad_request_period_interval"


    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
    }


}