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.reward.GMRewardAd
import com.bytedance.msdk.api.v2.ad.reward.GMRewardedAdLoadCallback
import com.bytedance.msdk.api.v2.slot.GMAdSlotRewardVideo
import com.composition.android.ad.gromore.util.GMSettingConfig
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.isActiveResult
import com.composition.android.lib.ad.util.printAdFailInfo
import com.composition.android.lib.ad.util.printAdInfo
import kotlinx.coroutines.suspendCancellableCoroutine

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/16
 * description: GroMore 激励视频广告加载类
 */
class GMRewardRequestImpl : IAdFormatRequest {
    override suspend fun load(context: Context, adUnitBean: AdUnitBean): AdResult {

        return suspendCancellableCoroutine {
            if (context is Activity) {
                GMSettingConfig.loadAdConfig {
                    val gmRewardAd = GMRewardAd(context, adUnitBean.adUnitId)
                    gmRewardAd.loadAd(GMAdSlotRewardVideo.Builder()
                        .setRewardAmount(1)
                        .setMuted(true)
                        .setOrientation(GMAdConstant.VERTICAL)
                        .build(), object : GMRewardedAdLoadCallback {
                        override fun onRewardVideoLoadFail(p0: AdError) {
                            printAdFailInfo(adUnitBean, p0.code, p0.message)
                            it.isActiveResult(AdResult.Fail(adUnitBean, msg = "GroMore广告-激励视频-加载失败"))
                        }

                        override fun onRewardVideoAdLoad() {
                        }

                        override fun onRewardVideoCached() {
                            printAdInfo(adUnitBean)
                            it.isActiveResult(AdResult.Success(adBean = adUnitBean, adObject = gmRewardAd, msg = "GroMore广告-激励视频-加载成功"))
                        }
                    })
                }
            } else {
                printAdFailInfo(adUnitBean, 0, "GroMore广告-激励视频-加载异常：context需要为Activity类型")
                it.isActiveResult(AdResult.Fail(adUnitBean, msg = "GroMore广告-激励视频-加载异常：context需要为Activity类型"))
            }
        }
    }
}