﻿using UnityEngine;
using Adverty5.Utils;
using System.Collections.Generic;

namespace Adverty5
{
    public class AdvertySettings : ScriptableObjectSingleton<AdvertySettings, AdvertySettingsFactory>
    {
        public List<PlatformSetting> platformSettings = new List<PlatformSetting>();

        [SerializeField]
        private bool sandboxMode;

        [SerializeField]
        private AdPlacementTextureSize adPlacementTextureMaxSize = AdPlacementTextureSize.Medium;

        public static string APIKey
        {
            get {
                if (Application.isEditor)
                {
                    return Instance.GetAPIKeyForEditor();
                }
                else
                {
                    return Instance.GetAPIKey();
                }
            }
        }

        public static bool SandboxMode
        {
            get {
                return Instance.sandboxMode;
            }
            set {
                Instance.sandboxMode = value;
            }
        }

        public static AdPlacementTextureSize AdPlacementTextureMaxSize
        {
            get {
                return Instance.adPlacementTextureMaxSize;
            }
            set {
                Instance.adPlacementTextureMaxSize = value;
            }
        }

        public static void SetAPIKeys(string androidKey, string iosKey, string otherKey)
        {
            Instance.platformSettings = new List<PlatformSetting> {
                new PlatformSetting { platform = SupportedPlatform.Android, apiKey = androidKey },
                new PlatformSetting { platform = SupportedPlatform.iOS, apiKey = iosKey },
                new PlatformSetting { platform = SupportedPlatform.Other, apiKey = otherKey }
            };
        }

        private string GetAPIKeyForEditor()
        {
            PlatformSetting platformSetting = platformSettings.Find(setting =>
                                                                    { return !string.IsNullOrEmpty(setting.apiKey); });
            if (platformSetting.Equals(default(PlatformSetting)))
            {
                return string.Empty;
            }

            return platformSetting.apiKey;
        }

        private string GetAPIKey()
        {
            SupportedPlatform platform;
            switch (Application.platform)
            {
            case RuntimePlatform.Android:
                platform = SupportedPlatform.Android;
                break;
            case RuntimePlatform.IPhonePlayer:
                platform = SupportedPlatform.iOS;
                break;
            default:
                platform = SupportedPlatform.Other;
                break;
            }

            PlatformSetting platformSetting = platformSettings.Find(setting =>
                                                                    { return setting.platform == platform; });
            if (platformSetting.Equals(default(PlatformSetting)))
            {
                return string.Empty;
            }

            return platformSetting.apiKey;
        }
    }
}
