using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

/// <summary>
/// 场景Elements Y-Sort添加工具
/// </summary>
public class SceneElementsYSorter : Editor
{
    [MenuItem("Tools/Y-Sort/给Test场景Elements添加YSorter")]
    public static void AddYSorterToTestSceneElements()
    {
        // 查找场景中的Elements物体
        GameObject elements = GameObject.Find("Elements");
        
        if (elements == null)
        {
            EditorUtility.DisplayDialog("错误", "找不到场景中的 'Elements' 物体", "确定");
            return;
        }

        Transform elementsTransform = elements.transform;
        int count = 0;
        List<string> processedObjects = new List<string>();

        // 遍历所有直接子物体
        for (int i = 0; i < elementsTransform.childCount; i++)
        {
            Transform child = elementsTransform.GetChild(i);
            
            // 检查是否已有YSorter
            if (child.GetComponent<YSorter>() != null)
            {
                Debug.Log($"跳过 {child.name} - 已有YSorter");
                continue;
            }

            // 添加YSorter组件
            YSorter ySorter = child.gameObject.AddComponent<YSorter>();
            
            // 根据物体名称设置baseSortingOrder
            int baseOrder = DetermineBaseOrder(child.name);
            
            // 通过SerializedObject设置
            SerializedObject so = new SerializedObject(ySorter);
            SerializedProperty prop = so.FindProperty("baseSortingOrder");
            if (prop != null)
            {
                prop.intValue = baseOrder;
                so.ApplyModifiedProperties();
            }

            // 标记场景已修改
            EditorUtility.SetDirty(child.gameObject);
            
            count++;
            processedObjects.Add($"{child.name} (baseSortingOrder: {baseOrder})");
            Debug.Log($"✓ 已添加 YSorter 到 {child.name} (baseSortingOrder: {baseOrder})");
        }

        // 保存场景
        if (count > 0)
        {
            UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(
                UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene()
            );
            
            EditorUtility.DisplayDialog(
                "完成", 
                $"已给 {count} 个物体添加YSorter\n\n" + 
                string.Join("\n", processedObjects),
                "确定"
            );
        }
        else
        {
            EditorUtility.DisplayDialog("提示", "所有物体都已有YSorter", "确定");
        }
    }

    [MenuItem("Tools/Y-Sort/给选中物体添加YSorter")]
    public static void AddYSorterToSelected()
    {
        if (Selection.gameObjects.Length == 0)
        {
            EditorUtility.DisplayDialog("提示", "请先选择物体", "确定");
            return;
        }

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

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

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

        if (count > 0)
        {
            UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(
                UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene()
            );
            Debug.Log($"完成！给 {count} 个物体添加了YSorter");
        }
    }

    private static int DetermineBaseOrder(string objectName)
    {
        string nameLower = objectName.ToLower();

        if (nameLower.Contains("ground") || nameLower.Contains("floor"))
            return 0;
        if (nameLower.Contains("shadow"))
            return 500;
        if (nameLower.Contains("grass") || nameLower.Contains("flower") || 
            nameLower.Contains("leaf") || nameLower.Contains("leaves"))
            return 800;
        if (nameLower.Contains("rock") || nameLower.Contains("bamboo") || 
            nameLower.Contains("barrel") || nameLower.Contains("box") ||
            nameLower.Contains("log") || nameLower.Contains("board") ||
            nameLower.Contains("bush") || nameLower.Contains("bonfire") ||
            nameLower.Contains("candle") || nameLower.Contains("carcasses") ||
            nameLower.Contains("lotus") || nameLower.Contains("pedestal") ||
            nameLower.Contains("scarecrow") || nameLower.Contains("skeleton") ||
            nameLower.Contains("skull") || nameLower.Contains("spear") ||
            nameLower.Contains("statue") || nameLower.Contains("tent") ||
            nameLower.Contains("tenticles") || nameLower.Contains("wooden") ||
            nameLower.Contains("bag") || nameLower.Contains("house"))
            return 1000;

        return 1000;
    }
}
