package com.ujuad.demo.helper

import android.app.Activity
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.listener.InterstitialAdObjectListener
import com.ujusdk.adsdk.public.objects.InterstitialAdObject

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

    private var interstitialAdObject: InterstitialAdObject? = null

    fun load() {
        val adConfig = UjuAdConfig(
            placementId = DemoConfig.INTERSTITIAL_ID,
        )
        interstitialAdObject = InterstitialAdObject(activity, adConfig)
        interstitialAdObject?.setAdObjectListener(object :
            InterstitialAdObjectListener {
            override fun onLoadSuccess(placementId: String) {
                //广告加载成功,此时可以展示，建议展示前判断isReady
                logger.add("Interstitial: onLoadSuccess")
            }

            override fun onLoadError(
                error: UjuException,
                placementId: String
            ) {
                //加载失败
                logger.add("Interstitial: onLoadError:${error.message}")
                DemoLogUtils.e("Interstitial:onLoadError:${error.message}")
            }

            override fun onAdShow() {
                val adInfo = interstitialAdObject?.getAdInfo()
                logger.add("Interstitial: onAdShow:ecpm:${adInfo?.ecpm}")
                DemoLogUtils.d("Interstitial: onAdShow:ecpm:${adInfo?.ecpm}")
            }

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

            override fun onAdPlayComplete() {
                logger.add("Interstitial: onAdPlayComplete")
            }

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

            override fun onLpClosed() {
                logger.add("Interstitial: onLpClosed")
            }

            override fun onAdClosed() {
                interstitialAdObject?.destroy()
                interstitialAdObject = null
                logger.add("Interstitial: onAdClosed")
            }
        })
        interstitialAdObject?.load()
        logger.add("Interstitial: load, placementId:${adConfig.placementId}")
        DemoLogUtils.d("Interstitial: load, placementId:${adConfig.placementId}")
    }

    fun show() {
        if (interstitialAdObject?.isReady() == true) {
            interstitialAdObject?.show(activity)
        } else {
            logger.add("Reward: 广告还未准备好")
        }
    }

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

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