//
//  AboutPanel.swift
//  SwanDesk — standard About panel carrying the open-source / GPL source notice.
//
//  SwanDesk links strongSwan (GPLv2), so distributing the binary obliges us to
//  offer the corresponding source. The About panel is where that notice
//  accompanies the running app: it states the licenses and points at the page
//  where the complete corresponding source can be obtained.
//

import AppKit

enum AboutPanel {
    /// Page where the complete corresponding source can be obtained.
    private static let sourceURL = "https://www.juvex.fi/swandesk"

    /// Present the standard macOS About panel (app icon + name + version) with the
    /// source notice in its credits. The app is `LSUIElement`, so we activate it
    /// and order the panel front explicitly.
    @MainActor
    static func present() {
        NSApp.activate(ignoringOtherApps: true)
        NSApp.orderFrontStandardAboutPanel(options: [.credits: credits])
    }

    private static var credits: NSAttributedString {
        let out = NSMutableAttributedString()
        let base: [NSAttributedString.Key: Any] = [
            .font: NSFont.systemFont(ofSize: 11),
            .foregroundColor: NSColor.labelColor,
        ]
        func text(_ s: String) {
            out.append(NSAttributedString(string: s, attributes: base))
        }
        func link(_ s: String, _ url: String) {
            var attrs = base
            attrs[.link] = URL(string: url)
            out.append(NSAttributedString(string: s, attributes: attrs))
        }

        text("SwanDesk bundles ")
        link("strongSwan", "https://www.strongswan.org")
        text(", licensed under the GNU General Public License v2, and OpenSSL, "
            + "licensed under the Apache License 2.0.\n\n")
        text("In accordance with the GPL, the complete corresponding source for "
            + "SwanDesk and the exact strongSwan and OpenSSL releases it bundles "
            + "can be obtained at ")
        link("www.juvex.fi/swandesk", sourceURL)
        text(".")

        return out
    }
}
