#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 Ray Interactor Set Force Grab ", directiveLibrary = DirectiveLibrary.XRI, directiveInfo = "This action will enable to moving an object to you hand rather than interacting at a distance.")]
    public class XRRayInteractorSetForceGrab : 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 Force Grab will be enabled, disabled or toggled")]
#endif
        public SimplifyXREnums.EnabledStateChoices ForceGrabEnabledState = SimplifyXREnums.EnabledStateChoices.Enable;
        public XRRayInteractor xRRayInteractorToChange;

        // 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("XRRayInteractor", typeof(XRRayInteractor)), new KnobKeywords("UseForceGrab", 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("XRRayInteractor", typeof(XRRayInteractor)), new KnobKeywords("UseForceGrab", 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 == "XRRayInteractor")
            {
                xRRayInteractorToChange = objectPassed as XRRayInteractor;

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

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

            return false;
        }

        void GetGameObject()
        {
            ChangeGameObject(xRRayInteractorToChange);
        }

        void ChangeGameObject(XRRayInteractor xRRayInteractor)
        {
            if (xRRayInteractor != null)
            {
                PickEnabledState(xRRayInteractor);
            }
        }

        void PickEnabledState(XRRayInteractor xRRayInteractor)
        {
            if (ForceGrabEnabledState == SimplifyXREnums.EnabledStateChoices.Disable)
            {
                xRRayInteractor.useForceGrab = false;
            }
            else if (ForceGrabEnabledState == SimplifyXREnums.EnabledStateChoices.Enable)
            {
                xRRayInteractor.useForceGrab = true;
            }
            else if (ForceGrabEnabledState == SimplifyXREnums.EnabledStateChoices.Toggle)
            {
                xRRayInteractor.useForceGrab = !xRRayInteractor.useForceGrab;
            }
        }

        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> { "XRRayInteractor", "UseForceGrab" }, new List<object> { xRRayInteractorToChange, xRRayInteractorToChange.useForceGrab });
        }


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

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

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

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