package com.composition.android.lib.ad

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

/**
 * @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>) {
        //去重，每个广告位只准配一个
        val list = adUnits.distinctBy {
            it.adSlotName
        }
        adUnitLists.clear()
        adUnitLists.addAll(list)
        printSetAdUnitInfo(list)
    }


    /**
     * 根据广告位名称获取广告单元配置
     */
    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.advertisers == advertisers
        }
    }

    private fun printSetAdUnitInfo(adUnits: List<AdUnitBean>) {
        adLog(msg = "┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────")
        adLog(msg = "│ 启用的广告位：")
        adLog(msg = "│ ----------------------------------")
        for (adUnit in adUnits) {
            adLog(msg = "│ 广告位:${adUnit.adSlotName}")
            adLog(msg = "│ 广告商:${adUnit.advertisersName}")
            adLog(msg = "│ 广告类型:${adUnit.adType}")
            adLog(msg = "│ 广告id:${adUnit.adUnitId}")
            adLog(msg = "│ 加载超时时间:${adUnit.requestAdConfig.adLoadTimeOut}")
            adLog(msg = "│ 加载重试次数:${adUnit.requestAdConfig.adLoadRetryCount}")
            adLog(msg = "│ ----------------------------------")
        }
        adLog(msg = "└────────────────────────────────────────────────────────────────────────────────────────────────────────────────")
    }

    /**
     * 广告位策略是通过状态
     */
    fun adSlotStrategyIsAllow(adSlotName: String): Boolean {
        val adUnitList = getAdUnitBySlotName(adSlotNames = arrayListOf(adSlotName))
        return adUnitList.any {
            it.loadStrategy.allow()
        }
    }

    /**
     * 广告单元是可用的
     */
    fun adUnitIsAvailable(adSlotName: String): Boolean {
        val adUnitList = getAdUnitBySlotName(adSlotNames = arrayListOf(adSlotName))
        val hasUnAvailableAdUnit = adUnitList.any {
            it.isAvailable().not()
        }
        return hasUnAvailableAdUnit.not()
    }

    fun removeAdUnit(adSlotName: String) : Boolean {
        val needRemoveBean = adUnitLists.find {
            it.adSlotName.equals(adSlotName)
        }
        return adUnitLists.remove(needRemoveBean)
    }

    fun getAllAdUnits() : List<AdUnitBean>{
        return adUnitLists
    }


}