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

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

        public const int GoogleServerError = 50001;
        public const int APNSError = 50002;

        /**
         * Register with Stove Push Server.
         * @param token A stable identifier that uniquely identifies the app instance.
         * @param action Register result.
         * @see Result
         * @since 2.0.0
         */
        public static void RegisterPush(string token, Action<Result> action)
        {
            Debug.Log("Push.RegisterPush");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                Debug.Log("Push.RegisterPush Result : " + result);
                action.Invoke(result);
            });
            PushInterface.RegisterPush(token, identifier);
        }

        /**
         * Fetch push's settings on the push's server.
         * @param action Fetch result.
         * @see Result
         * @see PushSettings
         * @since 2.0.0
         */
        public static void FetchPushSettings(Action<Result, PushSettings> action)
        {
            Debug.Log("Push.FetchPushSettings");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                PushSettings pushSettings = null;
                if (payload.ContainsKey("pushSettings"))
                {
                    pushSettings = new PushSettings(payload["pushSettings"] as Dictionary<string, object>);
                }

                Debug.Log("Push.FetchPushSettings Result : " + result);
                Debug.Log("Push.FetchPushSettings PushSettings : " + pushSettings);

                action.Invoke(result, pushSettings);
            });
            PushInterface.FetchPushSettings(identifier);
        }

        /**
         * Update push's settings to push's server.
         * @param pushSettings Push's settings.
         * @param action Update result.
         * @see Result
         * @see PushSettings
         * @since 2.0.0
         */
        public static void UpdatePushSettings(PushSettings pushSettings, Action<Result, PushSettings> action)
        {
            Debug.Log("Push.UpdatePushSettings");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                PushSettings payloadPushSettings = null;
                if (payload.ContainsKey("pushSettings"))
                {
                    payloadPushSettings = new PushSettings(payload["pushSettings"] as Dictionary<string, object>);
                }

                Debug.Log("Push.UpdatePushSettings Result : " + result);
                Debug.Log("Push.UpdatePushSettings PushSettings : " + payloadPushSettings);

                action.Invoke(result, payloadPushSettings);
            });
            PushInterface.UpdatePushSettings(pushSettings.ToJSONString(), identifier);
        }

        /**
         * Set a listener to receive push data.
         *
         * When a push is received while the app is running, it delivers the push's data to the listener.
         * @param action Listener to receive push data.
         * @since 2.0.0
         */
        public static void SetMessageDelegate(Action<Dictionary<string, string>> action)
        {
            Debug.Log("Push.SetMessageDelegate");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Dictionary<string, string> message = payload["message"] as Dictionary<string, string>;

                Debug.Log("Push.SetMessageDelegate Message : " + message);

                action.Invoke(message);
            });
            PushInterface.SetMessageDelegate(identifier);
        }

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

            string version = PushInterface.GetVersion();

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

            return version;
        }
    }
}
