using IndieExchange;
using UnityEngine;

// Import the Quick Start sample, then Tools > Indie Exchange > Create sample scene.
public sealed class IndieAdsExample : MonoBehaviour
{
    public IndieAdsSettings settings;
    private bool busy;
    private string message = "Starting...";
    private void Start()
    {
        // This test scene has no personal-data consent flow. Live games should initialize after theirs.
        message = IndieAds.Initialize(settings) ? "Ready to preload. Press the button at an ad break." : "Open Tools > Indie Exchange > Setup.";
    }

    private async void ShowOpportunity()
    {
        if (busy) return;
        busy = true;
        message = "Checking this opportunity...";
        var handled = await IndieAds.TryShowInterstitialAsync();
        if (this == null) return;
        if (!handled) ShowNormalAd();
        else message = "Exchange ad closed. Resume gameplay.";
        busy = false;
    }

    private void ShowNormalAd()
    {
        // Replace this with your existing AdMob, LevelPlay, or other provider's code.
        message = "Fallback reached immediately. Your normal ad code runs here.";
    }

    private void OnGUI()
    {
        GUILayout.BeginArea(new Rect(30,30, Mathf.Min(Screen.width-60,640), Screen.height-60), GUI.skin.box);
        GUILayout.Label("Indie Exchange integration sample");
        GUILayout.Label("Local test mode sends no requests or credits. Live mode downloads your game settings once at launch.");
        GUILayout.Space(12);
        GUILayout.Label("Interstitial ready: " + IndieAds.IsReady(IndieAdFormat.Interstitial));
        GUI.enabled = !busy;
        if (GUILayout.Button("Eligible ad opportunity", GUILayout.Height(50))) ShowOpportunity();
        if (GUILayout.Button("Preload (does not count)", GUILayout.Height(40))) IndieAds.Preload(IndieAdFormat.Interstitial);
        GUI.enabled = true;
        GUILayout.Label(message);
        var stats = IndieAds.GetStatistics(IndieAdFormat.Interstitial);
        GUILayout.Label("Opportunities: " + stats.opportunities + " | Selected: " + stats.selected + " | Shown: " + stats.shown);
        GUILayout.Label("Remainder: " + stats.remainder + " | Selected but unavailable: " + stats.unavailable);
        GUILayout.EndArea();
    }

    private void OnDestroy() { IndieAds.Shutdown(); }
}

