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

#if USING_TMP
using TMPro;
#endif

namespace SimplifyXR
{
    /// <summary>
    /// Changes the Text field of an UI Text component
    /// </summary>
    [DirectiveCategory(DirectiveCategories.Action, DirectiveSubCategory.UI, 
        prettyName ="Change UI Text",
        directiveInfo="This action will change the text of a <b>Text</b> or " +
        "<b>Text Mesh Pro</b> component.")]
    public class ChangeUIText : Actions
    {
      
        /// <summary>
        /// Text to change or pass
        /// </summary>
        #if UNITY_EDITOR
        [Tooltip("Text to change or pass.")]
        #endif
        public string NewText;

        /// <summary>
        /// The type of text to modify
        /// </summary>
        //#if USING_TMP
        [Tooltip("The type of text to modify.")]
        public TextType textType;
        //#endif

        /// <summary>
        /// The Text field to change
        /// </summary>
        #if UNITY_EDITOR
        [Tooltip("The Text field to change."), 
            Conditional("textType", TextType.Standard, ComparisonType.Equals)]
        #endif
        public Text TextFieldToChange;

    #if USING_TMP
        /// <summary>
        /// The Text mesh field to change
        /// </summary>
        #if UNITY_EDITOR
        [Tooltip("The Text mesh field to change."),
            Conditional("textType", TextType.TextMeshPro, ComparisonType.Equals)]
        #endif
        public TMP_Text TextMeshFieldToChange;

        /// <summary>
        /// The Text mesh field to change
        /// </summary>
#if UNITY_EDITOR
        [Tooltip("The Text mesh field to change."),
            Conditional("textType", TextType.TextMeshProUGUI, ComparisonType.Equals)]
#endif
        public TextMeshProUGUI TextMeshUGUIFieldToChange;


#endif

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

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

        public override void Execute()
        {
            GetNewText();
            TextChangeHandler();         
            SendData();
            ThisActionCompleted();
        }

        void TextChangeHandler()
        {
            switch (textType)
            {
                case TextType.Standard:
                    if (TextFieldToChange != null)
                        TextFieldToChange.text = NewText;
                    else
                        SimplifyXRDebug.SimplifyXRLog(
                            SimplifyXRDebug.Type.AuthorError, 
                            "There is no Text field to change on this Directive {0}.",
                            SimplifyXRDebug.Args(this));
                    break;

                #if USING_TMP
                case TextType.TextMeshPro:
                    if (TextMeshFieldToChange != null)
                        TextMeshFieldToChange.text = NewText;
                    else
                        SimplifyXRDebug.SimplifyXRLog(
                            SimplifyXRDebug.Type.AuthorError, 
                            "There is no Text mesh pro field to change on this Directive {0}.", 
                            SimplifyXRDebug.Args(this));
                    break;
                case TextType.TextMeshProUGUI:
                    if (TextMeshUGUIFieldToChange != null)
                        TextMeshUGUIFieldToChange.text = NewText;
                    else
                        SimplifyXRDebug.SimplifyXRLog(
                            SimplifyXRDebug.Type.AuthorError,
                            "There is no Text mesh pro field to change on this Directive {0}.",
                            SimplifyXRDebug.Args(this));
                break;
                #endif
            }
        }

            void GetNewText()
        {
            var objectPassed = GetPassableData();
            if (objectPassed == null) return;
            
            if (objectPassed != null)
            {
                if (KeywordInUse == "GameObjectForName")
                {
                    var myObject = objectPassed as GameObject;
                    NewText = myObject.name;
                }
                else if (KeywordInUse == "NewText")
                {
                    NewText = objectPassed as string;
                }
            }
        }

        void SendData()
        {
            AddPassableData(new List<string> { "NewText" }, new List<object> { NewText });
        }
    }
}