#if USING_MRTK3 && USING_XRI

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.XR.Interaction.Toolkit.UI;
using System.Linq;
using Unity.XR.CoreUtils;
using UnityEngine.XR.Interaction.Toolkit;
using Microsoft.MixedReality.Toolkit.SpatialManipulation;
using System.Threading.Tasks;
using Microsoft.MixedReality.Toolkit;

namespace SimplifyXR
{
    public class MRTK3PanelConfiguration : XRIPanelConfiguration, IXRPanelConfig
    {
        public new XRCompatibility Compatibility()
        {
            return XRCompatibility.MRTK3;
        }

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

            if (canvas != null)
            {

                var eventSystem = panel.GetComponentInChildren<EventSystem>();
                var xrui = eventSystem?.gameObject?.GetComponent<XRUIInputModule>();
                var trackedDeviceRaycaster = canvas.GetComponent<TrackedDeviceGraphicRaycaster>();

                if (xrui != null) MonoBehaviour.DestroyImmediate(xrui);
                if (trackedDeviceRaycaster != null) MonoBehaviour.DestroyImmediate(trackedDeviceRaycaster);

                var billboard = canvas.GetComponent<SimplifyXR.Billboard>();
                if (billboard != null) MonoBehaviour.DestroyImmediate(billboard);

                var xrorigin = Object.FindObjectOfType<XROrigin>();
                if (xrorigin != null)
                {
                    try
                    {
                        var es = xrorigin.gameObject.GetComponent<EventSystem>();
                        if (es != null && es.transform != xrui.transform) MonoBehaviour.DestroyImmediate(es);
                    }
                    catch (System.Exception e)
                    {

                    }

                }

                var boundsControl = canvas.gameObject.GetComponent<BoundsControl>();
                var om = canvas.gameObject.GetComponent<ObjectManipulator>();

                var bc = canvas.gameObject.GetComponent<BoxCollider>();
                var xrgi = canvas.gameObject.GetComponent<XRGrabInteractable>();
                var cr = canvas.gameObject.GetComponent<CanvasRenderer>();

                if (xrgi) MonoBehaviour.DestroyImmediate(xrgi);
                if (boundsControl != null) MonoBehaviour.DestroyImmediate(boundsControl);
                if (om != null) MonoBehaviour.DestroyImmediate(om);
                if (bc) MonoBehaviour.DestroyImmediate(bc);
                if (cr) MonoBehaviour.DestroyImmediate(cr);

                RemoveInteractableButtons(canvas);
                UnconfigureSliders(canvas);

                RemoveHandMenuConfiguration(panel);

            }
        }

        public new void RemoveConfiguration(List<Button> buttons)
        {
        }

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

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

                SetupEventSystems(panel);

                if(panelType != XRPanelType.handAnchored) canvas.gameObject.GetOrAddNewComponent<SimplifyXR.Billboard>().PivotAxis = PivotAxis.Y;
                canvas.gameObject.GetOrAddNewComponent<TrackedDeviceGraphicRaycaster>();

                var bc = canvas.gameObject.GetOrAddNewComponent<BoxCollider>();
                var rect = canvas.GetComponentInChildren<ElementRoot>().GetComponent<RectTransform>();               

                var canvasRenderer = canvas.gameObject.GetOrAddNewComponent<CanvasRenderer>();
                var boundsControl = canvas.gameObject.GetOrAddNewComponent<BoundsControl>();
                var oc = canvas.gameObject.GetOrAddNewComponent<ObjectManipulator>();
                oc.colliders.Clear();
                oc.colliders.Add(bc);

                SetBoxColliderToRectScale(bc, rect);
                SetupButtonsAsInteractable(canvas);
                SetupSliders(canvas);

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

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

            if (bc) MonoBehaviour.DestroyImmediate(bc);

            var solverHandler = canvas.GetComponent<SolverHandler>();
            solverHandler.TrackedTargetType = TrackedObjectType.HandJoint;
            solverHandler.TrackedHandJoint = TrackedHandJoint.ThumbProximal;
        }

        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 new void SetupConfiguration(List<Button> buttons)
        {
        }

    }
}

#endif


