using SHH.ProcessLaunchers;
using System.Diagnostics;
using System.Windows;

namespace SHH.CameraDashboard
{
    /// <summary>
    /// 启动器日志适配器
    /// <para>将底层 ProcessManager 的日志桥接到 System.Diagnostics.Debug 和 MessageBox</para>
    /// </summary>
    public class ProcessDashboardLogger : ILauncherLogger
    {
        public void LogConsole(string processId, string message, bool isError)
        {
            // 将子进程的控制台输出转发到 VS 的输出窗口，方便调试
            string prefix = isError ? "[STDERR]" : "[STDOUT]";
            Debug.WriteLine($"{prefix} <{processId}>: {message}");
        }

        public void LogLifecycle(string processId, LogAction action, LogTrigger trigger, string reason, object payload = null)
        {
            string msg = $"[ProcessManager] {processId} - {action}: {reason}";
            Debug.WriteLine(msg);

            // 如果是严重错误（如资源超限被杀），弹窗提醒
            if (trigger == LogTrigger.ResourceGuard && action == LogAction.Restart)
            {
                // 注意：确保在 UI 线程弹窗
                Application.Current.Dispatcher.Invoke(() =>
                {
                    MessageBox.Show($"进程 [{processId}] 资源异常！\n原因：{reason}\n系统已执行自动重启。",
                        "资源管控警报", MessageBoxButton.OK, MessageBoxImage.Warning);
                });
            }
        }
    }
}