#if USING_XRI

using System.Collections;
using System.Collections.Generic;
using System;
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 Get Interaction GameObject", directiveLibrary = DirectiveLibrary.XRI, directiveInfo = "This action will get the gameobject(s) of the interaction")]
    public class XRGetInteractionGameObject : 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

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

        // 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(GameObject)), new KnobKeywords("Interactable", typeof(GameObject)), new KnobKeywords("Interaction", typeof(List<GameObject>)) };
        }

        // Use the protected keyword in front of any Monobehaviour methods

        /* The Execute method is the entry point and first thing that occurs when this Action is told to execute. */
        public override void Execute()
        {
            FindObjectPassed();

            /* 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 == "BaseInteractionEvent")
            {
                BaseInteractionEventArgs args = objectPassed as BaseInteractionEventArgs;
                SendData(args);
            }
        }

        void SendData(BaseInteractionEventArgs args)
        {
            GameObject interactor = args.interactorObject.transform.gameObject;
            GameObject interactable = args.interactableObject.transform.gameObject;
            List<GameObject> interaction = new List<GameObject> { interactor, interactable};
            //Debug.Log("[" + this.name + "]: " + "XRGetInteractionGameObject -> " + "SendData -> " + interactor.name + "," + interactable.name + "," + interaction + ")");

            // 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", "Interactable", "Interaction" }, new List<object> { interactor, interactable, interaction });
        }
    }
}
#endif
