using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;

public class AnimatorParametersGenerator : Editor
{
    [MenuItem("Tools/Generate Animator Parameters with Properties")]
    public static void GenerateAnimatorParametersWithProperties()
    {
        // 获取选中的Animator Controller
        var controller = Selection.activeObject as AnimatorController;
        if (controller == null)
        {
            Debug.LogError("请选择一个Animator Controller文件！");
            return;
        }

        // 用于存储生成的代码
        string className = controller.name + "Parameters";
        string code = $"// Auto-generated class for {controller.name} parameters\n";
        code += "using UnityEngine;\n\n";
        code += "public class " + className + "\n{\n";
        code += "    private Animator _animator;\n\n";
        code += $"    public {className}(Animator animator)\n";
        code += "    {\n";
        code += "        _animator = animator;\n";
        code += "    }\n\n";

        foreach (var parameter in controller.parameters)
        {
            string paramName = parameter.name;
            string sanitizedName = SanitizeName(paramName); // 变量名合法化
            string typeName = GetTypeName(parameter.type); // 获取类型名称

            switch (parameter.type)
            {
                case AnimatorControllerParameterType.Bool:
                    code += $"    public bool {sanitizedName}\n";
                    code += "    {\n";
                    code += $"        get => _animator.GetBool(\"{paramName}\");\n";
                    code += $"        set => _animator.SetBool(\"{paramName}\", value);\n";
                    code += "    }\n\n";
                    break;

                case AnimatorControllerParameterType.Int:
                    code += $"    public int {sanitizedName}\n";
                    code += "    {\n";
                    code += $"        get => _animator.GetInteger(\"{paramName}\");\n";
                    code += $"        set => _animator.SetInteger(\"{paramName}\", value);\n";
                    code += "    }\n\n";
                    break;

                case AnimatorControllerParameterType.Float:
                    code += $"    public float {sanitizedName}\n";
                    code += "    {\n";
                    code += $"        get => _animator.GetFloat(\"{paramName}\");\n";
                    code += $"        set => _animator.SetFloat(\"{paramName}\", value);\n";
                    code += "    }\n\n";
                    break;

                case AnimatorControllerParameterType.Trigger:
                    code += $"    public void Set{sanitizedName}Trigger()\n";
                    code += "    {\n";
                    code += $"        _animator.SetTrigger(\"{paramName}\");\n";
                    code += "    }\n\n";
                    code += $"    public void Reset{sanitizedName}Trigger()\n";
                    code += "    {\n";
                    code += $"        _animator.ResetTrigger(\"{paramName}\");\n";
                    code += "    }\n\n";
                    break;
            }
        }

        code += "}\n";

        // 输出到控制台
        Debug.Log(code);

        // 可选：将代码写入到指定文件
        string path = EditorUtility.SaveFilePanel("保存参数类", "Assets", className, "cs");
        if (!string.IsNullOrEmpty(path))
        {
            System.IO.File.WriteAllText(path, code);
            AssetDatabase.Refresh();
            Debug.Log("生成的参数类已保存到：" + path);
        }
    }

    // 对参数名进行安全处理，避免非法字符
    private static string SanitizeName(string name)
    {
        return name.Replace(" ", "_").Replace("-", "_");
    }

    // 根据Animator参数类型返回对应的C#类型名
    private static string GetTypeName(AnimatorControllerParameterType type)
    {
        return type switch
        {
            AnimatorControllerParameterType.Bool => "bool",
            AnimatorControllerParameterType.Int => "int",
            AnimatorControllerParameterType.Float => "float",
            AnimatorControllerParameterType.Trigger => "void",
            _ => "unknown"
        };
    }
}