package com.composition.android.ad.gromore.load.request.custom

import android.content.Context
import android.view.ViewGroup
import android.view.ViewParent
import com.beizi.fusion.AdListener
import com.beizi.fusion.SplashAd
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.mediation.MediationConstant
import com.bytedance.sdk.openadsdk.mediation.bridge.custom.splash.MediationCustomSplashLoader
import com.bytedance.sdk.openadsdk.mediation.custom.MediationCustomServiceConfig
import com.composition.android.lib.ad.util.adLogWithFrame
import com.composition.android.lib.ad.util.getScreenHeight
import com.composition.android.lib.ad.util.getScreenHeightDp
import com.composition.android.lib.ad.util.getScreenWidth
import com.composition.android.lib.ad.util.getScreenWidthDp

/**
 * @author: HuBoWen
 * @date: 2023/4/25
 * @description:
 */
class CustomBeiZiAppOpenRequestImpl : MediationCustomSplashLoader() {

    private var splash: SplashAd? = null

    override fun load(context: Context?, adSlot: AdSlot?, serviceConfig: MediationCustomServiceConfig?) {

        adLogWithFrame(msg = "scope onAdLoaded")
        //step3:创建开屏广告请求参数AdSlot,具体参数含义参考文档
        context?.apply {
            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 = SplashAd(context, null, serviceConfig?.adnNetworkSlotId, object : AdListener {
                override fun onAdLoaded() {
                    adLogWithFrame(msg = "scope onAdLoaded")
                    if (biddingType == MediationConstant.AD_TYPE_CLIENT_BIDING) {
                        var epcm = splash?.ecpm?.toDouble()
                        epcm?.apply {
                            if (this < 0) {
                                epcm = 0.0
                            }
                            callLoadSuccess(epcm!!)
                        }
                    } else {
                        callLoadSuccess()
                    }
                }

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

                override fun onAdFailedToLoad(p0: Int) {
                    adLogWithFrame(msg = "scope onAdFailedToLoad")
//                    callLoadFail(p0, s)
                }

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

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

                override fun onAdClicked() {
                    adLogWithFrame(msg = "scope onAdClicked")
                }
            }, 5000)
            splash?.loadAd(splashWidthPx, (screenHeightPx - 100))
        }
    }

    override fun showAd(container: ViewGroup?) {
        splash?.show(container)
    }
}