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

    /// <summary>
    /// Purchase limit information. A limit of 0 means there is no limit.
    /// Always present on IAPProduct; all 0 when the information is unavailable.
    /// </summary>
    /// <remarks>Since 3.1.0</remarks>
    [Serializable]
    public class IAPPurchaseLimit
    {
        /// <summary>Purchased count on the account</summary>
        public int AccountPurchasedCount { get; }

        /// <summary>Purchase limit on the account (0 = no limit)</summary>
        public int AccountLimit { get; }

        /// <summary>Purchased count on the character</summary>
        public int CharacterPurchasedCount { get; }

        /// <summary>Purchase limit on the character (0 = no limit)</summary>
        public int CharacterLimit { get; }

        /// <summary>Total purchased count on the world (all users, not only this one)</summary>
        public int WorldPurchasedCount { get; }

        /// <summary>Purchase limit on the world (0 = no limit)</summary>
        public int WorldLimit { get; }

        public IAPPurchaseLimit()
            : this(0, 0, 0, 0, 0, 0)
        {
        }

        public IAPPurchaseLimit(int accountPurchasedCount,
                                int accountLimit,
                                int characterPurchasedCount,
                                int characterLimit,
                                int worldPurchasedCount,
                                int worldLimit)
        {
            AccountPurchasedCount = accountPurchasedCount;
            AccountLimit = accountLimit;
            CharacterPurchasedCount = characterPurchasedCount;
            CharacterLimit = characterLimit;
            WorldPurchasedCount = worldPurchasedCount;
            WorldLimit = worldLimit;
        }

        /// <summary>
        /// Missing, null or non-numeric members fall back to 0, so a partial payload is not discarded.
        /// </summary>
        public IAPPurchaseLimit(Dictionary<string, object> dictionary)
        {
            AccountPurchasedCount = GetInt(dictionary, "accountPurchasedCount");
            AccountLimit = GetInt(dictionary, "accountLimit");
            CharacterPurchasedCount = GetInt(dictionary, "characterPurchasedCount");
            CharacterLimit = GetInt(dictionary, "characterLimit");
            WorldPurchasedCount = GetInt(dictionary, "worldPurchasedCount");
            WorldLimit = GetInt(dictionary, "worldLimit");
        }

        public Dictionary<string, object> ToDictionary()
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            dictionary.Add("accountPurchasedCount", AccountPurchasedCount);
            dictionary.Add("accountLimit", AccountLimit);
            dictionary.Add("characterPurchasedCount", CharacterPurchasedCount);
            dictionary.Add("characterLimit", CharacterLimit);
            dictionary.Add("worldPurchasedCount", WorldPurchasedCount);
            dictionary.Add("worldLimit", WorldLimit);
            return dictionary;
        }

        public override string ToString()
        {
            return "IAPPurchaseLimit(AccountPurchasedCount=" + AccountPurchasedCount
                + ", AccountLimit=" + AccountLimit
                + ", CharacterPurchasedCount=" + CharacterPurchasedCount
                + ", CharacterLimit=" + CharacterLimit
                + ", WorldPurchasedCount=" + WorldPurchasedCount
                + ", WorldLimit=" + WorldLimit
                + ")";
        }

        // MiniJSON yields long for integers and double for decimals.
        private static int GetInt(Dictionary<string, object> dictionary, string key)
        {
            if (dictionary == null || !dictionary.ContainsKey(key))
            {
                return 0;
            }

            object value = dictionary[key];
            if (value is long || value is int || value is double || value is float)
            {
                try
                {
                    return Convert.ToInt32(value);
                }
                catch (OverflowException)
                {
                    return 0;
                }
            }
            return 0;
        }
    }
}
