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

import android.content.Context
import com.composition.android.lib.ad.AdManager
import com.composition.android.lib.ad.bean.AdUnitBean
import com.composition.android.lib.ad.util.adLog
import com.convenient.android.common.extension.getDensity
import com.convenient.android.common.extension.getScreenWidth
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.ResponseInfo


/**
 * 获取错误代码对应的信息
 */
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 printAdInfo(adBean: AdUnitBean, responseInfo: ResponseInfo?) {
    adLog(
        AdManager.TAG,
        "-------------------------------------------------------------------"
    )

    adLog(AdManager.TAG, "广告加载成功")
    adLog(AdManager.TAG, "广告商:${adBean.advertisersName}")
    adLog(AdManager.TAG, "广告位:${adBean.adSlotName}")
    adLog(AdManager.TAG, "广告ID:${adBean.adUnitId}")
    adLog(AdManager.TAG, "广告类型:${adBean.adType}")
    adLog(AdManager.TAG, "广告中介提供商:${responseInfo?.mediationAdapterClassName}")

    adLog(
        AdManager.TAG,
        "-------------------------------------------------------------------"
    )
}


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

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

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