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

    /**
     * Result of adding a game log (GameLog.Add).
     *
     * Mirrors the native StoveGameLogResult contract: keys "isSuccess", "isCancel", "errorMessage".
     * @since 3.0.0
     */
    public class GameLogResult
    {
        /**
         * The log was saved successfully.
         * @since 3.0.0
         */
        public bool IsSuccess { get; private set; }

        /**
         * Canceled by a disabled feature (batchSize 0) or a validation failure, etc.
         * @since 3.0.0
         */
        public bool IsCancel { get; private set; }

        /**
         * Failure message (non-null only on failure).
         * @since 3.0.0
         */
        public string ErrorMessage { get; private set; }

        /**
         * Constructs a result from the native result dictionary.
         * @param dictionary Dictionary with "isSuccess", "isCancel", "errorMessage" keys.
         * @since 3.0.0
         */
        public GameLogResult(Dictionary<string, object> dictionary)
        {
            if (dictionary == null)
            {
                IsCancel = true;
                return;
            }

            object isSuccess;
            if (dictionary.TryGetValue("isSuccess", out isSuccess) && isSuccess != null)
            {
                IsSuccess = System.Convert.ToBoolean(isSuccess);
            }

            object isCancel;
            if (dictionary.TryGetValue("isCancel", out isCancel) && isCancel != null)
            {
                IsCancel = System.Convert.ToBoolean(isCancel);
            }

            object errorMessage;
            if (dictionary.TryGetValue("errorMessage", out errorMessage) && errorMessage != null)
            {
                ErrorMessage = errorMessage.ToString();
            }
        }

        /**
         * Convenience constructor for a locally produced cancel result
         * (e.g. before Configure or on client-side validation failure).
         * @since 3.0.0
         */
        public static GameLogResult Cancel()
        {
            return new GameLogResult(new Dictionary<string, object> { { "isCancel", true } });
        }

        public override string ToString()
        {
            return "GameLogResult(IsSuccess=" + IsSuccess + ", IsCancel=" + IsCancel + ", ErrorMessage=" + ErrorMessage + ")";
        }
    }
}
