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.UIUtils.getScreenWidthDp
import com.bytedance.msdk.api.v2.ad.nativeAd.GMNativeAd
import com.bytedance.msdk.api.v2.ad.nativeAd.GMNativeAdLoadCallback
import com.bytedance.msdk.api.v2.ad.nativeAd.GMUnifiedNativeAd
import com.bytedance.msdk.api.v2.slot.GMAdOptionUtil
import com.bytedance.msdk.api.v2.slot.GMAdSlotNative
import com.bytedance.sdk.openadsdk.AdSlot
import com.composition.android.ad.gromore.impl.GMAdImageSize
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 GMNativeTemplateRequestImpl : IAdFormatRequest {


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

        return suspendCancellableCoroutine {
            if (context is Activity) {
                GMSettingConfig.loadAdConfig {

                    val adImageSize = if (adUnitBean.adSize != null && adUnitBean.adSize is GMAdImageSize) {
                        val size = adUnitBean.adSize as GMAdImageSize
                        size.gmAdSize
                    } else {
                        getScreenWidthDp(context).toInt() to 340
                    }
                    val gmNativeAd = GMUnifiedNativeAd(context, adUnitBean.adUnitId)
                    gmNativeAd.loadAd(GMAdSlotNative.Builder()
                        .setAdCount(1)
                        .setGMAdSlotBaiduOption(GMAdOptionUtil.getGMAdSlotBaiduOption().build())
                        .setGMAdSlotGDTOption(GMAdOptionUtil.getGMAdSlotGDTOption().build())
                        .setAdStyleType(AdSlot.TYPE_FEED)
                        .setImageAdSize(adImageSize.first, adImageSize.second)
                        .build(),

                        object : GMNativeAdLoadCallback {
                            override fun onAdLoaded(p0: MutableList<GMNativeAd>) {
                                if (p0.isNullOrEmpty().not()) {
                                    printAdInfo(adUnitBean)
                                    it.isActiveResult(AdResult.Success(adBean = adUnitBean, adObject = p0.get(0), msg = "GroMore广告-原生模板-加载成功"))
                                } else {
                                    printAdFailInfo(adUnitBean, 0, "SDK返回的广告实例集合为空")
                                    it.isActiveResult(AdResult.Fail(adUnitBean, msg = "GroMore广告-原生模板-加载失败"))
                                }
                            }

                            override fun onAdLoadedFail(p0: AdError) {
                                printAdFailInfo(adUnitBean, p0.code, p0.message)
                                it.isActiveResult(AdResult.Fail(adUnitBean, msg = "GroMore广告-原生模板-加载失败"))
                            }
                        }
                    )
                }
            } else {
                printAdFailInfo(adUnitBean, 0, "GroMore广告-原生模板-context需要为Activity类型")
                it.isActiveResult(AdResult.Fail(adUnitBean, msg = "GroMore广告-原生模板-context需要为Activity类型"))
            }
        }
    }

}