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.ad.splash.GMSplashAd
import com.bytedance.msdk.api.v2.ad.splash.GMSplashAdLoadCallback
import com.bytedance.msdk.api.v2.slot.GMAdSlotSplash
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 GMAppOpenRequestImpl : IAdFormatRequest {
    override suspend fun load(context: Context, adUnitBean: AdUnitBean): AdResult {
        return suspendCancellableCoroutine {
            if (context is Activity) {
                GMSettingConfig.loadAdConfig {
                    val mTTSplashAd = GMSplashAd(context, adUnitBean.adUnitId)
                    mTTSplashAd.loadAd(
                        GMAdSlotSplash.Builder()
                            .setImageAdSize(getScreenWidth(context), getScreenHeight(context))
                            .setVolume(1F)
                            .setMuted(false)
                            .setTimeOut(3000)
                            .setSplashShakeButton(true)
                            .build(),
                        object : GMSplashAdLoadCallback {
                            override fun onSplashAdLoadFail(p0: AdError) {
                                printAdFailInfo(adUnitBean, p0.code, p0.message)
                                it.isActiveResult(AdResult.Fail(adUnitBean, msg = "GroMore广告-开屏-加载失败"))
                            }

                            override fun onSplashAdLoadSuccess() {
                                printAdInfo(adUnitBean)
                                it.isActiveResult(AdResult.Success(adBean = adUnitBean, adObject = mTTSplashAd, msg = "GroMore广告-开屏-加载成功"))
                            }

                            override fun onAdLoadTimeout() {
                                printAdFailInfo(adUnitBean, 0, "GroMore广告-开屏-加载超时")
                                it.isActiveResult(AdResult.Fail(adUnitBean, msg = "GroMore广告-开屏-加载超时"))
                            }
                        })
                }

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