using UnityEngine;
using UnityEditor;

/// <summary>
/// 地面裁剪工具 - 给Sprite应用带地面裁剪的Shader
/// </summary>
public class GroundClipTool : Editor
{
    [MenuItem("Tools/Shader/应用地面裁剪Shader")]
    public static void ApplyGroundClipShader()
    {
        if (Selection.gameObjects.Length == 0)
        {
            EditorUtility.DisplayDialog("提示", "请先选择物体", "确定");
            return;
        }

        // 加载Shader
        Shader shader = Shader.Find("Custom/SpriteWithGroundClip");
        if (shader == null)
        {
            EditorUtility.DisplayDialog("错误", "找不到Shader: Custom/SpriteWithGroundClip\n请检查Shader文件是否存在", "确定");
            return;
        }

        int count = 0;
        foreach (GameObject go in Selection.gameObjects)
        {
            SpriteRenderer sr = go.GetComponentInChildren<SpriteRenderer>();
            if (sr == null)
            {
                Debug.LogWarning($"跳过 {go.name} - 没有SpriteRenderer");
                continue;
            }

            // 创建Material
            Material mat = new Material(shader);
            mat.name = $"{go.name}_GroundClipMat";
            
            // 设置默认值
            mat.SetFloat("_GroundHeight", 0f);
            mat.SetFloat("_EnableClip", 1f);
            mat.SetFloat("_ClipSoftness", 0.05f);
            mat.SetColor("_Color", Color.white);

            // 应用Material
            sr.material = mat;

            // 保存Material为Asset
            string path = $"Assets/Materials/{mat.name}.mat";
            if (!System.IO.Directory.Exists("Assets/Materials"))
            {
                System.IO.Directory.CreateDirectory("Assets/Materials");
            }
            
            AssetDatabase.CreateAsset(mat, path);
            
            EditorUtility.SetDirty(go);
            count++;
            Debug.Log($"✓ 已应用地面裁剪Shader到 {go.name}");
        }

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();

        if (count > 0)
        {
            EditorUtility.DisplayDialog("完成", $"已给 {count} 个物体应用地面裁剪Shader\n\n请在Inspector中调整Ground Height参数", "确定");
        }
    }

    [MenuItem("Tools/Shader/设置地面高度（批量）")]
    public static void SetGroundHeightBatch()
    {
        string input = EditorInputDialog.Show("设置地面高度", "请输入地面Y坐标:", "0");
        
        if (input == null) return;
        if (!float.TryParse(input, out float groundHeight))
        {
            EditorUtility.DisplayDialog("错误", "请输入有效的数字", "确定");
            return;
        }

        int count = 0;
        foreach (GameObject go in Selection.gameObjects)
        {
            SpriteRenderer sr = go.GetComponentInChildren<SpriteRenderer>();
            if (sr == null || sr.material == null) continue;

            if (sr.material.shader.name == "Custom/SpriteWithGroundClip")
            {
                sr.material.SetFloat("_GroundHeight", groundHeight);
                EditorUtility.SetDirty(go);
                count++;
            }
        }

        if (count > 0)
        {
            EditorUtility.DisplayDialog("完成", $"已设置 {count} 个物体的地面高度为 {groundHeight}", "确定");
        }
    }
}
