package com.composition.android.ad.admob.util

import android.content.Context
import com.composition.android.ad.admob.impl.AdmobAdSize
import com.composition.android.lib.ad.interfaces.IAdSize
import com.composition.android.lib.ad.util.getDensity
import com.composition.android.lib.ad.util.getScreenWidth
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize


/**
 * 获取错误代码对应的信息
 */
internal fun getFailedCodeInfo(code: Int): String {
    return when (code) {
        AdRequest.ERROR_CODE_NO_FILL ->
            "admob 没有填充广告 code:${AdRequest.ERROR_CODE_NO_FILL}"
        AdRequest.ERROR_CODE_INTERNAL_ERROR ->
            "admob 内部发生了一些事情; 例如，从广告服务器收到无效响应 code:${AdRequest.ERROR_CODE_INTERNAL_ERROR}"
        AdRequest.ERROR_CODE_NETWORK_ERROR ->
            "admob 由于网络连接，广告请求未成功。code:${AdRequest.ERROR_CODE_NETWORK_ERROR}"
        AdRequest.ERROR_CODE_INVALID_REQUEST ->
            "admob 广告请求无效; 例如，广告单元ID不正确。。code:${AdRequest.ERROR_CODE_INVALID_REQUEST}"
        else -> "其他错误 code: $code"
    }
}



/**
 * 横幅广告尺寸
 */
internal fun getAdSize(context: Context): AdSize {

    // Step 2 - Determine the screen width (less decorations) to use for the ad width.
    val widthPixels: Int = getScreenWidth(context)
    val density: Float = getDensity(context)
    val adWidth = (widthPixels / density).toInt()

    // Step 3 - Get adaptive ad size and return for setting on the ad view.
    return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(context, adWidth)
}


/**
 * 横幅 自适应尺寸
 */
fun AdSize.autoSize(context: Context) : AdSize{
    return getAdSize(context)
}


/**
 * 构建Admob横幅广告尺寸
 */
fun com.composition.android.lib.ad.interfaces.AdSize.Builder.buildAdmobBannerAdSize(adSize : AdSize) : IAdSize<AdSize>{
    return AdmobAdSize(adSize)
}