using System;

namespace Davanci.Nexus
{
    // The single monetization facade: ads (AppLovin MAX) + in-app purchases (Unity IAP) in one
    // place. Games call DavanciMonetization.ShowRewardedAd / ShowInterstitialAd / ShowBanner and
    // DavanciMonetization.Buy / OwnsProduct. Ads route through NexusAds, IAP through NexusIAP, so
    // both compile with no SDK installed (fake providers in the editor).
    //
    //   DavanciMonetization.ShowRewardedAd("coins_x2", r => { if (r.Rewarded) GrantCoins(200); });
    //   DavanciMonetization.ShowInterstitialAd("level_end");
    //   DavanciMonetization.ShowBanner("hud_bottom");
    //   DavanciMonetization.Buy("remove_ads", r => { if (r.Success) RemoveAds(); });
    public static class DavanciMonetization
    {
        // ---- Ads -----------------------------------------------------------------------

        public static void InitializeAds(Action<bool> onReady = null) => NexusAds.Initialize(onReady);

        public static void InitializeAds(bool? attTrackingAuthorized, Action<bool> onReady = null)
            => NexusAds.Initialize(onReady, attTrackingAuthorized);

        // Mediation SDK finished initializing (after ATT when enabled). bool = success.
        public static event Action<bool> AdsInitialized
        {
            add => NexusAds.AdsInitialized += value;
            remove => NexusAds.AdsInitialized -= value;
        }

        // Fires when a rewarded ad becomes ready or is consumed (e.g. after a show).
        public static event Action<bool> RewardedReadyChanged
        {
            add => NexusAds.RewardedReadyChanged += value;
            remove => NexusAds.RewardedReadyChanged -= value;
        }

        // Fires when an interstitial becomes ready or is consumed.
        public static event Action<bool> InterstitialReadyChanged
        {
            add => NexusAds.InterstitialReadyChanged += value;
            remove => NexusAds.InterstitialReadyChanged -= value;
        }

        public static bool IsAdsReady => NexusAds.IsReady;

        // Set true after a "remove ads" purchase to silence interstitials + banners. Rewarded ads
        // keep working (they're an opt-in reward).
        public static bool AdsRemoved { get => NexusAds.AdsRemoved; set => NexusAds.AdsRemoved = value; }

        // Rewarded — the reward is only granted when r.Rewarded == true. Placement is an analytics
        // label (forwarded to DavanciNexus.AdWatched / AdFailed).
        public static void ShowRewardedAd(string placement, Action<NexusAdResult> onResult)
            => NexusAds.ShowRewarded(placement, onResult);
        public static bool IsRewardedReady(string placement = null) => NexusAds.IsRewardedReady;

        // Interstitial
        public static void ShowInterstitialAd(string placement, Action<NexusAdResult> onResult = null)
            => NexusAds.ShowInterstitial(placement, onResult);
        public static bool IsInterstitialReady(string placement = null) => NexusAds.IsInterstitialReady;

        // In-game banner / MREC overlay
        public static void ShowBanner(string placement = "banner") => NexusAds.ShowBanner(placement);
        public static void HideBanner() => NexusAds.HideBanner();

        // Opens the active mediation's on-device debugger (LevelPlay Test Suite / MAX Mediation
        // Debugger) for per-network diagnostics. Call after ads initialize, on a device.
        public static void LaunchAdTestSuite() => NexusAds.LaunchTestSuite();

        // ---- In-app purchases (same surface as DavanciNexus, kept here so monetization is one API) ----

        public static void InitializeIAP(Action<bool> onReady = null) => NexusIAP.Initialize(onReady);
        public static bool IsIapReady => NexusIAP.IsReady;
        public static void Buy(string productId, Action<NexusPurchaseResult> onResult) => NexusIAP.Buy(productId, onResult);
        public static void RestorePurchases(Action<bool> onDone = null) => NexusIAP.Restore(onDone);
        public static bool OwnsProduct(string productId) => NexusIAP.OwnsProduct(productId);
        public static NexusIapPrice GetPrice(string productId) => NexusIAP.GetPrice(productId);
        public static string PriceString(string productId) => NexusIAP.PriceString(productId);
    }
}
