package com.all.pdfreader.pdf.reader.ad

import android.app.Activity
import com.all.pdfreader.pdf.reader.BuildConfig

object AdsInsUtil {
    /** 广告位定义（可扩展） */
    enum class AdPlacement(val tag: String) {
        SPL_AND_INTO_HOME(Placement.SPL_AND_INTO_HOME),
        INT_AND_TOPDF(Placement.INT_AND_TOPDF),
        INT_AND_PDFTOHOME(Placement.INT_AND_PDFTOHOME),
        INT_AND_MERGE(Placement.INT_AND_MERGE),
        INT_AND_SPLIT(Placement.INT_AND_SPLIT),
        NATIVE_AND_EXIT(Placement.NATIVE_AND_EXIT),
        BAN_AND_HOMEPAGE(Placement.BAN_AND_HOMEPAGE)
    }

    object Placement {
        const val TAG = "ocean-ad"

        /**
         * 启动页插页
         */
        const val SPL_AND_INTO_HOME = "spl_and_into_home"

        /**
         * 在首页/最近页/喜欢页-点击文件进入PDF内容页过程中，弹出缓存的插屏广告
         */
        const val INT_AND_TOPDF = "int_and_topdf"

        /**
         * PDF内容页点击返回到首页/最近页/喜欢页过程中，弹出缓存的插屏广告
         */
        const val INT_AND_PDFTOHOME = "int_and_pdftohome"

        /**
         * 首页/最近/喜欢-PDF内容页-更多-合并文件（在点击ok后，出现插屏广告，插屏广告结束后到合并成功页面）
         */
        const val INT_AND_MERGE = "int_and_merge"

        /**
         * 首页/最近/喜欢-PDF内容页-更多-拆分文件(点击ok后，出现插屏广告，插屏广告结束后到拆分成功页面)
         */
        const val INT_AND_SPLIT = "int_and_split"

        /**
         * 退出提示对话框原生
         */
        const val NATIVE_AND_EXIT = "native_and_exit"

        /**
         * 首页横幅
         */
        const val BAN_AND_HOMEPAGE = "ban_and_homepage"
    }

    // 广告位对应的广告ID
    val adUnitIdMap: Map<AdPlacement, String> = mapOf(
        AdPlacement.SPL_AND_INTO_HOME to if (BuildConfig.DEBUG) {
            "ca-app-pub-3940256099942544/1033173712"
        } else {
            "ca-app-pub-5717753826607607/9606833210"
        },
        AdPlacement.INT_AND_TOPDF to if (BuildConfig.DEBUG) {
            "ca-app-pub-3940256099942544/1033173712"
        } else {
            "ca-app-pub-5717753826607607/5308904672"
        },
        AdPlacement.INT_AND_PDFTOHOME to if (BuildConfig.DEBUG) {
            "ca-app-pub-3940256099942544/1033173712"
        } else {
            "ca-app-pub-5717753826607607/7085128570"
        },
        AdPlacement.INT_AND_MERGE to if (BuildConfig.DEBUG) {
            "ca-app-pub-3940256099942544/1033173712"
        } else {
            "ca-app-pub-5717753826607607/8928693282"
        },
        AdPlacement.INT_AND_SPLIT to if (BuildConfig.DEBUG) {
            "ca-app-pub-3940256099942544/1033173712"
        } else {
            "ca-app-pub-5717753826607607/2338415962"
        },
        AdPlacement.NATIVE_AND_EXIT to if (BuildConfig.DEBUG) {
            "ca-app-pub-3940256099942544/2247696110"
        } else {
            "ca-app-pub-5717753826607607/7276700267"
        },
        AdPlacement.BAN_AND_HOMEPAGE to if (BuildConfig.DEBUG) {
            "ca-app-pub-3940256099942544/9214589741"
        } else {
            "ca-app-pub-5717753826607607/5939567861"
        }
    )

    fun loadAd(
        act: Activity, adPlacement: AdPlacement
    ): AdInstLoad {
        return AdInstLoad(act, adPlacement, null)
    }

    fun loadAd(
        act: Activity, adPlacement: AdPlacement, loadListener: LoadListener?
    ): AdInstLoad {
        return AdInstLoad(act, adPlacement, loadListener)
    }

    fun showAd(
        act: Activity, adPlacement: AdPlacement
    ): AdInstShower {
        return AdInstShower(act, adPlacement, null)
    }

    fun showAd(
        act: Activity, adPlacement: AdPlacement, listener: ShowListener?
    ): AdInstShower {
        return AdInstShower(act, adPlacement, listener)
    }

    fun showFinishAd(act: Activity, adPlacement: AdPlacement, onFinish: () -> Unit) {
        showAd(
            act = act,
            adPlacement = adPlacement,
            listener = object : ShowListener {
                override fun onAdShown() {}
                override fun onAdClicked() {}
                override fun onAdShowFailed(string: String) {
                    onFinish()
                }

                override fun onAdClosed() {
                    onFinish()
                }
            }
        )
    }
}