using UnityEngine;
using UnityEditor;

/// <summary>
/// Y-Sort诊断工具
/// </summary>
public class YSortDebugger : Editor
{
    [MenuItem("Tools/Y-Sort/诊断YSorter状态")]
    public static void DebugYSorter()
    {
        // 查找场景中所有YSorter
        YSorter[] allSorters = FindObjectsOfType<YSorter>();
        
        if (allSorters.Length == 0)
        {
            EditorUtility.DisplayDialog("诊断结果", "场景中没有找到任何YSorter组件！\n\n请先使用菜单:\nTools → Y-Sort → 给Test场景Elements添加YSorter", "确定");
            return;
        }

        string report = $"找到 {allSorters.Length} 个YSorter组件\n\n";
        report += "=== 详细信息 ===\n\n";

        foreach (YSorter sorter in allSorters)
        {
            report += $"物体: {sorter.gameObject.name}\n";
            report += $"  位置: {sorter.transform.position}\n";
            
            // 获取SpriteRenderer
            SpriteRenderer sr = sorter.GetComponentInChildren<SpriteRenderer>();
            if (sr != null)
            {
                report += $"  SpriteRenderer: {sr.name}\n";
                report += $"  SortingLayer: {sr.sortingLayerName}\n";
                report += $"  SortingOrder: {sr.sortingOrder}\n";
            }
            else
            {
                report += $"  SpriteRenderer: 未找到!\n";
            }
            
            report += "\n";
        }

        // 检查是否都在同一个SortingLayer
        string[] layers = new string[allSorters.Length];
        for (int i = 0; i < allSorters.Length; i++)
        {
            SpriteRenderer sr = allSorters[i].GetComponentInChildren<SpriteRenderer>();
            if (sr != null)
                layers[i] = sr.sortingLayerName;
        }

        bool allSameLayer = true;
        if (layers.Length > 0)
        {
            string firstLayer = layers[0];
            foreach (string layer in layers)
            {
                if (layer != firstLayer)
                {
                    allSameLayer = false;
                    break;
                }
            }
        }

        if (!allSameLayer)
        {
            report += "⚠️ 警告: 物体不在同一个SortingLayer!\n";
            report += "这可能导致排序不正确。\n";
        }

        Debug.Log(report);
        EditorUtility.DisplayDialog("诊断完成", $"诊断信息已输出到控制台\n\n请查看Unity Console窗口", "确定");
    }

    [MenuItem("Tools/Y-Sort/测试YSorter计算")]
    public static void TestYSorterCalculation()
    {
        GameObject selected = Selection.activeGameObject;
        
        if (selected == null)
        {
            EditorUtility.DisplayDialog("提示", "请先选择一个物体", "确定");
            return;
        }

        YSorter sorter = selected.GetComponent<YSorter>();
        if (sorter == null)
        {
            EditorUtility.DisplayDialog("提示", $"选中的物体 {selected.name} 没有YSorter组件", "确定");
            return;
        }

        // 通过反射获取私有字段
        var baseOrderField = typeof(YSorter).GetField("baseSortingOrder", 
            System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        var scaleFactorField = typeof(YSorter).GetField("scaleFactor",
            System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

        int baseOrder = (int)baseOrderField.GetValue(sorter);
        float scaleFactor = (float)scaleFactorField.GetValue(sorter);

        Vector3 pos = selected.transform.position;
        int expectedOrder = baseOrder + Mathf.RoundToInt(pos.z * scaleFactor);

        SpriteRenderer sr = sorter.GetComponentInChildren<SpriteRenderer>();
        int actualOrder = sr != null ? sr.sortingOrder : -1;

        string msg = $"物体: {selected.name}\n";
        msg += $"位置: {pos}\n";
        msg += $"Z坐标: {pos.z:F3}\n";
        msg += $"baseSortingOrder: {baseOrder}\n";
        msg += $"scaleFactor: {scaleFactor}\n";
        msg += $"期望SortingOrder: {expectedOrder}\n";
        msg += $"实际SortingOrder: {actualOrder}\n\n";

        if (expectedOrder == actualOrder)
        {
            msg += "✓ 排序正确";
        }
        else
        {
            msg += "✗ 排序不匹配!";
        }

        EditorUtility.DisplayDialog("YSorter测试", msg, "确定");
        Debug.Log(msg);
    }
}
