package com.composition.android.lib.ad

import android.content.Context
import androidx.appcompat.widget.AppCompatImageView
import com.composition.android.lib.ad.bean.OnAdShow
import com.composition.android.lib.ad.interfaces.AdLoader
import com.composition.android.lib.ad.interfaces.ImageLoader
import com.composition.android.lib.ad.interfaces.Initialize
import com.composition.android.lib.ad.util.adLog
import com.composition.android.lib.ad.util.adLogWithFrame

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/1
 * description:广告管理类
 */
class AdManager {


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

        const val TAG = "广告组件"
    }

    /**
     * 上下文，使用application
     */
    lateinit var context: Context

    /**
     * 是否开启日志
     */
    var logEnable = false
    set(value) {
        field = value
        adLogWithFrame(msg = "广告日志已${if (value) "开启" else "关闭"}")
    }

    var isDebug = false

    /**
     * 广告全局开关
     */
    var enableAd: Boolean = true

    /**
     * 注册的广告加载器
     */
    internal var adLoaderLists = mutableListOf<AdLoader>()

    var globalListener: OnAdShow? = null

    var normalImageLoader : ImageLoader<AppCompatImageView>? = null


    /**
     * 广告初始化
     */
    fun init(applicationContext: Context, logEnable: Boolean = false) {
        this.context = applicationContext
        this.logEnable = logEnable
    }

    /**
     * 注册广告加载器
     */
    fun registerAdLoader(vararg adLoader: AdLoader) {
        adLoaderLists.clear()
        adLoaderLists.addAll(adLoader)
        adLogWithFrame(msg = "注册的广告加载器:[" +
                "${
                    adLoader.map { 
                        it.javaClass.simpleName
                    }
                }]")
    }


    fun addGlobalAdShowListener(adListener: OnAdShow) {
        globalListener = adListener
    }


    /**
     * 初始化广告商SDK
     */
    fun initAdvertisersSDK(context: Context, vararg advertisersSdk: Initialize) {
        for (initialize in advertisersSdk) {
            initialize.init(context)
        }
    }



}