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.RewardAdObjectListener
import com.ujusdk.adsdk.public.objects.RewardAdObject

/**
 * @CreateDate: 2026/2/4 16:15
 * @Author: 青柠
 * @Description: 激励广告
 */
class RewardAdHelper(private val activity: Activity, private val logger: PrintLogger) {
    private var rewardAdObject: RewardAdObject? = null

    /**
     * 加载激励广告
     */
    fun load() {
        val adConfig = UjuAdConfig(
            placementId = DemoConfig.REWARD_ID,
        )
        rewardAdObject = RewardAdObject(activity, adConfig)
        rewardAdObject?.setAdObjectListener(object :
            RewardAdObjectListener {
            override fun onLoadSuccess(placementId: String) {
                //广告加载成功,此时可以展示，建议展示前判断isReady
                logger.add("Reward: onLoadSuccess")
            }

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

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

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

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

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

            override fun onAdSkippedVideo() {
                logger.add("Reward: onAdSkippedVideo")
            }

            override fun onAdRewardArrived() {
                logger.add("Reward: onAdRewardArrived")
            }


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

    /**
     * 展示激励广告
     */
    fun show() {
        if (rewardAdObject?.isReady() == true) {
            rewardAdObject?.show(activity)
        } else {
            logger.add("Reward: 广告还未准备好")
        }
    }

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

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

}