package com.composition.android.lib.ad

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.composition.android.lib.ad.basic.NativeAdViewHolder
import com.composition.android.lib.ad.basic.AdResult
import com.composition.android.lib.ad.basic.BannerAdViewHolder
import com.composition.android.lib.ad.bean.AdUnitBean
import com.composition.android.lib.ad.impl.Api
import com.composition.android.lib.ad.impl.NormalAdListener
import kotlinx.coroutines.flow.Flow

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/1
 * description:广告加载类，用于处理广告的加载、展示
 */
object AdLoad {

    /**
     * 每个广告位最大缓存数量
     */
    var adSlotMaxCacheCount = 1

    /**
     * 缓存集合
     */
    private var cacheList: MutableList<AdResult.Success> = mutableListOf()

    fun addToCache(success: AdResult.Success){
        cacheList.add(success)
    }

    suspend fun preloadAd(context: Context, vararg adSlotNames: String) {
        val beans = AdUnitConfigManager.instance.getAdUnitBySlotName(adSlotNames.toList())
        preloadAd(context, beans)
    }


    suspend fun preloadAd(context: Context, adUnitBeans: List<AdUnitBean>) {
        loadAd(context, adUnitBeans)
            .collect {
                if (it is AdResult.Success) {
                    cacheList.add(it)
                }
            }
    }

    suspend fun hasCacheAd(adSlotName: String): Boolean {
        return cacheList.any {
            it.adBean.adSlotName.equals(adSlotName)
        }
    }

    suspend fun getCacheAd(adUnitBean: AdUnitBean): AdResult.Success? {
        val ad = cacheList.find {
            it.adBean.equals(adUnitBean)
        }
        if (ad != null) {
            cacheList.remove(ad)
        }
        return ad
    }


    /**
     * 根据广告位置名称加载广告
     * 需要先进行配置广告单元信息
     * 可传入多个广告位，会按顺序加载多个广告
     * @param context 上下文，推荐用activity context, 部分中介广告加载必须要activity context
     * @param adSlotName 广告位名称
     * @see AdUnitBean
     * @see AdUnitConfigManager
     * @return Flow<AdResult> 返回协程Flow流
     */
    suspend fun loadAd(context: Context, vararg adSlotNames: String): Flow<AdResult> {
        val beans = AdUnitConfigManager.instance.getAdUnitBySlotName(adSlotNames.toList())
        return loadAd(context = context, adUnitBeans = beans)
    }


    /**
     * 根据广告单元信息加载广告
     * @param context 上下文，推荐用activity context, 部分中介广告加载必须要activity context
     *
     */
    suspend fun loadAd(context: Context, adUnitBeans: List<AdUnitBean>): Flow<AdResult> {
        return Api.instance.load(context, adUnitBeans)
    }


    /**
     * 获取Banner广告View
     */
    fun getBannerAdView(context: Context, success: AdResult.Success, viewHolder: BannerAdViewHolder): View? {
        return Api.instance.getBannerView(context, success,viewHolder)
    }


    fun getNativeAdView(context: Context, adResult : AdResult.Success, viewHolder: NativeAdViewHolder) : View?{
        return Api.instance.getNativeAdView(context, adResult, viewHolder)
    }

    fun populateNativeAdView(context: Context, adResult: AdResult.Success, viewHolder: NativeAdViewHolder, adRootView : View){
        Api.instance.populateNativeAdView(context, adResult, viewHolder, adRootView)
    }

    /**
     * 显示全屏广告，包含插屏、激励视频、激励插屏，根据传入的数据自行判断
     */
    fun showFullScreenAd(
        activity: Activity,
        success: AdResult.Success,
        adListener: NormalAdListener.() -> Unit
    ) {
        Api.instance.showFullScreenAd(activity, success, adListener = NormalAdListener().also(adListener))
    }


    fun showInterstitialAd(
        activity: Activity,
        success: AdResult.Success,
        adListener: NormalAdListener.() -> Unit
    ) {
        Api.instance.showInterstitialAd(activity = activity, success = success, adListener = NormalAdListener().also(adListener))
    }


    fun showRewardedAd(
        activity: Activity,
        success: AdResult.Success,
        adListener: NormalAdListener.() -> Unit
    ) {
        Api.instance.showRewardedAd(activity = activity, success = success, adListener = NormalAdListener().also(adListener))
    }

    fun showSplash(
        activity: Activity,
        splashViewGroup: ViewGroup?,
        success: AdResult.Success,
        adListener: NormalAdListener.() -> Unit
    ) {
        Api.instance.showSplash(activity = activity, splashViewGroup = splashViewGroup, success = success, adListener = NormalAdListener().also(adListener))
    }


}