#if USING_MRTK2

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.UI;
using UnityEngine.EventSystems;



namespace SimplifyXR
{

    public class MRTK2ClickMapping : XRClickMapping, 
        IMixedRealityTouchHandler
    {
        ObjectManipulator objectManipulator;

        public override void OnPointerEnter(PointerEventData eventData)
        {
            objectManipulator = this.gameObject.GetComponentInParent<ObjectManipulator>();
            if (objectManipulator != null) objectManipulator.enabled = false;
        }

        public override void OnPointerExit(PointerEventData eventData)
        {
            objectManipulator = this.gameObject.GetComponentInParent<ObjectManipulator>();
            if (objectManipulator != null) objectManipulator.enabled = true;
        }


        // Start is called before the first frame update
        public new void Start()
        {
            base.Start();
            SetupMRTK2InteractionHandlers();
        }

        public void ReinitObjectManipulator()
        {
            if (objectManipulator) objectManipulator.enabled = true;

        }

        public void OnTouchStarted(HandTrackingInputEventData eventData)
        {
            var timeDelta = Time.time - lastTouch;
            if (timeDelta >= touchDelay) OnClick();
            Debug.Log("Touch started");
            lastTouch = Time.time;
        }

        public void OnTouchCompleted(HandTrackingInputEventData eventData) {
            Debug.Log("Touch Completed");
        }

        public void OnTouchUpdated(HandTrackingInputEventData eventData) { }

        void SetupMRTK2InteractionHandlers()
        {
            this.gameObject.GetOrAddNewComponent<NearInteractionTouchable>();
            objectManipulator = this.gameObject.GetComponentInParent<ObjectManipulator>();
            //if (uGUIButton) uGUIButton.onClick.AddListener(ReinitObjectManipulator);
        }

        public void OnDisable()
        {
            ReinitObjectManipulator();
        }

        public void OnDestroy()
        {
            ReinitObjectManipulator();
        }

    }

}

#endif

