using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEditor;
using UnityEngine.SceneManagement;

namespace SimplifyXR
{

    [CustomEditor(typeof(ObjectAnchorPanelFeatures), true)]
    public class ObjectAnchorPanelFeaturesEditor : BasePanelFeatureEditor
    {
        ObjectAnchorPanelFeatures objectAnchorPanel;
        bool expanded;

        private bool currentValueObjectAnchorAuto;
        private ObjectAnchorModelType currentValueAnchorModelType;

        public new void OnEnable()
        {

            objectAnchorPanel = (ObjectAnchorPanelFeatures)target;

            currentValueObjectAnchorAuto = objectAnchorPanel.autoAnchorObject;
            currentValueAnchorModelType = objectAnchorPanel.anchorModelType;

            Undo.undoRedoPerformed += HandleUndoRedo;

            base.OnEnable();
        }
     
        public new void OnDisable()
        {
            Undo.undoRedoPerformed -= HandleUndoRedo;

            base.OnDisable();
        }

        private void HandleUndoRedo()
        {
            //update current values, we may need more handling in the future
            currentValueObjectAnchorAuto = objectAnchorPanel.autoAnchorObject;
            currentValueAnchorModelType = objectAnchorPanel.anchorModelType;
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            if (GUI.changed && !Application.isPlaying)
            {
                ValidateAnchorObject();
            }
        }

        private void ValidateAnchorObjectModel()
        {
            //check model
            if (currentValueAnchorModelType != objectAnchorPanel.anchorModelType)
            {
                objectAnchorPanel.anchorObjectModel = null;
            }
            //else we changed model in inspector
            else
            {
                if (objectAnchorPanel.anchorModelType == ObjectAnchorModelType.gameObject)
                {
                    if (objectAnchorPanel.anchorObjectModel != null)
                    {
                        if (!(objectAnchorPanel.anchorObjectModel is GameObject))
                        {
                            if (objectAnchorPanel.anchorObjectModel is Mesh)
                            {
                                Debug.LogWarning("Object model detected as mesh, setting correct model type:" + objectAnchorPanel.anchorObjectModel.name);
                                objectAnchorPanel.anchorModelType = ObjectAnchorModelType.mesh;
                            }
                            else
                            {
                                //currently sets null, potential improvement to retain last valid object in inspector
                                Debug.LogError("Desired Object model is neither mesh or gameobject:" + objectAnchorPanel.anchorObjectModel.name);
                                objectAnchorPanel.anchorObjectModel = null;
                            }
                        }
                    }
                }
                else if (objectAnchorPanel.anchorModelType == ObjectAnchorModelType.mesh)
                {
                    if (objectAnchorPanel.anchorObjectModel != null)
                    {
                        if (!(objectAnchorPanel.anchorObjectModel is Mesh))
                        {
                            if (objectAnchorPanel.anchorObjectModel is GameObject)
                            {
                                Debug.LogWarning("Object model detected as game object, setting correct model type:" + objectAnchorPanel.anchorObjectModel.name);
                                objectAnchorPanel.anchorModelType = ObjectAnchorModelType.gameObject;
                            }
                            else
                            {
                                //currently sets null, potential improvement to retain last valid object in inspector
                                Debug.LogError("Desired Object model is neither mesh or gameobject:" + objectAnchorPanel.anchorObjectModel.name);
                                objectAnchorPanel.anchorObjectModel = null;
                            }
                        }
                    }
                }
            }
        }


        private void ValidateAnchorObject()
        {

            ValidateAnchorObjectModel(); 

            var autoGO = GameObject.Find("ObjectAnchor_Auto");            
            
            //check anchor object
            if (currentValueObjectAnchorAuto != objectAnchorPanel.autoAnchorObject)
            {
                //NOTE these will duplicate/be unable to delete Auto Anchors if the current Auto Anchor is disabled in editor
                //For future improvement we should tag this so it can be found when inactive
                if (objectAnchorPanel.autoAnchorObject)
                {
                    if (autoGO == null)
                        objectAnchorPanel.anchorGameObject = new GameObject("ObjectAnchor_Auto");
                    else
                        objectAnchorPanel.anchorGameObject = autoGO;
                }
                else
                {
                    if (autoGO != null)
                    {
                        //redundant check whether the objectAnchor is the autoGO, or if user has already set custom object 
                        if (autoGO == objectAnchorPanel.anchorGameObject)
                            objectAnchorPanel.anchorGameObject = null;

                        autoGO.transform.DetachChildren();
                        Undo.DestroyObjectImmediate(autoGO);
                    }
                }
            }
            else
            {
                //in case we just changed to custom object but haven't set autoAnchor off
                if (objectAnchorPanel.anchorGameObject != null && objectAnchorPanel.anchorGameObject != autoGO)
                {
                    objectAnchorPanel.autoAnchorObject = false;
                    if (autoGO != null)
                    {
                        autoGO.transform.DetachChildren();
                        Undo.DestroyObjectImmediate(autoGO);

                    }
                }
            }
            
            currentValueObjectAnchorAuto = objectAnchorPanel.autoAnchorObject;
            currentValueAnchorModelType = objectAnchorPanel.anchorModelType;
        }
    }
}

