using System.Runtime.InteropServices;
using UnityEngine;

public class AdsterRewardedInterstitialAd : MonoBehaviour
{
    public static AdsterRewardedInterstitialAd Instance { get; private set; }

    public event System.Action<string> OnLoaded;
    public event System.Action<string> OnLoadFailed;
    public event System.Action<string> OnShowFailed;
    public event System.Action<string> OnWillShow;
    public event System.Action<string> OnClosed;
    public event System.Action<string> OnClicked;
    public event System.Action<string> OnImpression;
    public event System.Action<string> OnVideoStarted;
    public event System.Action<string> OnVideoEnded;
    public event System.Action<string> OnUserEarnedReward;

#if UNITY_IOS && !UNITY_EDITOR
    [DllImport("__Internal")] private static extern void load_rewarded_interstitial_ad(string placementKey);
    [DllImport("__Internal")] private static extern void show_rewarded_interstitial_ad();
#endif

    private void Awake()
    {
        if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); }
        else { Destroy(gameObject); }
    }

    public void LoadRewardedInterstitialAd(string placementKey)
    {
        Debug.Log($"LoadRewardedInterstitialAd with placement: {placementKey}");
#if UNITY_IOS && !UNITY_EDITOR
        load_rewarded_interstitial_ad(placementKey);
#else
        Debug.Log($"load_rewarded_interstitial_ad({placementKey}) (simulated in editor)");
        Invoke(nameof(SimulateRewardedInterstitialLoaded), 2f);
#endif
    }

    public void ShowRewardedInterstitialAd()
    {
        Debug.Log("ShowRewardedInterstitialAd called...");
#if UNITY_IOS && !UNITY_EDITOR
        show_rewarded_interstitial_ad();
#else
        Debug.Log("show_rewarded_interstitial_ad (simulated in editor)");
        Invoke(nameof(SimulateRewardedInterstitialCompleted), 3f);
#endif
    }

    public void HandleRewardedInterstitialAdLoaded(string result) { Debug.Log($"Rewarded interstitial ad loaded: {result}"); OnLoaded?.Invoke(result); }
    public void HandleRewardedInterstitialAdLoadFailed(string error) { Debug.LogError($"Rewarded interstitial ad load failed: {error}"); OnLoadFailed?.Invoke(error); }
    public void HandleRewardedInterstitialAdShowFailed(string error) { Debug.LogError($"Rewarded interstitial ad show failed: {error}"); OnShowFailed?.Invoke(error); }
    public void HandleRewardedInterstitialAdWillShow(string result) { Debug.Log($"Rewarded interstitial ad will show: {result}"); OnWillShow?.Invoke(result); }
    public void HandleRewardedInterstitialAdClosed(string result) { Debug.Log($"Rewarded interstitial ad closed: {result}"); OnClosed?.Invoke(result); }
    public void HandleRewardedInterstitialAdClicked(string result) { Debug.Log($"Rewarded interstitial ad clicked: {result}"); OnClicked?.Invoke(result); }
    public void HandleRewardedInterstitialAdImpression(string result) { Debug.Log($"Rewarded interstitial ad impression: {result}"); OnImpression?.Invoke(result); }
    public void HandleRewardedInterstitialAdVideoStarted(string result) { Debug.Log($"Rewarded interstitial ad video started: {result}"); OnVideoStarted?.Invoke(result); }
    public void HandleRewardedInterstitialAdVideoEnded(string result) { Debug.Log($"Rewarded interstitial ad video ended: {result}"); OnVideoEnded?.Invoke(result); }
    public void HandleRewardedInterstitialUserEarnedReward(string rewardData) { Debug.Log($"Rewarded interstitial user earned reward: {rewardData}"); OnUserEarnedReward?.Invoke(rewardData); }

    private void SimulateRewardedInterstitialLoaded() => HandleRewardedInterstitialAdLoaded("Success");
    private void SimulateRewardedInterstitialCompleted()
    {
        HandleRewardedInterstitialUserEarnedReward("100:coins");
        HandleRewardedInterstitialAdClosed("Success");
    }
}
