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

import android.app.Activity
import android.content.Context
import com.bytedance.msdk.api.AdError
import com.bytedance.msdk.api.v2.GMAdConstant
import com.bytedance.msdk.api.v2.ad.interstitialFull.GMInterstitialFullAd
import com.bytedance.msdk.api.v2.ad.interstitialFull.GMInterstitialFullAdLoadCallback
import com.bytedance.msdk.api.v2.slot.GMAdOptionUtil
import com.bytedance.msdk.api.v2.slot.GMAdSlotInterstitialFull
import com.composition.android.ad.gromore.util.GMSettingConfig
import com.composition.android.lib.ad.AdManager
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

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/16
 * description:GroMore 插屏广告加载类
 */
class GMInterstitialRequestImpl : IAdFormatRequest {
    override suspend fun load(context: Context, adUnitBean: AdUnitBean): AdResult {
        return suspendCancellableCoroutine {
            GMSettingConfig.loadAdConfig {
                if (context is Activity) {
                    val mInterstitialFullAd = GMInterstitialFullAd(context, adUnitBean.adUnitId)
                    val customData: MutableMap<String, String> = HashMap()
                    customData[GMAdConstant.CUSTOM_DATA_KEY_GDT] = "gdt custom data"
                    val adSlot = GMAdSlotInterstitialFull.Builder()
                        .setCustomData(customData)
                        .setGMAdSlotGDTOption(GMAdOptionUtil.getGMAdSlotGDTOption().build())
                        .setGMAdSlotBaiduOption(GMAdOptionUtil.getGMAdSlotBaiduOption().build())
                        .setOrientation(GMAdConstant.VERTICAL)
                        .setUserID(adUnitBean.adUnitId)
                        .setMuted(true)
                        .build()

                    mInterstitialFullAd.loadAd(adSlot, object : GMInterstitialFullAdLoadCallback {
                        override fun onInterstitialFullLoadFail(p0: AdError) {
                            printAdFailInfo(adUnitBean, p0.code, p0.message)
                            it.isActiveResult(AdResult.Fail(adUnitBean, msg = "GroMore广告-插屏-加载失败"))
                        }

                        override fun onInterstitialFullAdLoad() {
                            //这里为广告加载成功，但还没缓存好
                        }

                        override fun onInterstitialFullCached() {
                            printAdInfo(adUnitBean)
                            //这里为广告已经缓存好了，保证流畅播放，再次返回成功
                            it.isActiveResult(AdResult.Success(adBean = adUnitBean, adObject = mInterstitialFullAd, msg = "GroMore广告-插屏-加载成功"))
                        }
                    })

                } else {
                    printAdFailInfo(adUnitBean, 0, "GroMore广告-插屏-加载失败：context需要为activity")
                    it.isActiveResult(AdResult.Fail(adUnitBean, msg = "GroMore广告-插屏-加载失败：context需要为activity"))
                }
            }
        }
    }
}