using System;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using AppsFlyerSDK;

public static class AdHelper
{
    public static double GetRoundedEcpm(double ecpm) => Math.Round(ecpm, 10);

    public static double GetIncrementedPriceFloor(double loadedEcpm, double defaultPriceFloor, PriceFloorIncrementStrategy strategy, double step)
    {
        if (loadedEcpm <= 0) return defaultPriceFloor;

        return strategy switch
        {
            PriceFloorIncrementStrategy.Fixed => loadedEcpm + step,
            PriceFloorIncrementStrategy.Percentage => loadedEcpm + loadedEcpm * (step / 100),
        };
    }

    public static void Log(string message)
    {
        if (!Debug.isDebugBuild) return;
        Debug.Log($"[Ads] {message}");
    }

    public static void LogError(string message) => Debug.LogError($"[Ads] {message}");

    public static void LogAdRevenue(AdInfo info)
    {
        if (AppsFlyer.isSDKStopped()) return;

        Log($"[AdHelper] [Method] LogAdRevenue(adType: {info.AdType})");

        var additionalParams = new Dictionary<string, string>
        {
            { "ad_type", info.AdType },
            { "ad_unit", info.AdUnitId },
            { "mediation_id", info.MediationId },
            { "network_ad_unit", info.NetworkAdUnitId },
            { "placement", info.Placement },
            { "revenue_precision", info.RevenuePrecision },
            { "session_id", SessionController.Instance.SessionUuid },
            { "session_uuid", SessionController.Instance.SessionUuid },
            { "session_uptime", SessionController.Instance.SessionUptime.ToString() }
        };

        var adRevenueData = new AFAdRevenueData(info.NetworkName, MediationNetwork.IronSource, info.CurrencyCode, info.Revenue);
        AppsFlyer.logAdRevenue(adRevenueData, additionalParams);

        additionalParams.Add("currency", info.CurrencyCode);
        additionalParams.Add("network_name", info.NetworkName);
        additionalParams.Add("revenue", info.Revenue.ToString(CultureInfo.InvariantCulture));

        AppsFlyer.sendEvent("impression", additionalParams);
    }
}
