using UnityEngine;
using UnityEditor;
using IndianOceanAssets.Engine2_5D;

/// <summary>
/// 回声描边诊断工具
/// </summary>
public class EchoDebugger : Editor
{
    [MenuItem("Tools/Echo/诊断回声描边状态")]
    public static void DebugEchoOutline()
    {
        // 查找EchoOutlineManager
        EchoOutlineManager manager = Object.FindObjectOfType<EchoOutlineManager>();
        
        if (manager == null)
        {
            EditorUtility.DisplayDialog("错误", "场景中没有找到EchoOutlineManager组件", "确定");
            return;
        }

        // 通过反射获取私有字段
        var parentRenderersField = typeof(EchoOutlineManager).GetField("_parentRenderers",
            System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        var outlineRenderersField = typeof(EchoOutlineManager).GetField("_outlineRenderers",
            System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        var renderersEnabledField = typeof(EchoOutlineManager).GetField("_renderersEnabled",
            System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

        var parentRenderers = parentRenderersField.GetValue(manager) as System.Collections.Generic.List<SpriteRenderer>;
        var outlineRenderers = outlineRenderersField.GetValue(manager) as System.Collections.Generic.List<SpriteRenderer>;
        bool renderersEnabled = (bool)renderersEnabledField.GetValue(manager);

        string info = $"=== 回声描边诊断 ===\n\n";
        info += $"EchoOutlineManager: {manager.name}\n";
        info += $"描边材质: {(manager != null ? "已设置" : "未设置")}\n\n";
        
        info += $"父物体数量: {parentRenderers?.Count ?? 0}\n";
        info += $"描边子物体数量: {outlineRenderers?.Count ?? 0}\n";
        info += $"描边渲染器启用: {renderersEnabled}\n\n";

        if (outlineRenderers != null && outlineRenderers.Count > 0)
        {
            info += $"=== 前5个描边子物体 ===\n\n";
            int showCount = Mathf.Min(5, outlineRenderers.Count);
            for (int i = 0; i < showCount; i++)
            {
                var cr = outlineRenderers[i];
                var pr = parentRenderers[i];
                
                if (cr == null || pr == null) continue;
                
                info += $"父物体: {pr.name}\n";
                info += $"  描边子物体: {cr.name}\n";
                info += $"  启用状态: {cr.enabled}\n";
                info += $"  SortingOrder: {cr.sortingOrder}\n";
                info += $"  Material: {cr.sharedMaterial?.shader?.name ?? "null"}\n\n";
            }
        }
        else
        {
            info += "⚠️ 警告: 没有找到描边子物体！\n";
            info += "可能原因:\n";
            info += "1. EchoOutlineManager.Start()没有执行\n";
            info += "2. 场景中没有符合条件的SpriteRenderer\n";
            info += "3. outlineMaterial未设置\n";
        }

        // 检查全局Shader参数
        float intensity = Shader.GetGlobalFloat(Shader.PropertyToID("_EchoOutlineIntensity"));
        info += $"\n=== 全局Shader参数 ===\n\n";
        info += $"_EchoOutlineIntensity: {intensity}\n";

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

    [MenuItem("Tools/Echo/手动刷新描边")]
    public static void RefreshEchoOutlines()
    {
        EchoOutlineManager manager = Object.FindObjectOfType<EchoOutlineManager>();
        
        if (manager == null)
        {
            EditorUtility.DisplayDialog("错误", "场景中没有找到EchoOutlineManager组件", "确定");
            return;
        }

        manager.RefreshList();
        EditorUtility.DisplayDialog("完成", "已刷新描边子物体", "确定");
        Debug.Log("✓ 已刷新描边子物体");
    }
}
