import AppKit
import SwiftUI

/// 独立的设置窗口控制器（参考语音智能体项目 SettingsWindow 模式）
final class SettingsWindowController: NSWindowController {

    private let monitor: SystemMonitor

    init(monitor: SystemMonitor) {
        self.monitor = monitor
        let window = NSWindow(
            contentRect: NSRect(x: 0, y: 0, width: 340, height: 640),
            styleMask: [.titled, .closable, .resizable, .fullSizeContentView],
            backing: .buffered,
            defer: false
        )
        window.title = "Monitor 设置"
        window.titlebarAppearsTransparent = true
        window.isReleasedWhenClosed = false
        window.minSize = NSSize(width: 300, height: 480)
        super.init(window: window)

        // 嵌入 SwiftUI 视图
        let settingsView = SettingsView(monitor: monitor)
        let hostingView = NSHostingView(rootView: settingsView)
        hostingView.translatesAutoresizingMaskIntoConstraints = false
        window.contentView = hostingView

        window.center()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func show() {
        window?.center()
        window?.makeKeyAndOrderFront(nil)
        NSApp.activate(ignoringOtherApps: false)
    }
}
