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

    [Serializable]
    public class IAPProduct
    {
        public enum ProductType
        {
            InAppPurchase = 0,
            Subscription = 1,
        };

        public enum ProductState
        {
            Available = 0,
            Purchased = 1,
            Waiting = 2,
            Disabled = 3,
        };

        public ProductType Type { get; }
        public string ProductIdentifier { get; }
        public string StoveProductId { get; }
        public string LocalizedTitle { get; }
        public string LocalizedDescription { get; }
        public string Price { get; }
        public string PriceCurrencyCode { get; }
        public double PriceAmountMicros { get; }
        public ProductState State { get; }
        public bool IsGift { get; }
        public bool IsLimit { get; }
        public bool IsRewarded { get; }
        public string SubscriptionPeriod { get; }
        public List<string> FreeTrialPeriods { get; }

        public IAPProduct(ProductType type,
                            string productIdentifier,
                            string stoveProductId,
                            string localizedTitle,
                            string localizedDescription,
                            string price,
                            string priceCurrencyCode,
                            double priceAmountMicros,
                            ProductState state,
                            bool isGift,
                            bool isLimit,
                            bool isRewarded,
                            string subscriptionPeriod,
                            List<string> freeTrialPeriods)
        {
            Type = type;
            ProductIdentifier = productIdentifier;
            StoveProductId = stoveProductId;
            LocalizedTitle = localizedTitle;
            LocalizedDescription = localizedDescription;
            Price = price;
            PriceCurrencyCode = priceCurrencyCode;
            PriceAmountMicros = priceAmountMicros;
            State = state;
            IsGift = isGift;
            IsLimit = isLimit;
            IsRewarded = isRewarded;
            SubscriptionPeriod = subscriptionPeriod;
            FreeTrialPeriods = freeTrialPeriods;
        }

        public IAPProduct(Dictionary<string, object> dictionary)
        {
            Type = (ProductType)Convert.ToInt32(dictionary["type"]);
            ProductIdentifier = dictionary["productIdentifier"] as string;
            StoveProductId = dictionary["stoveProductId"] as string;

            if (dictionary.ContainsKey("localizedTitle"))
            {
                LocalizedTitle = dictionary["localizedTitle"] as string;
            }
            else
            {
                LocalizedTitle = "";
            }

            if (dictionary.ContainsKey("localizedDescription"))
            {
                LocalizedDescription = dictionary["localizedDescription"] as string;
            }
            else
            {
                LocalizedDescription = "";
            }

            if (dictionary.ContainsKey("price"))
            {
                Price = dictionary["price"] as string;
            }
            else
            {
                Price = "";
            }

            if (dictionary.ContainsKey("priceCurrencyCode"))
            {
                PriceCurrencyCode = dictionary["priceCurrencyCode"] as string;
            }
            else
            {
                PriceCurrencyCode = "";
            }

            if (dictionary.ContainsKey("priceAmountMicros"))
            {
                PriceAmountMicros = Convert.ToDouble(dictionary["priceAmountMicros"]);
            }
            else
            {
                PriceAmountMicros = 0;
            }

            if (dictionary.ContainsKey("state"))
            {
                State = (ProductState)Convert.ToInt32(dictionary["state"]);
            }
            else
            {
                State = ProductState.Available;
            }

            if (dictionary.ContainsKey("isGift"))
            {
                IsGift = Convert.ToBoolean(dictionary["isGift"]);
            }
            else
            {
                IsGift = false;
            }

            if (dictionary.ContainsKey("isLimit"))
            {
                IsLimit = Convert.ToBoolean(dictionary["isLimit"]);
            }
            else
            {
                IsLimit = false;
            }

            if (dictionary.ContainsKey("isRewarded"))
            {
                IsRewarded = Convert.ToBoolean(dictionary["isRewarded"]);
            }
            else
            {
                IsRewarded = false;
            }

            if (dictionary.ContainsKey("subscriptionPeriod"))
            {
                SubscriptionPeriod = dictionary["subscriptionPeriod"] as string;
            }
            else
            {
                SubscriptionPeriod = "";
            }

            List<string> freeTrialPeriods = new List<string>();
            if (dictionary.ContainsKey("freeTrialPeriods"))
            {
                List<object> freeTrialPeriodList = dictionary["freeTrialPeriods"] as List<object>;
                foreach (object obj in freeTrialPeriodList)
                {
                    string freeTrialPeriod = obj as string;
                    freeTrialPeriods.Add(freeTrialPeriod);
                }
            }
            FreeTrialPeriods = freeTrialPeriods;

        }

        public Dictionary<string, object> ToDictionary()
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            dictionary.Add("type", Convert.ToInt32(Type));
            dictionary.Add("productIdentifier", ProductIdentifier);
            dictionary.Add("stoveProductId", StoveProductId);
            dictionary.Add("localizedTitle", LocalizedTitle);
            dictionary.Add("localizedDescription", LocalizedDescription);
            dictionary.Add("price", Price);
            dictionary.Add("priceCurrencyCode", PriceCurrencyCode);
            dictionary.Add("priceAmountMicros", PriceAmountMicros);
            dictionary.Add("state", Convert.ToInt32(State));
            dictionary.Add("isGift", IsGift);
            dictionary.Add("isLimit", IsLimit);
            dictionary.Add("isRewarded", IsRewarded);
            dictionary.Add("subscriptionPeriod", SubscriptionPeriod);
            dictionary.Add("freeTrialPeriods", FreeTrialPeriods);
            return dictionary;
        }

        public string ToJSONString()
        {
            Dictionary<string, object> dictionary = ToDictionary();

            return Json.Serialize(dictionary);
        }

        public override string ToString()
        {
            return "IAPProduct(Type='" + Type
                + "', ProductIdentifier='" + ProductIdentifier
                + "', StoveProductId='" + StoveProductId
                + "', LocalizedTitle='" + LocalizedTitle
                + "', LocalizedDescription='" + LocalizedDescription
                + "', Price='" + Price
                + "', PriceCurrencyCode='" + PriceCurrencyCode
                + "', PriceAmountMicros=" + PriceAmountMicros
                + ", State=" + State
                + ", IsGift=" + IsGift
                + ", IsLimit=" + IsLimit
                + ", IsRewarded=" + IsRewarded
                + ", SubscriptionPeriod=" + SubscriptionPeriod
                + ", FreeTrialPeriods=[" + string.Join(",", FreeTrialPeriods.ToArray())
                + "])";
        }
    }
}

