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

    /**
     * Auth UI provided by the Stove.
     * @since 2.0.0
     */
    public class AuthUI
    {
        /**
         * Shows stove integrated login screen.
         *
         * Provides functions related to authentication.
         *
         * - Integrated Terms of Use, Register, Login, Auto login,  App update, inspection, User sanctions
         * @param action Login Result.
         * @see Result
         * @see AccessToken
         * @since 2.0.0
         */
        public static void Login(Action<Result, AccessToken> action)
        {
            Debug.Log("AuthUI.Login");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                AccessToken accessToken = Auth.GetAccessTokenFromDictionary(payload);
                Debug.Log("AuthUI.Login Result(" + result + "), AccessToken(" + accessToken + ")");
                action.Invoke(result, accessToken);
            });
            AuthUIInterface.Login(identifier);
        }

        /**
         * List of 3rd party provider to expose on login screen.
         * @see Provider
         * @since 2.0.0
         */
        public static List<Provider> Providers
        {
            set
            {
                Debug.Log("AuthUI.Providers : " + value);
                List<string> providerClassNameList = new List<string>();
                foreach (Provider provider in value)
                {
                    providerClassNameList.Add(provider.ClassName);
                }
                AuthUIInterface.SetProviders(Json.Serialize(providerClassNameList));
            }
        }

        /**
         * Shows Stove account withdrawal screen.
         *
         * If you successfully unsubscribe, your current account will be logged out.
         * @param action Withdraw Result.
         * @see Result
         * @since 2.0.0
         */
        public static void Withdraw(Action<Result> action)
        {
            Debug.Log("AuthUI.Withdraw");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                action.Invoke(result);
            });
            AuthUIInterface.Withdraw(identifier);
        }

        /**
         * Connect provider to current account.
         *
         * Guest account only.
         * @param action Link result.
         * @since 2.0.0
         */
        public static void Link(Action<Result> action)
        {
            Debug.Log("AuthUI.Link");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                action.Invoke(result);
            });
            AuthUIInterface.Link(identifier);
        }

        /**
         * Account manage view.
         *
         * @param action ManageAccount result.
         * @since 2.0.0
         */
        public static void ManageAccount(Action<Result> action)
        {
            Debug.Log("AuthUI.ManageAccount");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                action.Invoke(result);
            });
            AuthUIInterface.ManageAccount(identifier);
        }

        /**
         * Devices manage view.
         *
         * @param action ManageDevices result.
         * @since 2.7.1
         */
        public static void ManageDevices(Action<Result> action)
        {
            Debug.Log("ManageDevices");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                action.Invoke(result);
            });
            AuthUIInterface.ManageDevices(identifier);
        }

        /**
        * Shows Identity Verification.
        *
        * @param action VerifyIdentification result.
        * @since 2.8.1
        */
        public static void VerifyIdentification(bool compareIdentifier, Action<Result, string> action)
        {
            Debug.Log("VerifyIdentification");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                string state = null;
                if (payload.ContainsKey("state"))
                {
                    state = payload["state"] as string;
                }
                action.Invoke(result, state);
            });
            AuthUIInterface.VerifyIdentification(identifier, compareIdentifier);
        }

        /**
         * Shows SwitchAccount.
         *
         * @param action SwitchAccount result.
         * @since 2.8.3
         */
        public static void SwitchAccount(Action<Result> action)
        {
            Debug.Log("SwitchAccount");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                action.Invoke(result);
            });
            AuthUIInterface.SwitchAccount(identifier);
        }

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

            string version = AuthUIInterface.GetVersion();

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

            return version;
        }
    }
}


 