using System.Globalization;
using KwaiAds.Scripts.Api.Interstitial;
using UnityEngine;

public class InterstitialAdLoadListener : IInterstitialAdLoadListener
{
    private const int maxLoadCount = 3;
    private int currentLoadCount = 0;
    
    public void OnAdLoadFailed(string trackId, int code, string msg)
    {
        // 受国内环境限制，国内无法请求到海外广告，需要加白。可以将trackId反馈给对接同学进行加白。| Due to the limitations of the domestic environment, it is not possible to request overseas advertisements in China, and it is necessary to add white. You can feedback the trackId to the contact peroson to add white.
        Debug.LogFormat($"InterstitialAdLoadListener#OnAdLoadFailed , trackId:{trackId}, code:{code}, msg:{msg}");
        currentLoadCount++;
        if (currentLoadCount < maxLoadCount)
        {
            KwaiAdsManager.LoadRewardAd();
        }
    }

    public void OnAdLoadStart(string trackId)
    {
        Debug.Log($"InterstitialAdLoadListener#OnAdLoadStart , trackId:{trackId}");
    }

    public void OnAdLoadSuccess(string trackId, string price)
    {
        // price 单位是$(美元，ecpm) | price in $ (dollars, ecpm)
        Debug.Log($"InterstitialAdLoadListener#OnAdLoadSuccess , trackId:{trackId}, price:{price}");
        if (double.TryParse(price, NumberStyles.Float, CultureInfo.InvariantCulture, out double result))
        {
            KwaiAdsManager.interstitiaAdRevenue = result;
            Debug.Log($"InterstitialAdLoadListener#OnAdLoadSuccess , trackId:{trackId}, price:{price}");
        }
    }
}