﻿using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

#if USING_TMP
using TMPro; 
#endif


// Include the SimplifyXR namespace
namespace SimplifyXR
{

    public enum InputFieldType
    {
        Standard
#if USING_TMP
        , TextMeshPro
#endif
    }


    /// <summary>
    /// This action takes in the values of a username and password and attempts to cash them in
    /// for an access token to the web api
    /// </summary>
    [DirectiveCategory(DirectiveCategories.Action, DirectiveSubCategory.AppManager, prettyName ="Login with simpleAR ID",
        directiveInfo = "This action authenticates a user with the <b>simpleAR Pro Identity Service.</b>")]
    public class AppLogin : Actions, IChangeString
    {

        public InputFieldType inputFieldType;
        string username;
        string password;

#if USING_TMP
        [Conditional("inputFieldType", InputFieldType.Standard, ComparisonType.Equals)]
#endif
        [Tooltip("Text object containing the user's username input"), SceneTip]
        public InputField usernameInput;

#if USING_TMP
        [Conditional("inputFieldType", InputFieldType.TextMeshPro, ComparisonType.Equals)]
        [Tooltip("Text object containing the user's username input (TMP)"), SceneTip]
        public TMP_InputField usernameInputTMP;
#endif


#if USING_TMP
        [Conditional("inputFieldType", InputFieldType.Standard, ComparisonType.Equals)]
#endif
        [Tooltip("Text object containing the user's password input"), SceneTip]
        public InputField passwordInput;

#if USING_TMP
        [Conditional("inputFieldType", InputFieldType.TextMeshPro, ComparisonType.Equals)]
        [Tooltip("Text object containing the user's password input (TMP)"), SceneTip]
        public TMP_InputField passwordInputTMP;
#endif

        public override List<KnobKeywords> ReceiveKeywords()
        {
            return new List<KnobKeywords>() { new KnobKeywords("Username", typeof(string)) };
        }

        public override List<KnobKeywords> SendKeywords()
        {
            return new List<KnobKeywords> { };
        }
		/// <summary>
		/// Subscribe the action to the TokenManager's notification list
		/// </summary>
        public void Awake()
        {
            IdentityManager.Instance.IdentityBehavior = new AuthController();
            ((AuthController)IdentityManager.Instance.IdentityBehavior).SetupController();
        }

        public override void Execute()
        {
            GetData();
            GetUserInfo();
            username = null;
            password = null;
        }

        void GetData()
        {
            var objectPassed = GetPassableData();
            if (objectPassed == null) return;

            if (KeywordInUse == "Username")
            {
                username = objectPassed as string;
            }
        }


        public void InputTypeHandler()
        {
            switch (inputFieldType)
            {
                case InputFieldType.Standard:
                    if (usernameInput != null && !string.IsNullOrEmpty(usernameInput.text))
                    {
                        username = usernameInput.text;
                    }
                    if (passwordInput != null && !string.IsNullOrEmpty(passwordInput.text))
                    {
                        password = passwordInput.text;
                    }
                    break;
#if USING_TMP
                case InputFieldType.TextMeshPro:
                    if (usernameInputTMP != null && !string.IsNullOrEmpty(usernameInputTMP.text))
                    {
                        username = usernameInputTMP.text;
                    }
                    if (passwordInputTMP != null && !string.IsNullOrEmpty(passwordInputTMP.text))
                    {
                        password = passwordInputTMP.text;
                    }
                    break;
#endif
            }
        }




		//Get the information from the input fields
        void GetUserInfo()
        {
            // Use the Input fields if they are there
            InputTypeHandler();

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                SimplifyXRDebug.SimplifyXRLog(SimplifyXRDebug.Type.DeveloperDeepDebug, "Attempting login with Username {0} and Password {1}.", SimplifyXRDebug.Args(username, password));
                Login();
            }
            else
            {
                SimplifyXRDebug.SimplifyXRLog(SimplifyXRDebug.Type.AuthorError, "Username or password were empty.  Must supply username and password to login.");
                ThisActionCompleted(false);
            }
        }
		//Call the login behavior on the TokenManager
        void Login()
        {
            IdentityManager.Instance.Login(username, password);
            ThisActionCompleted();
        }

#region IChangeString implementation
        string IModify<IChangeString>.ModifyObjectName {get { return "Password";}}

        public void ChangeString(string s)
        {
            password = s;
        }
#endregion
    }
}
