#if UNITY_ANDROID
﻿namespace Stove.StoveSDK
{
    using System;
    using System.Collections.Generic;
    using UnityEngine;
    using MiniJSON;

    /**
     * Google Play Games Provider by the Stove.
     * @since 2.5.0
     */
    public class GPGProvider : Provider
    {
        /**
         * Google Play Games Provider's domain
         * @since 2.5.0
         */
        public const string Domain = "com.stove.auth.googleplaygames";

        private string SessionId;
        public Dictionary<string, string> User { get; set; }
        private Dictionary<string, object> Account;

        public GPGProvider(string sessionId)
        {
            SessionId = sessionId;
        }

        public GPGProvider(string sessionId, Dictionary<string, object> account)
        {
            SessionId = sessionId;
            Account = account;

            Dictionary<string, string> userinfo = new Dictionary<string, string>();
            userinfo.Add("recall_token", account["recall_token"] as string);
            User = userinfo;
        }

        public string ClassName
        {
            get
            {
#if UNITY_ANDROID
            return "com.stove.auth.googleplaygames.GPGProvider";
#else
            return "";
#endif
            }
        }

        /**
         * Request a list of stove accounts linked to a PGS ID
         * 
         * @param action Fetch Result.
         * @see Result
         * @since 2.5.0
         */
        public void Fetch(Action<Result, List<Dictionary<string, object>>> action)
        {
            Debug.Log("GPGProvider.Fetch Action(" + action + ")");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                List<object> accounts = payload["accounts"] as List<object>;


                List<Dictionary<string, object>> returnAccounts = new List<Dictionary<string, object>>();
                foreach (object account in accounts)
                {
                    Dictionary<string, object> newAccount = new Dictionary<string, object>();
                    foreach (KeyValuePair<string, object> keyValuePair in account as Dictionary<string, object>)
                    {
                        newAccount.Add(keyValuePair.Key, keyValuePair.Value);
                    }

                    returnAccounts.Add(newAccount);
                }

                Debug.Log("GPGProvider.Fetch Result(" + result + ")");
                action.Invoke(result, returnAccounts);
            });
            GPGProviderInterface.Fetch(SessionId, identifier);
        }

        /**
         * Shows the account import pop-up.
         * 
         * @param account Account you want to import
         * @param action Load Result.
         * @see Result
         * @since 2.5.0
         */
        public void Load(Dictionary<string, object> account, Action<Result, GPGProvider> action)
        {
            Debug.Log("GPGProvider.Load Action(" + action + ")");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                GPGProvider provider = new GPGProvider(payload["provider"] as Dictionary<string, object>);
                Debug.Log("GPGProvider.Load Result(" + result + ")");
                action.Invoke(result, provider);
            });
            GPGProviderInterface.Load(SessionId, Json.Serialize(account), identifier);
        }

        /**
         * Dictionary serialization.
         * @since 2.5.0
         */
        public GPGProvider(Dictionary<string, object> dictionary)
        {
            SessionId = dictionary["sessionId"] as string;
            if (dictionary.ContainsKey("userInfo"))
            {
                Dictionary<string, string> userinfo = new Dictionary<string, string>();
                Dictionary<string, object> userInfoObject = dictionary["userInfo"] as Dictionary<string, object>;
                foreach (KeyValuePair<string, object> keyValuePair in userInfoObject)
                {
                    userinfo.Add(keyValuePair.Key, keyValuePair.Value as string);
                }
                User = userinfo;
            }
        }

        /**
         * Dictionary deserialization.
         * @since 2.5.0
         */
        public Dictionary<string, object> ToDictionary()
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            dictionary.Add("sessionId", SessionId);
            
            if (User != null)
            {
                dictionary.Add("userInfo", User);
            }

            return dictionary;
        }

        /**
         * JSON serialization.
         * @since 2.5.0
         */
        public string ToJSONString()
        {
            Dictionary<string, object> dictionary = ToDictionary();

            return Json.Serialize(dictionary);
        }
    }
}
#endif