using UnityEngine;
using UnityEditor;
using UniHumanoid;

public class HumanPoseClipEditor : EditorWindow
{
    private Animator sourceAnimator;
    private string savePath = "Assets/NewPose.pose";

    [MenuItem("Tools/Human Pose Clip Creator")]
    static void ShowWindow()
    {
        var window = GetWindow<HumanPoseClipEditor>("포즈 생성기");
        window.minSize = new Vector2(300, 100);
    }

    void OnGUI()
    {
        EditorGUILayout.Space(10);
        EditorGUILayout.LabelField("휴머노이드 포즈 생성", EditorStyles.boldLabel);
        EditorGUILayout.Space(10);

        sourceAnimator = EditorGUILayout.ObjectField("소스 애니메이터", 
            sourceAnimator, typeof(Animator), true) as Animator;

        savePath = EditorGUILayout.TextField("저장 경로", savePath);

        EditorGUILayout.Space(10);

        GUI.enabled = sourceAnimator != null;
        if (GUILayout.Button("현재 포즈로 에셋 생성"))
        {
            CreatePoseClip();
        }
        GUI.enabled = true;
    }

    void CreatePoseClip()
    {
        if (!sourceAnimator.isHuman)
        {
            EditorUtility.DisplayDialog("오류", 
                "선택된 애니메이터가 휴머노이드가 아닙니다.", "확인");
            return;
        }

        var clip = ScriptableObject.CreateInstance<HumanPoseClip>();
        var handler = new HumanPoseHandler(sourceAnimator.avatar, sourceAnimator.transform);
        var pose = new HumanPose();
        
        handler.GetHumanPose(ref pose);
        clip.ApplyPose(ref pose);

        // 에셋 저장
        AssetDatabase.CreateAsset(clip, savePath + ".asset");
        AssetDatabase.SaveAssets();
        
        EditorUtility.DisplayDialog("완료", 
            "포즈 클립이 생성되었습니다.", "확인");
        
        // 생성된 에셋 선택
        Selection.activeObject = clip;
    }
}