package com.ad.scope.load.request

import android.content.Context
import com.beizi.fusion.AdListener
import com.beizi.fusion.SplashAd
import com.composition.android.lib.ad.basic.AdResult
import com.composition.android.lib.ad.bean.AdUnitBean
import com.composition.android.lib.ad.interfaces.IAdFormatRequest
import com.composition.android.lib.ad.util.*
import kotlinx.coroutines.suspendCancellableCoroutine

/**
 * @author: HuBoWen
 * @date: 2023/4/25
 * @description:
 */
class ScopeAppOpenRequestImpl : IAdFormatRequest {
    override suspend fun load(context: Context, adUnitBean: AdUnitBean): AdResult {
        return suspendCancellableCoroutine {
            var splash: SplashAd? = null
            splash = SplashAd(context, null, adUnitBean.adUnitId, object : AdListener {
                override fun onAdLoaded() {
                    it.isActiveResult(AdResult.Success(adBean = adUnitBean, adObject = splash, msg = "scope-开屏-加载成功"))
                }

                override fun onAdShown() {
                    adLogWithFrame(msg = "scope onAdShown")
                }

                override fun onAdFailedToLoad(p0: Int) {
                    adLogWithFrame(msg = "scope onAdFailedToLoad")
                    it.isActiveResult(AdResult.Success(adBean = adUnitBean, adObject = splash, msg = "scope-开屏-加载成功"))
                }

                override fun onAdClosed() {
                    adLogWithFrame(msg = "scope onAdClosed")
                }

                override fun onAdTick(p0: Long) {
                    adLogWithFrame(msg = "scope onAdTick")
                }

                override fun onAdClicked() {
                    adLogWithFrame(msg = "scope onAdClicked")
                }
            }, 5000)

            adLogWithFrame(msg = "scope onAdLoaded")
            //step3:创建开屏广告请求参数AdSlot,具体参数含义参考文档
            val splashWidthDp: Float = getScreenWidthDp(context)
            val splashWidthPx: Int = getScreenWidth(context)
            val screenHeightPx: Int = getScreenHeight(context)
            val screenHeightDp: Float = getScreenHeightDp(context)

            // 开屏高度 = 屏幕高度 - 下方预留的高度，demo中是预留了屏幕高度的1/5，因此开屏高度传入 屏幕高度*4/5
            val splashHeightDp: Float = screenHeightDp * 4 / 4f
            val splashHeightPx: Int = (screenHeightPx * 4 / 4f).toInt()

            splash.loadAd(splashWidthPx, (screenHeightPx - 100))
        }
    }
}