using System;
using UnityEngine;

namespace IndieExchange
{
    [Serializable]
    public sealed class IndiePlacement
    {
        public string id = "level_complete";
        public IndieAdFormat format = IndieAdFormat.Interstitial;
        [Range(0, 100)] public int percentage = 5;
        public string bannerSize = "320x50";
        public string bannerPosition = "top";
        internal string category;
        internal bool policyRoot;
        internal int publicPercentage;
    }

    public enum IndieAdFormat { Interstitial = 0, Rewarded = 1, Banner = 2 }

    [CreateAssetMenu(menuName = "Indie Exchange/Settings", fileName = "IndieAdsSettings")]
    public sealed class IndieAdsSettings : ScriptableObject
    {
        public const string ServiceUrl = "https://indie-exchange.com";
        [Tooltip("Copy the Game ID from My games on the Indie Exchange website.")]
        [InspectorName("Game ID")] public string gameId = "";
        [HideInInspector]
        public string apiBaseUrl = "https://indie-exchange.com";
        [HideInInspector] // Legacy serialized field. Live requests use Game ID.
        public string sdkKey = "";
        [HideInInspector]
        public bool autoInitialize = false;
        [Tooltip("Editor/development builds only. Displays a local test card and sends no tracking.")]
        public bool testMode = true;
        [HideInInspector]
        public bool allowDevelopmentHttp = false;
        [HideInInspector] // Legacy field retained for serialized assets. Server derives the country.
        public string countryCode = "ZZ";
        [HideInInspector] // Live device verification is unavailable in the Editor.
        public string editorPlatform = "android";
        [HideInInspector, Range(2, 30)] public int networkTimeoutSeconds = 8;
        [HideInInspector, Range(1, 15)] public int cacheMinutes = 5;
        [HideInInspector]
        [Range(1, 4)] public int maxCachedAds = 4;
        [HideInInspector] public IndiePlacement[] placements = {
            new IndiePlacement { id = "interstitial", percentage = 100 },
            new IndiePlacement { id = "banner", format = IndieAdFormat.Banner, percentage = 100 }
        };
        internal string session;
#if UNITY_EDITOR
        internal bool useLocalTestPlacements;
#endif

        internal IndieAdsSettings Snapshot()
        {
            var copy = Instantiate(this);
            copy.hideFlags = HideFlags.HideAndDontSave;
#if UNITY_EDITOR
            copy.useLocalTestPlacements = useLocalTestPlacements;
#endif
            copy.networkTimeoutSeconds = Mathf.Clamp(networkTimeoutSeconds, 2, 30);
            copy.cacheMinutes = Mathf.Clamp(cacheMinutes, 1, 15);
            copy.maxCachedAds = Mathf.Clamp(maxCachedAds, 1, 4);
            copy.apiBaseUrl = (Application.isEditor || Debug.isDebugBuild) && allowDevelopmentHttp
                ? (apiBaseUrl ?? "").Trim().TrimEnd('/') : ServiceUrl;
            copy.gameId = (gameId ?? "").Trim();
            copy.sdkKey = (sdkKey ?? "").Trim();
            copy.countryCode = NormalizeCountry(countryCode);
            // Instantiate clones serialized fields, including the placement array.
            return copy;
        }

        internal static string NormalizeCountry(string value)
        {
            var code = (value ?? "").Trim().ToUpperInvariant();
            return code.Length == 2 && code[0] >= 'A' && code[0] <= 'Z' &&
                code[1] >= 'A' && code[1] <= 'Z' ? code : "ZZ";
        }
    }
}
