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

    /**
     * GameLog provided by the Stove.
     * @since 2.8.4
     */
    public class GameLog
    {
        /**
         * Send events to the stove game log collector.
         *
         * Please use after initializing Stove SDK.
         * @param logEvent GameLog's Event containing parameters and additionalParameters.
         * @param action Callback for add result.
         * @see Result
         * @since 2.8.4
         */
        public static void Add(GameLogEvent logEvent, Action<Result> action)
        {
            Debug.Log("GameLog.Add");

            // Check if logEvent is null
            if (logEvent == null)
            {
                Debug.LogError("GameLog.Add failed: logEvent is null");
                action?.Invoke(new Result("com.stove.cancel", 10, "Canceled"));
                return;
            }

            // Check if logEvent has parameters (required)
            bool hasParameters = logEvent.Parameters != null && logEvent.Parameters.Count > 0;
            if (!hasParameters)
            {
                Debug.LogError("GameLog.Add failed: GameLogEvent.Parameters is required");
                action?.Invoke(new Result("com.stove.cancel", 10, "Canceled"));
                return;
            }

            try
            {
                string jsonString = logEvent.ToJSONString();

                string identifier = StoveSDK.Instance.AddAction(payload =>
                {
                    try
                    {
                        var resultDictionary = payload["result"] as Dictionary<string, object>;
                        if (resultDictionary == null)
                        {
                            Debug.LogError("GameLog.Add callback failed: result is null");
                            action?.Invoke(new Result("com.stove.cancel", 10, "Canceled"));
                            return;
                        }

                        Result result = new Result(resultDictionary);

                        Debug.Log("Log.Add Result : " + result);

                        action?.Invoke(result);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError("GameLog.Add callback failed: " + e.Message);
                        action?.Invoke(new Result("com.stove.cancel", 10, "Canceled"));
                    }
                });

                LogInterface.AddGameLog(jsonString, identifier);
            }
            catch (Exception e)
            {
                Debug.LogError("GameLog.Add failed: " + e.Message);
                action?.Invoke(new Result("com.stove.cancel", 10, "Canceled"));
            }
        }

        /**
         * Sends all pending game logs immediately.
         * @since 2.8.4
         */
        public static void Flush()
        {
            try
            {
                Debug.Log("GameLog.Flush");
                LogInterface.FlushGameLog();
            }
            catch (Exception e)
            {
                Debug.LogError("GameLog.Flush failed: " + e.Message);
            }
        }
    }
}

