package com.ujuad.demo.helper

import android.app.Activity
import android.view.ViewGroup
import com.ujuad.demo.constant.DemoConfig
import com.ujuad.demo.listener.PrintLogger
import com.ujuad.demo.utils.DemoLogUtils
import com.ujusdk.adsdk.exception.UjuException
import com.ujusdk.adsdk.public.config.UjuAdConfig
import com.ujusdk.adsdk.public.data.AdViewSize
import com.ujusdk.adsdk.public.listener.FeedAdObjectListener
import com.ujusdk.adsdk.public.objects.BannerAdObject

/**
 * @CreateDate: 2026/2/4 16:29
 * @Author: 青柠
 * @Description:
 */
class BannerAdHelper(private val activity: Activity, private val logger: PrintLogger) {

    private var bannerAdObject: BannerAdObject? = null

    fun load() {
        val adConfig = UjuAdConfig(
            placementId = DemoConfig.BANNER_ID,
            adViewSize = AdViewSize(width = 320, height = 100)
        )
        bannerAdObject = BannerAdObject(activity, adConfig)
        bannerAdObject?.setAdObjectListener(object : FeedAdObjectListener {
            override fun onLoadSuccess(placementId: String) {
                //广告加载成功,此时可以展示，建议展示前判断isReady
                logger.add("Banner: onLoadSuccess")
            }

            override fun onLoadError(
                error: UjuException,
                placementId: String
            ) {
                logger.add("Banner: onLoadError：${error.message}")
                DemoLogUtils.e("onLoadError：${error.message}")
            }

            override fun onAdError(
                error: UjuException,
                placementId: String
            ) {
                logger.add("Banner: onAdError：${error.message}")
                DemoLogUtils.e("onAdError：${error.message}")
            }

            override fun onAdShow() {
                val adInfo = bannerAdObject?.getAdInfo()
                logger.add("Banner: onAdShow：ecpm:${adInfo?.ecpm}")
            }

            override fun onAdClicked() {
                logger.add("Banner: onAdClicked")
            }

            override fun onAdClosed() {
                logger.add("Banner: onAdClosed")
            }

            override fun onLpClosed() {
                logger.add("Banner: onLpClosed")
            }
        })
        bannerAdObject?.load()
        logger.add("Banner: load, placementId:${adConfig.placementId}")
        DemoLogUtils.d("Banner: load, placementId:${adConfig.placementId}")
    }

    fun show(viewGroup: ViewGroup) {
        if (bannerAdObject?.isReady() == true) {
            bannerAdObject?.show(activity, viewGroup)
        } else {
            logger.add("Banner: 广告还未准备好")
        }
    }

    fun isLoaded(): Boolean {
        return bannerAdObject != null
    }

    fun destroy() {
        bannerAdObject?.destroy()
        bannerAdObject = null
    }
}