using UnityEditor;
using UnityEngine;


namespace ToolsBox.SoundManager.Editor
{
    [CustomPropertyDrawer(typeof(SoundSource))]
    public class SoundSourceDrawer : PropertyDrawer
    {
        private const float HeaderHeight = 20f;
        private const float Padding = 4f;
        float ContentHeightRatio = 5f;

        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) 
        {

            Rect headerRect = new Rect(position.x, position.y, position.width, HeaderHeight);
            Rect backgroundRect = new Rect(position.x, position.y + HeaderHeight, position.width, position.height - HeaderHeight * 1.75f);
            Rect labelRect = new Rect(position.x + 5f, position.y, position.width - 20f, HeaderHeight);
            Rect objectLabelRect = new Rect(position.x + Padding, position.y + HeaderHeight + Padding + 25, position.width - (Padding * 7), EditorGUIUtility.singleLineHeight);
            Rect contentRect = new Rect(position.x + Padding, position.y + HeaderHeight + Padding, position.width - (Padding * 2), EditorGUIUtility.singleLineHeight);
            Rect sourcePosRect = new Rect(position.x + Padding, position.y + HeaderHeight + Padding + 50, position.width - (Padding * 7), EditorGUIUtility.singleLineHeight);
            Rect posRect = new Rect(position.x + Padding, position.y + HeaderHeight + Padding + 75, position.width - (Padding * 7), EditorGUIUtility.singleLineHeight);

            EditorGUI.BeginProperty(position, GUIContent.none, property);

            GUI.Box(headerRect, GUIContent.none, new GUIStyle("RL Header"));
            GUI.Box(backgroundRect, GUIContent.none, new GUIStyle("RL Background"));
            EditorGUI.LabelField(labelRect, label);

            var soundProp = property.FindPropertyRelative("SoundData");
            var posProp = property.FindPropertyRelative("Position");
            var sourcePosProp = property.FindPropertyRelative("PositionSource");
            var parentProp = property.FindPropertyRelative("ParentObject");

            EditorGUI.PropertyField(contentRect, soundProp, GUIContent.none);

            EditorGUI.LabelField(objectLabelRect,"Source Position", EditorStyles.boldLabel);

            EditorGUI.PropertyField(sourcePosRect, sourcePosProp, GUIContent.none);

            switch (sourcePosProp.intValue)
            {
                case 0:
                    ContentHeightRatio = 5f;
                    break;
                case 1: 
                    EditorGUI.PropertyField(posRect, parentProp, GUIContent.none);
                    ContentHeightRatio = 6.5f;
                    break;
                case 2: 
                    EditorGUI.PropertyField(posRect, posProp, GUIContent.none);
                    ContentHeightRatio = 6.5f;
                    break;
            }
            //if (property.objectReferenceValue == null)
            //{
            //    EditorGUI.EndProperty();
            //    return;
            //}

            //EditorGUI.LabelField(objectLabelRect,"Source Position", EditorStyles.boldLabel);
            //SerializedObject serializedObject = new SerializedObject(property.objectReferenceValue);

            //serializedObject.Update();
            //var sourcePos = serializedObject.FindProperty("SourcePosition");
            //EditorGUI.PropertyField(sourcePosRect, sourcePos, GUIContent.none);

            //if(sourcePos.intValue == 2)
            //{
            //    ContentHeightRatio = 6.5f;
            //    var pos = serializedObject.FindProperty("Position");
            //    EditorGUI.PropertyField(posRect, pos, GUIContent.none);
            //}
            //else
            //{
            //    ContentHeightRatio = 5;

            //}

            //serializedObject.ApplyModifiedProperties();
            EditorGUI.EndProperty();
        }

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
            float contentHeight = EditorGUIUtility.singleLineHeight * ContentHeightRatio;
            return HeaderHeight + contentHeight + (Padding * 2);
        }


    }
}
