using System.Collections;
using System.Collections.Generic;
using KwaiAds.Scripts.Api.Interstitial;
using KwaiAds.Scripts.Api.Reward;
using UnityEngine;

public static class KwaiAdsManager
{
    //目前都是测试id
    private const string appId = "899999"; 
    private const string token = "EaCw0AipSYyvf3E7"; 
    private const string rewardAdUnitId = "8999996001";
    private const string interstitialAdUnitId = "8999996002";

    /// <summary>
    /// 目前是千倍广告价值
    /// </summary>
    public static double rewardAdRevenue = 0;
    /// <summary>
    /// 插屏奖励价值 如果有竞价功能可能会用到
    /// </summary>
    public static double interstitiaAdRevenue = 0;
    
    private static IRewardAdController rewardAdController;
    private static IInterstitialAdController interstitialAdController;
    
    public static void Init()
    {
        bool debug = true; // Whether in debug mode. Plsease set to false when in release build.
        var kwaiAdConfig = new KwaiAds.Scripts.Api.KwaiAdConfig.Builder()
            .SetAppId(appId)
            .SetToken(token)
            .SetAppName("App Name") // Optional
            .SetDebugLog(debug) // Optional
            .Build();
        
        KwaiAds.Scripts.Api.KwaiAdsSdk.Initialize(kwaiAdConfig, new InitResultCallbackImpl());
    }

    #region 激励广告

    /// <summary>
    /// 加载激励广告
    /// </summary>
    public static void LoadRewardAd()
    {
        if (rewardAdController != null)
        {
            rewardAdController.Destroy();
            rewardAdController = null;
            rewardAdRevenue = 0;
        }
        
        rewardAdController = KwaiAds.Scripts.Api.KwaiAdsSdk.SDK.getRewardAdController();
        KwaiRewardAdRequest kwaiRewardAdRequest = new KwaiRewardAdRequest(rewardAdUnitId);
        rewardAdController.Load(kwaiRewardAdRequest, new RewardAdListener(), new RewardAdLoadListener());
    }

    /// <summary>
    /// 展示激励广告
    /// </summary>
    public static void ShowRewardAd()
    {
        if (rewardAdController != null)
        {
            rewardAdController.Show();
        }
        else
        {
            LoadRewardAd();
        }
    }

    /// <summary>
    /// 广告播放完成 获得奖励
    /// </summary>
    public static void OnRewardAdCallback()
    {
        AdsSDKManager.instance.OnRewardAdCallback(rewardAdRevenue);
        LoadRewardAd();
    }

    #endregion

    #region 插屏

    public static void LoadInterstitialAd()
    {
        if (interstitialAdController != null)
        {
            interstitialAdController.Destroy();
            interstitialAdController = null;
            interstitiaAdRevenue = 0;
        }
        
        interstitialAdController = KwaiAds.Scripts.Api.KwaiAdsSdk.SDK.getInterstitialAdController();
        KwaiInterstitialAdRequest kwaiInterstitialAdRequest = new KwaiInterstitialAdRequest(interstitialAdUnitId);
        interstitialAdController.Load(kwaiInterstitialAdRequest, new InterstitialAdListener(), new InterstitialAdLoadListener());
    }

    public static void ShowInterstitialAd()
    {
        if (interstitialAdController != null)
        {
            interstitialAdController.Show();
        }
        else
        {
            LoadInterstitialAd();
        }
    }
    
    /// <summary>
    /// 广告播放完成 获得奖励
    /// </summary>
    public static void OnInterstitialCallback()
    {
        if (AdsSDKManager.instance.IsMoreAdsBidding)
        {
            AdsSDKManager.instance.OnRewardAdCallback(interstitiaAdRevenue);
        }
       
        LoadInterstitialAd();
    }

    #endregion
    
}

