using System.Runtime.InteropServices;
using UnityEngine;

/// <summary>
/// Base class for Adster SDK functionality
/// </summary>
public abstract class AdsterBase : MonoBehaviour
{
#if UNITY_IOS && !UNITY_EDITOR
    [DllImport("__Internal")]
    protected static extern void print_hello_swift();
    
    [DllImport("__Internal")]
    protected static extern void initilalize_sdk();
    
    [DllImport("__Internal")]
    protected static extern void load_banner_ad(string placementKey);
    
    [DllImport("__Internal")]
    protected static extern void show_banner_ad(int placement);
    
    [DllImport("__Internal")]
    protected static extern void hide_banner_ad();
    
    [DllImport("__Internal")]
    protected static extern void destroy_banner_ad();
    
    [DllImport("__Internal")]
    protected static extern void load_interstitial_ad(string placementKey);
    
    [DllImport("__Internal")]
    protected static extern void show_interstitial_ad();
    
    [DllImport("__Internal")]
    protected static extern void load_rewarded_ad(string placementKey);
    
    [DllImport("__Internal")]
    protected static extern void show_rewarded_ad();
    
    [DllImport("__Internal")]
    protected static extern void load_rewarded_interstitial_ad(string placementKey);
    
    [DllImport("__Internal")]
    protected static extern void show_rewarded_interstitial_ad();
#endif

    public static AdsterBase Instance { get; protected set; }

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

    public virtual void TestSwiftFunction()
    {
        Debug.Log("Testing Swift function...");
#if UNITY_IOS && !UNITY_EDITOR
        print_hello_swift();
#else
        Debug.Log("hello swift (simulated in editor)");
#endif
    }

    public virtual void InitializeSDK()
    {
        Debug.Log("InitializeSDK function...");
#if UNITY_IOS && !UNITY_EDITOR
        initilalize_sdk();
#else
        Debug.Log("initialize_sdk (simulated in editor)");
        Invoke(nameof(SimulateInitialized), 1f);
#endif
    }

    private void SimulateInitialized()
    {
        if (this is AdsterManager manager)
        {
            manager.HandleInitialized("Success");
        }
    }
}
