using UnityEngine;
using UnityEditor;

namespace KindRetargeting
{
    [CustomEditor(typeof(LimbWeightController))]
    public class LimbWeightControllerEditor : BaseRetargetingEditor
    {
        SerializedProperty maxDistance;
        SerializedProperty minDistance;
        SerializedProperty weightSmoothSpeed;
        SerializedProperty middleWeightMultiplier;
        SerializedProperty hipsMinDistance;
        SerializedProperty hipsMaxDistance;
        SerializedProperty props;
        SerializedProperty characterRoot;
        SerializedProperty groundHipsMinHeight;
        SerializedProperty groundHipsMaxHeight;
        SerializedProperty footHeightMinThreshold;
        SerializedProperty footHeightMaxThreshold;

        private bool showDistanceSettings = true;
        private bool showWeightSettings = true;
        private bool showHipsSettings = true;
        private bool showReferences = true;
        private bool showGroundHipsSettings = true;
        private bool showFootHeightSettings = true;

        protected override void OnEnable()
        {
            base.OnEnable();

            // SerializedProperty들 초기화
            if (serializedObject != null)
            {
                maxDistance = serializedObject.FindProperty("maxDistance");
                minDistance = serializedObject.FindProperty("minDistance");
                weightSmoothSpeed = serializedObject.FindProperty("weightSmoothSpeed");
                middleWeightMultiplier = serializedObject.FindProperty("middleWeightMultiplier");
                hipsMinDistance = serializedObject.FindProperty("hipsMinDistance");
                hipsMaxDistance = serializedObject.FindProperty("hipsMaxDistance");
                props = serializedObject.FindProperty("props");
                characterRoot = serializedObject.FindProperty("characterRoot");
                groundHipsMinHeight = serializedObject.FindProperty("groundHipsMinHeight");
                groundHipsMaxHeight = serializedObject.FindProperty("groundHipsMaxHeight");
                footHeightMinThreshold = serializedObject.FindProperty("footHeightMinThreshold");
                footHeightMaxThreshold = serializedObject.FindProperty("footHeightMaxThreshold");
            }
        }

        public override void OnInspectorGUI()
        {
            if (serializedObject == null || target == null)
                return;

            serializedObject.Update();
            EditorGUI.BeginChangeCheck();

            EditorGUILayout.Space(10);
            GUI.backgroundColor = new Color(0.8f, 0.9f, 1f);

            showDistanceSettings = EditorGUILayout.Foldout(showDistanceSettings, "거리 기반 가중치 설정", true);
            if (showDistanceSettings)
            {
                EditorGUI.indentLevel++;
                
                // 일반 거리 범위 슬라이더
                float minVal = minDistance.floatValue;
                float maxVal = maxDistance.floatValue;
                EditorGUILayout.LabelField("거리 범위 (가중치 1 -> 0)");
                EditorGUILayout.BeginHorizontal();
                minVal = EditorGUILayout.FloatField(minVal, GUILayout.Width(50));
                EditorGUILayout.MinMaxSlider(ref minVal, ref maxVal, 0f, 1f);
                maxVal = EditorGUILayout.FloatField(maxVal, GUILayout.Width(50));
                EditorGUILayout.EndHorizontal();
                
                minDistance.floatValue = minVal;
                maxDistance.floatValue = maxVal;
                
                EditorGUI.indentLevel--;
            }

            EditorGUILayout.Space(5);
            showWeightSettings = EditorGUILayout.Foldout(showWeightSettings, "가중치 변화 설정", true);
            if (showWeightSettings)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(weightSmoothSpeed, new GUIContent("가중치 변화 속도"));
                EditorGUI.indentLevel--;
            }

            EditorGUILayout.Space(5);
            showHipsSettings = EditorGUILayout.Foldout(showHipsSettings, "허리 가중치 설정", true);
            if (showHipsSettings)
            {
                EditorGUI.indentLevel++;
                
                // 허리 거리 범위 슬라이더
                float hipsMin = hipsMinDistance.floatValue;
                float hipsMax = hipsMaxDistance.floatValue;
                EditorGUILayout.LabelField("허리 거리 범위 (가중치 1 -> 0)");
                EditorGUILayout.BeginHorizontal();
                hipsMin = EditorGUILayout.FloatField(hipsMin, GUILayout.Width(50));
                EditorGUILayout.MinMaxSlider(ref hipsMin, ref hipsMax, 0f, 1f);
                hipsMax = EditorGUILayout.FloatField(hipsMax, GUILayout.Width(50));
                EditorGUILayout.EndHorizontal();
                
                hipsMinDistance.floatValue = hipsMin;
                hipsMaxDistance.floatValue = hipsMax;
                
                EditorGUI.indentLevel--;
            }

            EditorGUILayout.Space(5);
            showGroundHipsSettings = EditorGUILayout.Foldout(showGroundHipsSettings, "바닥 기준 히프 보정 설정", true);
            if (showGroundHipsSettings)
            {
                EditorGUI.indentLevel++;
                
                // 바닥 기준 히프 거리 범위 슬라이더
                float groundMin = groundHipsMinHeight.floatValue;
                float groundMax = groundHipsMaxHeight.floatValue;
                EditorGUILayout.LabelField("바닥 기준 히프 높이 범위 (가중치 0 -> 1)");
                EditorGUILayout.BeginHorizontal();
                groundMin = EditorGUILayout.FloatField(groundMin, GUILayout.Width(50));
                EditorGUILayout.MinMaxSlider(ref groundMin, ref groundMax, 0f, 2f);
                groundMax = EditorGUILayout.FloatField(groundMax, GUILayout.Width(50));
                EditorGUILayout.EndHorizontal();
                
                groundHipsMinHeight.floatValue = groundMin;
                groundHipsMaxHeight.floatValue = groundMax;
                
                EditorGUI.indentLevel--;
            }

            EditorGUILayout.Space(5);
            showFootHeightSettings = EditorGUILayout.Foldout(showFootHeightSettings, "발 높이 기반 가중치 설정", true);
            if (showFootHeightSettings)
            {
                EditorGUI.indentLevel++;
                
                // 발 높이 범위 슬라이더
                float footMin = footHeightMinThreshold.floatValue;
                float footMax = footHeightMaxThreshold.floatValue;
                EditorGUILayout.LabelField("발 높이 범위 (가중치 1 -> 0)");
                EditorGUILayout.BeginHorizontal();
                footMin = EditorGUILayout.FloatField(footMin, GUILayout.Width(50));
                EditorGUILayout.MinMaxSlider(ref footMin, ref footMax, 0.1f, 1f);
                footMax = EditorGUILayout.FloatField(footMax, GUILayout.Width(50));
                EditorGUILayout.EndHorizontal();
                
                footHeightMinThreshold.floatValue = footMin;
                footHeightMaxThreshold.floatValue = footMax;
                
                EditorGUI.indentLevel--;
            }

            EditorGUILayout.Space(5);
            showReferences = EditorGUILayout.Foldout(showReferences, "참조 설정", true);
            if (showReferences)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(props, new GUIContent("프랍 오브젝트"));
                EditorGUILayout.PropertyField(characterRoot, new GUIContent("캐릭터 루트"));
                EditorGUI.indentLevel--;
            }

            if (EditorGUI.EndChangeCheck())
            {
                MarkDirty();
                
                // 연관된 컴포넌트들도 업데이트
                var script = (LimbWeightController)target;
                var retargeting = script.GetComponent<CustomRetargetingScript>();
                if (retargeting != null) 
                {
                    EditorUtility.SetDirty(retargeting);
                    var window = EditorWindow.GetWindow<RetargetingControlWindow>();
                    if (window != null)
                    {
                        window.Repaint();
                    }
                }
            }
        }
    }
}