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

    /**
     * Log provided by the Stove.
     * @since 2.0.0
     */
    public class Log
    {
        /**
         * Log Provider's domain
         * @since 2.0.0
         */
        public const string Domain = "com.stove.log";

        /**
         *
         * Send events to the stove log collector.
         *
         * Please use after initializing Stove SDK.
         * @param logEvent Log's Event
         * @param action Add result.
         * @see Result
         * @since 2.0.0
         */
        public static void Add(LogEvent logEvent, Action<Result> action)
        {
            Debug.Log("Log.Add");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);

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

                action.Invoke(result);
            });

            LogInterface.Add(logEvent.ToJSONString(), identifier);
        }

        /**
         * Transfer accumulated logs.
         * @return Whether to send logs.
         * @since 2.0.0
         */
        public static void Flush()
        {
            Debug.Log("Log.Flush");
            LogInterface.Flush();            
        }

        /**
        * Log Version
        * @since 2.3.0
        */
        public static string GetVersion()
        {
            Debug.Log("GetVersion");

            string version = LogInterface.GetVersion();

            Debug.Log("GetVersion return : " + version);

            return version;
        }

        public static Dictionary<string, object> GetSDKVersions()
        {
            Debug.Log("GetSDKVersions");

            string version = LogInterface.GetSDKVersions();

            Debug.Log("GetSDKVersions :" + version);

            Dictionary<string, object> sdkVersions = Json.Deserialize(version) as Dictionary<string, object>;

            return sdkVersions;
        }

        /**
         * Set External ID
         * @param externalId External ID
         * @since 2.8.2
         */
        public static void SetExternalId(string externalId)
        {
            Debug.Log("Log.SetExternalId : " + externalId);
            LogInterface.SetExternalId(externalId);
        }
    }
}

