﻿#if CVR_CCK_EXISTS
using UnityEngine;
using UnityEditor;

namespace CVR.CCKEditor.Hacks
{
    [CustomEditor(typeof(Animator))]
    public class CustomAnimatorEditorToAppendTheUsefulPropertiesThatArentNormallyExposed : UnityEditor.Editor
    {
        #region Constants
        
        private const string KeepStateProperty = "m_KeepAnimatorStateOnDisable";
        private const string WriteDefaultProperty = "m_WriteDefaultValuesOnDisable";
        private const string KeepStateTooltip = "When enabled, the Animator will preserve its state and animated values when the GameObject is inactive. For example, if animated from x=0 to x=3, the GameObject keeps x=3 when disabled instead of reverting to x=0.";
        private const string WriteDefaultTooltip = "When enabled (true), resets the playable graph to default values when the Animator is disabled. When disabled (false), preserves the current state of the playable graph and animated values when the GameObject is disabled.";
        private const string KeepStateURL = "https://docs.unity3d.com/ScriptReference/Animator-keepAnimatorStateOnDisable.html";
        private const string WriteDefaultURL = "https://docs.unity3d.com/ScriptReference/Animator-writeDefaultValuesOnDisable.html";
        
        #endregion Constants

        #region Private Variables

        private UnityEditor.Editor _defaultEditor;
        private Animator _targetAnimator;
        private bool _showAdvancedOptions;

        #endregion Private Variables
        
        #region Serialized Properties

        private SerializedProperty _mKeepAnimatorStateOnDisable;
        private SerializedProperty _mWriteDefaultValuesOnDisable;

        #endregion Serialized Properties
        
        #region Unity Events
        
        private void OnEnable()
        {
            _targetAnimator = (Animator)target;
            if (!_targetAnimator) return;
            
            _defaultEditor = CreateEditor(target, System.Type.GetType("UnityEditor.AnimatorInspector, UnityEditor"));
            
            _mKeepAnimatorStateOnDisable = serializedObject.FindProperty(KeepStateProperty);
            _mWriteDefaultValuesOnDisable = serializedObject.FindProperty(WriteDefaultProperty);
        }
        
        private void OnDisable() => DestroyImmediate(_defaultEditor);
        
        public override void OnInspectorGUI()
        {
            _defaultEditor.OnInspectorGUI();
            
            // if (targetAnimator.gameObject.name != "meow") return;
            
            EditorGUILayout.Space(10);
            _showAdvancedOptions = EditorGUILayout.Foldout(_showAdvancedOptions, "Advanced Animator Options for ChilloutVR", true);
            if (!_showAdvancedOptions) return;
            
            EditorGUI.indentLevel++;
            serializedObject.Update();
            
            DrawPropertyWithHelpIcon(_mKeepAnimatorStateOnDisable, 
                "Keep Animator State On Disable", KeepStateTooltip, KeepStateURL);
            
            DrawPropertyWithHelpIcon(_mWriteDefaultValuesOnDisable, 
                "Write Default Values On Disable", WriteDefaultTooltip, WriteDefaultURL);
            
            serializedObject.ApplyModifiedProperties();
            EditorGUI.indentLevel--;

            return;
            static void DrawPropertyWithHelpIcon(SerializedProperty property, string label, string tooltip, string helpUrl)
            {
                Rect position = EditorGUILayout.GetControlRect();
                float extendedLabelWidth = EditorGUIUtility.labelWidth + 50;
            
                EditorGUI.LabelField(new Rect(position.x, position.y, extendedLabelWidth, position.height), new GUIContent(label, tooltip));
                EditorGUI.PropertyField(new Rect(position.x + extendedLabelWidth + 5, position.y, 20, position.height), property, GUIContent.none);
            
                if (GUI.Button(new Rect(position.width - 20, position.y, 20, position.height), "?", EditorStyles.miniButton))
                    Application.OpenURL(helpUrl);
            }
        }
        
        #endregion Unity Events
    }
}
#endif