package com.composition.android.lib.ad.impl

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.composition.android.lib.ad.AdLoad
import com.composition.android.lib.ad.basic.AdLoadCode
import com.composition.android.lib.ad.basic.AdResult
import com.composition.android.lib.ad.basic.BannerAdViewHolder
import com.composition.android.lib.ad.basic.NativeAdViewHolder
import com.composition.android.lib.ad.bean.AdUnitBean
import com.composition.android.lib.ad.factory.AdLoaderFactory
import com.composition.android.lib.ad.interfaces.AdLoader
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/5
 * description:
 */
class Api {

    companion object{
        val instance by lazy {
            Api()
        }
    }

    private fun getAdLoader(adUnitBean: AdUnitBean) : AdLoader{
        return AdLoaderFactory.getAdLoader(adUnitBean)
    }


    fun load(context: Context, adUnitBeans : List<AdUnitBean>) : Flow<AdResult>{
        return flow {
            if (adUnitBeans.isNullOrEmpty()){
                emit(AdResult.Fail(adBean = AdUnitBean.emptyAdUnitBean(), msg = "AdUnitBeans 为空"))
                return@flow
            }
            for (adUnitBean in adUnitBeans) {
                if (adUnitBean.loadStrategy.allow().not()) {
                    emit(AdResult.Fail(adUnitBean, AdLoadCode.FAIL, msg = "策略未通过"))
                    continue
                }
                val cacheAd = AdLoad.getCacheAd(adUnitBean)
                if (cacheAd != null){
                    emit(cacheAd)
                    continue
                }
                val result = getAdLoader(adUnitBean).load(context, adUnitBean)
                emit(result)
            }
        }
    }


    fun getBannerView(context: Context, adResult: AdResult.Success, viewHolder: BannerAdViewHolder) : View?{
        if (adResult.adBean.loadStrategy.allow().not()){
            return null
        }
        return getAdLoader(adResult.adBean).getBannerView(context, adResult, viewHolder)
    }

    fun getNativeAdView(context: Context, adResult : AdResult.Success, viewHolder: NativeAdViewHolder) : View?{
        if (adResult.adBean.loadStrategy.allow().not()){
            return null
        }
        return getAdLoader(adResult.adBean).getNativeAdView(context, adResult, viewHolder)
    }


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


    fun showInterstitialAd(
        activity: Activity,
        success: AdResult.Success,
        adListener: NormalAdListener
    ){
        if (success.adBean.loadStrategy.allow().not()){
            adListener.onAdShowFail("展示策略未通过")
            return
        }
        getAdLoader(success.adBean).showInterstitialAd(activity, success, adListener)
    }


    fun showRewardedAd(
        activity: Activity,
        success: AdResult.Success,
        adListener: NormalAdListener
    ){
        if (success.adBean.loadStrategy.allow().not()){
            adListener.onAdShowFail("展示策略未通过")
            return
        }
        getAdLoader(success.adBean).showRewardedAd(activity, success, adListener)
    }


    fun showSplash(
        activity: Activity,
        splashViewGroup: ViewGroup?,
        success: AdResult.Success,
        adListener: NormalAdListener
    ){
        if (success.adBean.loadStrategy.allow().not()){
            adListener.onAdShowFail("展示策略未通过")
            return
        }
        getAdLoader(success.adBean).showSplashAd(activity, splashViewGroup, success, adListener)
    }

    fun showFullScreenAd(
        activity: Activity,
        success: AdResult.Success,
        adListener: NormalAdListener
    ){
        if (success.adBean.loadStrategy.allow().not()){
            adListener.onAdShowFail("展示策略未通过")
            return
        }
        getAdLoader(success.adBean).showFullScreenAd(activity, success, adListener)
    }


}