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

import android.content.Context
import com.bytedance.sdk.openadsdk.*
import com.composition.android.ad.csj.impl.CSJBannerAdSize
import com.composition.android.ad.csj.util.printAdFailInfo
import com.composition.android.ad.csj.util.printAdInfo
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.adLogE
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/13
 * description:穿山甲原生广告加载类
 */
class CSJNativeRequestImpl : IAdFormatRequest {

    override suspend fun load(context: Context, adUnitBean: AdUnitBean): AdResult {
        return suspendCancellableCoroutine {
            adLogE(AdManager.TAG, "穿山甲广告已不再支持原生广告格式，请使用横幅模板广告 AdType.Banner")
            it.resume(AdResult.Fail(adUnitBean, msg = "穿山甲已不再支持原生广告格式，请改用横幅广告"))

 /*           val adSlot = AdSlot.Builder()
                .setCodeId(adUnitBean.adUnitId)
                .setAdCount(1)
                .setImageAcceptedSize(640, 320)
                .setAdLoadType(TTAdLoadType.PRELOAD)
                .setNativeAdType(AdSlot.TYPE_BANNER)
                .build()

            TTAdSdk.getAdManager().createAdNative(context)
                .loadNativeExpressAd(adSlot, object : TTAdNative.NativeExpressAdListener {
                    override fun onError(p0: Int, p1: String?) {
                        printAdFailInfo(p0, p1)
                        if (it.isActive) {
                            it.resume(AdResult.Fail(adUnitBean, msg = p1 ?: "穿山甲原生广告加载失败"))
                        }
                    }

                    override fun onNativeExpressAdLoad(p0: MutableList<TTNativeExpressAd>?) {
                        printAdInfo(adUnitBean)
                        if (it.isActive) {
                            if (p0.isNullOrEmpty().not()) {
                                it.resume(AdResult.Success(adBean = adUnitBean, adObject = p0?.get(0), msg = "穿山甲原生广告加载成功"))
                            } else {
                                it.resume(AdResult.Fail(adUnitBean, msg = "穿山甲原生广告加载失败, adList为空"))
                            }
                        }
                    }
                })*/

        }
    }
}