using System.Windows;

namespace NexusShell.Services.Diagnostics;

public sealed class WindowEventTracker
{
    private readonly DiagnosticsService _diagnostics;

    public WindowEventTracker(DiagnosticsService diagnostics)
    {
        _diagnostics = diagnostics;
    }

    public void Attach(Window window, string appId, string displayName)
    {
        window.Loaded += (_, _) =>
            _diagnostics.LogWindow("window_loaded", displayName,
                new Dictionary<string, string> { ["appId"] = appId });

        window.Activated += (_, _) =>
            _diagnostics.LogWindow("window_activated", displayName,
                new Dictionary<string, string> { ["appId"] = appId });

        window.Deactivated += (_, _) =>
            _diagnostics.LogWindow("window_deactivated", displayName,
                new Dictionary<string, string> { ["appId"] = appId });

        window.Closed += (_, _) =>
            _diagnostics.LogWindow("window_closed", displayName,
                new Dictionary<string, string> { ["appId"] = appId });
    }

    public void TrackExternalWindowDetected(string processName, int processId)
    {
        _diagnostics.LogWindow("external_window_detected",
            $"Eksternt vindue registreret: {processName} (PID {processId})",
            new Dictionary<string, string>
            {
                ["processName"] = processName,
                ["processId"] = processId.ToString(),
                ["warning"] = "POC: Eksternt hosted app-vindue er ikke fuldt indlejret i Nexus.",
            });
    }
}
