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

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.bytedance.msdk.api.AdError
import com.bytedance.msdk.api.v2.GMAdSize
import com.bytedance.msdk.api.v2.ad.banner.GMBannerAd
import com.bytedance.msdk.api.v2.ad.banner.GMBannerAdListener
import com.bytedance.msdk.api.v2.ad.banner.GMBannerAdLoadCallback
import com.bytedance.msdk.api.v2.ad.banner.GMNativeAdInfo
import com.bytedance.msdk.api.v2.ad.banner.GMNativeToBannerListener
import com.bytedance.msdk.api.v2.slot.GMAdOptionUtil
import com.bytedance.msdk.api.v2.slot.GMAdSlotBanner
import com.composition.android.ad.gromore.R

import com.composition.android.ad.gromore.impl.GMAdImageSize
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.adLogWithFrame
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
import kotlin.coroutines.resume

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/15
 * description: GroMore Banner广告加载类
 */
class GMBannerRequestImpl : IAdFormatRequest {

    override suspend fun load(context: Context, adUnitBean: AdUnitBean): AdResult {
        return suspendCancellableCoroutine {

            GMSettingConfig.loadAdConfig {

                if (context is Activity) {
                    val mTTBannerViewAd = GMBannerAd(context, adUnitBean.adUnitId)
                    val size = if (adUnitBean.adSize is GMAdImageSize) (adUnitBean.adSize as GMAdImageSize).gmAdSize else 600 to 90
                    val slotBanner = GMAdSlotBanner.Builder()
                        .setBannerSize(GMAdSize.BANNER_CUSTOME)
                        .setAllowShowCloseBtn(true)
                        .setGMAdSlotGDTOption(GMAdOptionUtil.getGMAdSlotGDTOption().build())
                        .setGMAdSlotBaiduOption(GMAdOptionUtil.getGMAdSlotBaiduOption().build())
                        .setImageAdSize(size.first, size.second)
                        .setMuted(true)
                        .build()

                    mTTBannerViewAd.loadAd(slotBanner, object : GMBannerAdLoadCallback {
                        override fun onAdFailedToLoad(p0: AdError) {
                            printAdFailInfo(adUnitBean, p0.code, p0.message)
                            it.isActiveResult(AdResult.Fail(adUnitBean, msg = p0.message))
                        }

                        override fun onAdLoaded() {
                            printAdInfo(adUnitBean)
                            it.isActiveResult(AdResult.Success(adBean = adUnitBean, adObject = mTTBannerViewAd, msg = "GroMore广告-横幅-加载成功"))
                        }
                    })

                } else {
                    if (it.isActive) {
                        printAdFailInfo(adUnitBean, 0, "GroMore广告-横幅-加载失败：context需要为Activity")
                        it.resume(AdResult.Fail(adUnitBean, msg = "GroMore广告-横幅-加载失败：context需要为Activity"))
                    }
                }
            }
        }
    }
}