#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 Interactor Set Layer Mask", directiveLibrary = DirectiveLibrary.XRI, directiveInfo = "This action will set the InteractionLayerMask of an interactor.")]
    public class XRInteractorSetLayerMask : Actions
    {
        // 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

        public InteractionLayerMask interactionLayerMask;
        public XRBaseInteractor xRInteractorToChange;

        private InteractionLayerMask _iniInteractionLayerMask;

        // 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("Interactor", typeof(XRBaseInteractor)) };
        }

        // 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("Interactor", typeof(XRBaseInteractor)) };
        }

        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 == "Interactor")
            {
                xRInteractorToChange = objectPassed as XRBaseInteractor;
            }
        }

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

            return false;
        }

        void ChangeLayer(XRBaseInteractor xRBaseInteractor)
        {
            if (xRBaseInteractor != null)
            {
                xRBaseInteractor.interactionLayers = interactionLayerMask;
            }
        }

        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> { "Interactor" }, new List<object> { xRInteractorToChange });
        }
    }
}
#endif
