using UnityEngine;
using UnityEditor;
using System.Linq;

namespace KindRetargeting
{
    [CustomEditor(typeof(PropLocationController))]
    public class PropLocationControllerEditor : BaseRetargetingEditor
    {
        private bool showLeftHand = true;
        private bool showRightHand = true;
        private bool showHead = true;

        public override void OnInspectorGUI()
        {
            // 기본 인스펙터 그리기
            DrawDefaultInspector();

            // PropLocationController 컴포넌트 가져오기
            PropLocationController controller = (PropLocationController)target;

            EditorGUILayout.Space(10);
            EditorGUILayout.LabelField("오프셋 조정", EditorStyles.boldLabel);

            // 왼손 오프셋 컨트롤
            Transform leftOffset = controller.GetLeftHandOffset();
            if (leftOffset != null)
            {
                showLeftHand = EditorGUILayout.Foldout(showLeftHand, "왼손 오프셋");
                if (showLeftHand)
                {
                    EditorGUI.indentLevel++;
                    EditorGUI.BeginChangeCheck();
                    Vector3 leftPos = EditorGUILayout.Vector3Field("위치", leftOffset.localPosition);
                    Vector3 leftRot = EditorGUILayout.Vector3Field("회전", leftOffset.localRotation.eulerAngles);
                    
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(leftOffset, "Update Left Hand Offset");
                        leftOffset.localPosition = leftPos;
                        leftOffset.localRotation = Quaternion.Euler(leftRot);
                        EditorUtility.SetDirty(leftOffset);
                    }
                    EditorGUI.indentLevel--;
                }
            }

            // 오른손 오프셋 컨트롤
            Transform rightOffset = controller.GetRightHandOffset();
            if (rightOffset != null)
            {
                showRightHand = EditorGUILayout.Foldout(showRightHand, "오른손 오프셋");
                if (showRightHand)
                {
                    EditorGUI.indentLevel++;
                    EditorGUI.BeginChangeCheck();
                    Vector3 rightPos = EditorGUILayout.Vector3Field("위치", rightOffset.localPosition);
                    Vector3 rightRot = EditorGUILayout.Vector3Field("회전", rightOffset.localRotation.eulerAngles);
                    
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(rightOffset, "Update Right Hand Offset");
                        rightOffset.localPosition = rightPos;
                        rightOffset.localRotation = Quaternion.Euler(rightRot);
                        EditorUtility.SetDirty(rightOffset);
                    }
                    EditorGUI.indentLevel--;
                }
            }

            // 머리 오프셋 컨트롤
            Transform headOffset = controller.GetHeadOffset();
            if (headOffset != null)
            {
                showHead = EditorGUILayout.Foldout(showHead, "머리 오프셋");
                if (showHead)
                {
                    EditorGUI.indentLevel++;
                    EditorGUI.BeginChangeCheck();
                    Vector3 headPos = EditorGUILayout.Vector3Field("위치", headOffset.localPosition);
                    Vector3 headRot = EditorGUILayout.Vector3Field("회전", headOffset.localRotation.eulerAngles);
                    
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(headOffset, "Update Head Offset");
                        headOffset.localPosition = headPos;
                        headOffset.localRotation = Quaternion.Euler(headRot);
                        EditorUtility.SetDirty(headOffset);
                    }
                    EditorGUI.indentLevel--;
                }
            }

            EditorGUILayout.Space(10);
            EditorGUILayout.LabelField("부착된 프랍 목록", EditorStyles.boldLabel);

            // 머리 프랍 목록
            EditorGUILayout.BeginVertical("box");
            EditorGUILayout.LabelField("머리 프랍", EditorStyles.boldLabel);
            GameObject[] headProps = controller.GetHeadProps();
            if (headProps.Length > 0)
            {
                foreach (GameObject prop in headProps)
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField(prop.name);
                    if (GUILayout.Button("선택", GUILayout.Width(60)))
                    {
                        Selection.activeGameObject = prop;
                    }
                    EditorGUILayout.EndHorizontal();
                }
            }
            else
            {
                EditorGUILayout.LabelField("부착된 프랍 없음");
            }
            EditorGUILayout.EndVertical();

            // 왼손 프랍 목록
            EditorGUILayout.BeginVertical("box");
            EditorGUILayout.LabelField("왼손 프랍", EditorStyles.boldLabel);
            GameObject[] leftHandProps = controller.GetLeftHandProps();
            if (leftHandProps.Length > 0)
            {
                foreach (GameObject prop in leftHandProps)
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField(prop.name);
                    if (GUILayout.Button("선택", GUILayout.Width(60)))
                    {
                        Selection.activeGameObject = prop;
                    }
                    EditorGUILayout.EndHorizontal();
                }
            }
            else
            {
                EditorGUILayout.LabelField("부착된 프랍 없음");
            }
            EditorGUILayout.EndVertical();

            // 오른손 프랍 목록
            EditorGUILayout.BeginVertical("box");
            EditorGUILayout.LabelField("오른손 프랍", EditorStyles.boldLabel);
            GameObject[] rightHandProps = controller.GetRightHandProps();
            if (rightHandProps.Length > 0)
            {
                foreach (GameObject prop in rightHandProps)
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField(prop.name);
                    if (GUILayout.Button("선택", GUILayout.Width(60)))
                    {
                        Selection.activeGameObject = prop;
                    }
                    EditorGUILayout.EndHorizontal();
                }
            }
            else
            {
                EditorGUILayout.LabelField("부착된 프랍 없음");
            }
            EditorGUILayout.EndVertical();

            // 구분선 추가
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("프랍 위치 이동", EditorStyles.boldLabel);

            // 버튼들을 가로로 배치
            EditorGUILayout.BeginHorizontal();

            if (GUILayout.Button("머리로 이동", GUILayout.Height(30)))
            {
                controller.MoveToHead();
            }

            if (GUILayout.Button("왼손으로 이동", GUILayout.Height(30)))
            {
                controller.MoveToLeftHand();
            }

            if (GUILayout.Button("오른손으로 이동", GUILayout.Height(30)))
            {
                controller.MoveToRightHand();
            }

            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Space();
            if (GUILayout.Button("프랍 해제", GUILayout.Height(30)))
            {
                Undo.RecordObject(Selection.activeGameObject?.transform, "Detach Prop");
                controller.DetachProp();
            }
        }
    }
}