﻿#if UNITY_IOS && !UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

using Platform101XP.Serialization;
using Platform101XP.Entities;

namespace Platform101XP.Platforms
{
    public class IOSPlatform : IPlatform
    {
        public Token GetToken()
        {
            var json = NativeGetToken();
            return string.IsNullOrEmpty(json) ? null : Json.Deserialize<Token>(json);
        }

        public Account GetAccount()
        {
            var json = NativeGetAccount();
            return string.IsNullOrEmpty(json) ? null : Json.Deserialize<Account>(json);
        }

        public Product GetProduct(string productId)
        {
            var json = NativeGetProduct(productId);
            return string.IsNullOrEmpty(json) ? null : Json.Deserialize<Product>(json);
        }

        public void Initialize()
        {
            NativeInitialize();
        }

        public void ShowTroublesDialog()
        {
            // Not implemented.
        }

        public void ApplicationInvite()
        {
            NativeApplicationInvite();
        }

        public void ApplicationReview()
        {
            NativeApplicationReview();
        }

        public void ForcedApplicationReview()
        {
            NativeForcedApplicationReview();
        }

        public void Authorize()
        {
            NativeAuthorize();
        }

        public void GetSocialFriends()
        {
            NativeGetSocialFriends();
        }

        public void PurchaseProduct(string productId, string serverId, Dictionary<string, string> properties)
        {
            NativePurchaseProduct(productId, serverId, Json.Serialize(properties));
        }

        public void FinishPurchase(PurchaseResult purchaseResult)
        {
            NativeFinishPurchase(Json.Serialize(purchaseResult));
        }

        public void SendInviteLink(string eventName)
        {
            // NOTE: Depricated
        }

        public void ShowProfile()
        {
            NativeShowProfile();
        }

        public void LogEvent(string message)
        {
            NativeLogEvent(message);
        }

        public void SendEventToAnalytic(string eventName, Dictionary<string, string> eventProperties)
        {
            NativeSendEventToAnalytic(eventName, Json.Serialize(eventProperties));
        }

        public void ShowMessage(string message)
        {
            NativeShowMessage(message);
        }

        public void RestoreProducts()
        {
            NativeRestoreProducts();
        }

        public void PrefetchProduct(string productId)
        {
            NativePrefetchProduct(productId);
        }

        public void GetProducts(List<string> productIds)
        {
            Dictionary<string, string> dictionary = null;
            if (productIds != null)
            {
                dictionary = new Dictionary<string, string>();
                foreach (var value in productIds)
                {
                    dictionary.Add(value, value);
                }
            }

            NativeGetProducts(dictionary != null ? Json.Serialize(dictionary) : null);
        }

        public void CrashLog(string tag, string text)
        {
            NativeCrashLog(tag, text);
        }

        public void ShowWebStore(Dictionary<string, string> purchaseProperties, List<string> allowedLocale)
        {
            var jsonPurchaseProperties = Json.Serialize(purchaseProperties);
            NativeShowWebStore(jsonPurchaseProperties);
        }

        public void CheckMobilePurchase(string itemName)
        {
            // Not implemented
        }

        public void TrackSentryEvent(string eventName, Dictionary<string, object> values)
        {
            string jsonValues = Json.Serialize(values);
            NativeTrackSentryEvent(eventName, jsonValues);
        }

        public void TrackSentryException(Exception exception)
        {
            string exceptionName = exception.GetType().FullName;
            string exceptionMessage = exception.Message;
            NativeTrackSentryError(exceptionName, exceptionMessage);
        }

        // MARK: - Advertising

        public void LoadRewardedAd(string id)
        {
            // Not implemented
        }

        public void ShowRewardedAd()
        {
            // Not implemented
        }

        public bool IsRewardedVideoLoaded()
        {
            // Not implemented
            return false;
        }



        public void LoadInterstitialAd(string id)
        {
            // Not implemented
        }

        public void ShowInterstitialAd()
        {
            // Not implemented
        }

        public bool IsInterstitialAdLoaded()
        {
            // Not implemented
            return false;
        }



        public void LoadBannerAd(string id)
        {
            // Not implemented
        }

        public void ShowBannerAd()
        {
            // Not implemented
        }

        public void HideBannerAd()
        {
            // Not implemented
        }

        public bool IsBannerAdLoaded()
        {
            // Not implemented
            return false;
        }



        public void LoadRewardedInterstitialAd(string id)
        {
            // Not implemented       
        }

        public void ShowRewardedInterstitialAd()
        {
            // Not implemented
        }



        public void ResetConsentInformation()
        {
            // Not implemented
        }

        // MARK: - Native methods

        [DllImport("__Internal")]
        private static extern string NativeGetToken();

        [DllImport("__Internal")]
        private static extern string NativeGetAccount();   

        [DllImport("__Internal")]
        private static extern void NativeInitialize();

        [DllImport("__Internal")]
        private static extern void NativeApplicationInvite();

        [DllImport("__Internal")]
        private static extern void NativeApplicationReview();

        [DllImport("__Internal")]
        private static extern void NativeAuthorize();

        [DllImport("__Internal")]
        private static extern void NativeGetSocialFriends();

        [DllImport("__Internal")]
        private static extern void NativePurchaseProduct(string productId, string serverId, string jsonProperties);

        [DllImport("__Internal")]
        private static extern void NativeShowProfile();

        [DllImport("__Internal")]
        private static extern void NativeLogEvent(string message);

        [DllImport("__Internal")]
        private static extern void NativeSendEventToAnalytic(string eventName, string jsonProperties);

        [DllImport("__Internal")]
        private static extern void NativeShowMessage(string message);

        [DllImport("__Internal")]
        private static extern void NativeForcedApplicationReview();

        [DllImport("__Internal")]
        private static extern void NativeRestoreProducts();

        [DllImport("__Internal")]
        private static extern void NativePrefetchProduct(string productId);

        [DllImport("__Internal")]
        private static extern string NativeGetProduct(string productId);

        [DllImport("__Internal")]
        private static extern void NativeGetProducts(string jsonProductIds);

        [DllImport("__Internal")]
        private static extern void NativeFinishPurchase(string jsonPurchase);

        [DllImport("__Internal")]
        private static extern void NativeCrashLog(string tag, string text);

        [DllImport("__Internal")]
        private static extern void NativeShowWebStore(string jsonPurchaseProperties);

        [DllImport("__Internal")]
        private static extern void NativeTrackSentryEvent(string eventName, string jsonValues);

        [DllImport("__Internal")]
        private static extern void NativeTrackSentryError(string name, string message);
    }
}
#endif