# if USING_MRTK2

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.UI;
using Microsoft.MixedReality.Toolkit.Input.Utilities;
using System.Linq;
using Microsoft.MixedReality.Toolkit.Utilities.Solvers;

namespace SimplifyXR
{
    public class MRTK2PanelConfiguration : IXRPanelConfig

    {
        public MRTK2PanelConfiguration() { }

        public XRCompatibility Compatibility()
        {
            return XRCompatibility.MRTK2;
        }

        public void RemoveConfiguration(GameObject parent, XRPanelType panelType)
        {
            var canvas = parent.GetComponentInChildren<Canvas>();

            if (canvas != null)
            {
                var cu = canvas.gameObject.GetComponent<CanvasUtility>();
                var  neartouch = canvas.gameObject.GetComponent<NearInteractionTouchableUnityUI>();
                var billboard = canvas.gameObject.GetComponent<Microsoft.MixedReality.Toolkit.UI.Billboard>();
                var om = canvas.gameObject.GetComponent<ObjectManipulator>();
                var cm = canvas.gameObject.GetComponent<ConstraintManager>();

                if (cu != null) MonoBehaviour.DestroyImmediate(cu);
                if (neartouch != null) MonoBehaviour.DestroyImmediate(neartouch);
                if (billboard != null) MonoBehaviour.DestroyImmediate(billboard);
                if (om != null) MonoBehaviour.DestroyImmediate(om);
                if (cm != null) MonoBehaviour.DestroyImmediate(cm);

                var buttons = parent.GetComponentsInChildren<Button>(true);
                buttons.ToList().ForEach(b =>
                {
                    var mapping = b.gameObject.GetComponent<MRTK2ClickMapping>();
                    if (mapping != null) MonoBehaviour.DestroyImmediate(mapping);
                });

                var sliders = parent.GetComponentsInChildren<Slider>(true);
                sliders.ToList().ForEach(s => {
                    var mapping = s.gameObject.GetComponent<MRTK2SliderMapping>();
                    if (mapping != null) MonoBehaviour.DestroyImmediate(mapping);
                });

                RemoveHandMenuConfiguration(parent);

            }
        }

        public void RemoveConfiguration(List<Button> buttons)
        {
           
            buttons.ToList().ForEach(b =>
            {
                var mapping = b.gameObject.GetComponent<MRTK2ClickMapping>();
                if (mapping != null) MonoBehaviour.DestroyImmediate(mapping);
            });
        }

        public void SetupConfiguration(GameObject parent, XRPanelType panelType)
        {
            var canvas = parent.GetComponentInChildren<Canvas>();

            if (canvas != null)
            {
                canvas.renderMode = RenderMode.WorldSpace;
                canvas.worldCamera = null;

                canvas.gameObject.GetOrAddNewComponent<CanvasUtility>();
                canvas.gameObject.GetOrAddNewComponent<NearInteractionTouchableUnityUI>();
                if (panelType != XRPanelType.handAnchored) canvas.gameObject.GetOrAddNewComponent<Microsoft.MixedReality.Toolkit.UI.Billboard>();
                canvas.gameObject.GetOrAddNewComponent<GraphicRaycaster>();
                if (panelType != XRPanelType.handAnchored) canvas.gameObject.GetOrAddNewComponent<ObjectManipulator>();
                if (panelType != XRPanelType.handAnchored) canvas.gameObject.GetOrAddNewComponent<ConstraintManager>();

                var eventSystem = parent.GetComponentInChildren<EventSystem>();
                if(eventSystem != null) eventSystem.enabled = false;

                var standaloneInput = parent.GetComponentInChildren<StandaloneInputModule>();
                if (standaloneInput != null) MonoBehaviour.DestroyImmediate(standaloneInput);

                var buttons = parent.GetComponentsInChildren<Button>(true);

                buttons.ToList().ForEach(b =>
                {
                    b.gameObject.GetOrAddNewComponent<MRTK2ClickMapping>();
                });


                var sliders = parent.GetComponentsInChildren<Slider>(true);
                sliders.ToList().ForEach(s => {
                    s.gameObject.GetOrAddNewComponent<MRTK2SliderMapping>();
                });

                if (panelType == XRPanelType.handAnchored)
                {
                    SetupHandMenuConfiguration(parent);
                }
                else
                {
                    RemoveHandMenuConfiguration(parent);
                }

            }
        }

        public void SetupHandMenuConfiguration(GameObject panel)
        {
            var canvas = panel.GetComponentInChildren<Canvas>();
            canvas.gameObject.GetOrAddNewComponent<HandConstraintPalmUp>();
            canvas.gameObject.GetOrAddNewComponent<MRTK2_3HandMenuEvents>();

            var solverHandler = canvas.GetComponent<SolverHandler>();
            solverHandler.TrackedTargetType = Microsoft.MixedReality.Toolkit.Utilities.TrackedObjectType.HandJoint;
            solverHandler.TrackedHandJoint = Microsoft.MixedReality.Toolkit.Utilities.TrackedHandJoint.ThumbProximalJoint;
        }

        public void RemoveHandMenuConfiguration(GameObject panel)
        {
            var canvas = panel.GetComponentInChildren<Canvas>();

            var handConstraint = canvas.GetComponent<HandConstraintPalmUp>();
            var handsBounds = canvas.GetComponent<HandBounds>();
            var solverHandler = canvas.GetComponent<SolverHandler>();
            var mrtkMenuEvents = canvas.GetComponent<MRTK2_3HandMenuEvents>();

            if (handConstraint) MonoBehaviour.DestroyImmediate(handConstraint);
            if (handsBounds) MonoBehaviour.DestroyImmediate(handsBounds);
            if (solverHandler) MonoBehaviour.DestroyImmediate(solverHandler);
            if (mrtkMenuEvents) MonoBehaviour.DestroyImmediate(mrtkMenuEvents);
        }

        public void SetupConfiguration(List<Button> buttons)
        {
            buttons.ToList().ForEach(b =>
            {
                b.gameObject.GetOrAddNewComponent<MRTK2ClickMapping>();
            });
        }
    }
}

#endif
