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

    /**
     * Game Profile.
     * @param characterNumber The unique number of the character.
     * @param characterId The unique Id of the character.
     * @param world World of game.
     * @since 2.0.0
     */
    public class GameProfile
    {
        /**
         * The unique number of the character.
         * @since 2.0.0
         */
        public long CharacterNumber { get; set; }

        /**
         * World of game.
         * @since 2.0.0
         */
        public string World { get; set; }

        internal bool needToCallAuthInterface = false;

        private Dictionary<string, object> properties;
        /**
         * Update additional information of the game
         * @since 2.1.0
         */
        public Dictionary<string, object> Properties {
            get
            {
                return properties;
            }
            set
            {
                properties = value;

                if(needToCallAuthInterface)
                {
                    string propertiesJsonString = Json.Serialize(value);
                    AuthInterface.SetGameProfileProperties(propertiesJsonString);
                }
            }
        }

        /**
         * Dictionary deserialization.
         * @since 2.0.0
         */
        public GameProfile(Dictionary<string, object> dictionary)
        {
            if (dictionary.ContainsKey("characterNumber"))
            {
                CharacterNumber = Convert.ToInt64(dictionary["characterNumber"]);
            }

            if (dictionary.ContainsKey("world"))
            {
                World = dictionary["world"] as string;
            }

            if (dictionary.ContainsKey("properties"))
            {
                properties = dictionary["properties"] as Dictionary<string, object>;
            }
        }

        public GameProfile()
        {

        }

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

            dictionary.Add("characterNumber", CharacterNumber);

            if (World != null)
            {
                dictionary.Add("world", World);
            }

            if (Properties != null)
            {
                dictionary.Add("properties", Properties);
            }

            return dictionary;
        }

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

            return Json.Serialize(dictionary);
        }

        public override string ToString()
        {
            string propertiesString = "";
            if(Properties != null)
            {
                propertiesString = Json.Serialize(Properties);
            }
            return "GameProfile(CharacterNumber=" + CharacterNumber + ", World='" + World + "', Properties=" + propertiesString + ")";
        }
    }

}
