package com.composition.android.lib.ad

import com.composition.android.lib.ad.basic.Advertisers
import com.composition.android.lib.ad.bean.AdUnitBean

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/1
 * description:广告单元配置管理类
 */
class AdUnitConfigManager {

    companion object {

        val instance by lazy {
            AdUnitConfigManager()
        }

    }

    /**
     * 配置的广告单元集合
     */
    private var adUnitLists: MutableList<AdUnitBean> = mutableListOf<AdUnitBean>()

    /**
     * 设置广告单元集
     */
    fun setAdUnits(adUnits: List<AdUnitBean>) {
        adUnitLists.clear()
        adUnitLists.addAll(adUnits)
    }


    /**
     * 根据广告位名称获取广告单元配置
     */
    fun getAdUnitBySlotName(adSlotNames: List<String>): List<AdUnitBean> {
        val list = adUnitLists.filter {
            adSlotNames.contains(it.adSlotName) && hasAdLoader(it.getAdvertisers())
        }
        return list
    }


    private fun hasAdLoader(advertisers: Advertisers) : Boolean{
        return AdManager.instance.adLoaderLists.any {
            it.first == advertisers
        }
    }

}