using UnityEngine;
using UnityEditor;
using UnityEngine.Tilemaps;

/// <summary>
/// Tilemap Y-Sort工具 - 快速给Tilemap添加YSorter
/// </summary>
public class TilemapYSortTool : Editor
{
    [MenuItem("Tools/Tilemap/给选中Tilemap添加YSorter")]
    public static void AddYSorterToTilemap()
    {
        if (Selection.gameObjects.Length == 0)
        {
            EditorUtility.DisplayDialog("提示", "请先选择Tilemap物体", "确定");
            return;
        }

        int count = 0;
        foreach (GameObject go in Selection.gameObjects)
        {
            // 检查是否有TilemapRenderer
            TilemapRenderer renderer = go.GetComponentInChildren<TilemapRenderer>();
            if (renderer == null)
            {
                Debug.LogWarning($"跳过 {go.name} - 没有TilemapRenderer组件");
                continue;
            }

            // 检查是否已有YSorter
            if (go.GetComponent<YSorter>() != null)
            {
                Debug.Log($"跳过 {go.name} - 已有YSorter");
                continue;
            }

            // 添加YSorter
            YSorter ySorter = go.AddComponent<YSorter>();
            
            // 设置默认的baseSortingOrder
            SerializedObject so = new SerializedObject(ySorter);
            SerializedProperty prop = so.FindProperty("baseSortingOrder");
            if (prop != null)
            {
                prop.intValue = renderer.sortingOrder;
                so.ApplyModifiedProperties();
            }

            EditorUtility.SetDirty(go);
            count++;
            Debug.Log($"✓ 已添加YSorter到 {go.name} (baseSortingOrder: {renderer.sortingOrder})");
        }

        if (count > 0)
        {
            EditorUtility.DisplayDialog("完成", $"已给 {count} 个Tilemap添加YSorter", "确定");
        }
    }

    [MenuItem("Tools/Tilemap/批量添加YSorter到所有Tilemap")]
    public static void AddYSorterToAllTilemaps()
    {
        TilemapRenderer[] allRenderers = FindObjectsByType<TilemapRenderer>(FindObjectsSortMode.None);
        
        if (allRenderers.Length == 0)
        {
            EditorUtility.DisplayDialog("提示", "场景中没有找到TilemapRenderer", "确定");
            return;
        }

        int count = 0;
        foreach (TilemapRenderer renderer in allRenderers)
        {
            GameObject go = renderer.gameObject;
            
            if (go.GetComponent<YSorter>() != null)
            {
                Debug.Log($"跳过 {go.name} - 已有YSorter");
                continue;
            }

            YSorter ySorter = go.AddComponent<YSorter>();
            
            SerializedObject so = new SerializedObject(ySorter);
            SerializedProperty prop = so.FindProperty("baseSortingOrder");
            if (prop != null)
            {
                prop.intValue = renderer.sortingOrder;
                so.ApplyModifiedProperties();
            }

            EditorUtility.SetDirty(go);
            count++;
            Debug.Log($"✓ 已添加YSorter到 {go.name} (baseSortingOrder: {renderer.sortingOrder})");
        }

        if (count > 0)
        {
            EditorUtility.DisplayDialog("完成", $"已给 {count} 个Tilemap添加YSorter", "确定");
        }
    }
}
