#if USING_XRI

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;

namespace SimplifyXR
{
    /// <summary>
    /// Insert summary of purpose of this Directive and any appropriate remarks.
    /// </summary>

    // The DirectiveCategory and SubCategory attriubte determines how the Directive is displayed in the Node Editor right-click create menu
    [DirectiveCategory(DirectiveCategories.Action, DirectiveSubCategory.GameObject, prettyName = "XR Socket Interactor Set Socket Active ", directiveLibrary = DirectiveLibrary.XRI, directiveInfo = "This action will enable or disable whether this socket can interact.")]
    public class XRSocketInteractorSetSocketActive : Actions, ICanToggle
    {
        // Public fields will be displayed in the Inspector when using the Node Editor
        // Tooltips for users when they hover over the field in the Inspector. Surround with the UNITY_EDITOR direcctive

#if UNITY_EDITOR
        [Tooltip("If Socket Active will be enabled, disabled or toggled")]
#endif
        public SimplifyXREnums.EnabledStateChoices SocketActiveEnabledState = SimplifyXREnums.EnabledStateChoices.Enable;
        public XRSocketInteractor xRSocketInteractorToChange;

        // ReceiveKeywords are how the developer can control what type of data is received to this Directive and how that is labeled in the Node Editor.
        public override List<KnobKeywords> ReceiveKeywords()
        {
            // KnobKeywords consist of the string label for the Node Editor and the System.Type of the data that can be used
            return new List<KnobKeywords> { new KnobKeywords("XRSocketInteractor", typeof(XRSocketInteractor)), new KnobKeywords("SocketActive", typeof(bool)) };
        }

        // SendKeywords are how the developer can control what type of data is sent from this Directive and how that is labeled in the Node Editor.
        public override List<KnobKeywords> SendKeywords()
        {
            // KnobKeywords consist of the string label for the Node Editor and the System.Type of the data that can be used
            return new List<KnobKeywords> { new KnobKeywords("XRSocketInteractor", typeof(XRSocketInteractor)), new KnobKeywords("SocketActive", typeof(bool)) };
        }

        public override void Execute()
        {
            FindObjectPassed();
            if (CheckIfObjectExists())
            {
                SendData();
            }

            /* Must call this event when execution of this Action is complete to pass control on to whatever is next to execute. */
            ThisActionCompleted();
        }


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

            if (KeywordInUse == "XRSocketInteractor")
            {
                xRSocketInteractorToChange = objectPassed as XRSocketInteractor;

            }
            else if (KeywordInUse == "SocketActive")
            {
                //TODO handle   
            }
        }

        bool CheckIfObjectExists()
        {
            if (xRSocketInteractorToChange != null)
            {
                GetGameObject();
                return true;
            }
            else
                SimplifyXRDebug.SimplifyXRLog(SimplifyXRDebug.Type.AuthorError, "No XR Socket Interactor specified or passed for {0}", SimplifyXRDebug.Args(this));

            return false;
        }

        void GetGameObject()
        {
            ChangeGameObject(xRSocketInteractorToChange);
        }

        void ChangeGameObject(XRSocketInteractor xRSocketInteractor)
        {
            if (xRSocketInteractor != null)
            {
                PickEnabledState(xRSocketInteractor);
            }
        }

        void PickEnabledState(XRSocketInteractor xRSocketInteractor)
        {
            if (SocketActiveEnabledState == SimplifyXREnums.EnabledStateChoices.Disable)
            {
                xRSocketInteractor.socketActive = false;
            }
            else if (SocketActiveEnabledState == SimplifyXREnums.EnabledStateChoices.Enable)
            {
                xRSocketInteractor.socketActive = true;
            }
            else if (SocketActiveEnabledState == SimplifyXREnums.EnabledStateChoices.Toggle)
            {
                xRSocketInteractor.socketActive = !xRSocketInteractor.socketActive;
            }
        }

        void SendData()
        {
            // When sending data from this Directive, use the AddPassableData method.
            // You will send a List<string> from your SendKeywords to be used as labels in the Node Editor,
            // and a corresponding List<object>, which is the data you desire to pass along.
            AddPassableData(new List<string> { "XRSocketInteractor", "SocketActive" }, new List<object> { xRSocketInteractorToChange, xRSocketInteractorToChange.socketActive });
        }


        #region ICanToggle implementation
        string IModify<ICanToggle>.ModifyObjectName { get { return "Enabled state"; } }

        public void SetTrue()
        {
            SocketActiveEnabledState = SimplifyXREnums.EnabledStateChoices.Enable;
        }

        public void SetFalse()
        {
            SocketActiveEnabledState = SimplifyXREnums.EnabledStateChoices.Disable;
        }

        public void Toggle()
        {
            SocketActiveEnabledState = SimplifyXREnums.EnabledStateChoices.Toggle;
        }
        #endregion
    }
}
#endif