//
// AboutWindow.swift
// IceGlass
//
// Copyright 2026 Rouslan Zenetl. All Rights Reserved.
//

import SwiftUI
import IndieAbout

@MainActor private var aboutWindowController: NSWindowController?

@MainActor func showAboutWindow() {
    if let controller = aboutWindowController {
        controller.showWindow(nil)
        NSApp.activate(ignoringOtherApps: true)
        return
    }

    let configuration = AppInfoConfiguration(
        showDeviceInfo: false,
        documents: [
            .license(),
            .releaseNotes()
        ]
    )

    let aboutView = IndieAbout(configuration: configuration)
        .frame(width: 300)

    let window = NSWindow(
        contentRect: .zero,
        styleMask: [.titled, .closable],
        backing: .buffered,
        defer: false
    )

    window.title = "About IceGlass"
    window.contentView = NSHostingView(rootView: aboutView)
    window.center()

    let controller = NSWindowController(window: window)
    aboutWindowController = controller

    NotificationCenter.default.addObserver(
        forName: NSWindow.willCloseNotification,
        object: window,
        queue: .main
    ) { _ in
        Task { @MainActor in
            aboutWindowController = nil
        }
    }

    controller.showWindow(nil)
    NSApp.activate(ignoringOtherApps: true)
}
