import AppKit

/// The branded header at the top of a menu-bar dropdown.
///
/// Built as a custom view rather than a disabled menu item on purpose: a disabled item is
/// drawn grey by macOS, which made the status line hard to read. A view renders at full
/// contrast and doesn't highlight on hover.
enum MenuHeader {
    static func item(appName: String,
                     brand: String = "by Incuv8or",
                     status: String) -> NSMenuItem {
        let width: CGFloat = 250
        let container = NSView(frame: NSRect(x: 0, y: 0, width: width, height: 44))

        let title = NSTextField(labelWithAttributedString: titleString(appName, brand))
        title.frame = NSRect(x: 14, y: 22, width: width - 28, height: 18)
        container.addSubview(title)

        let subtitle = NSTextField(labelWithString: status)
        subtitle.font = .systemFont(ofSize: 11.5)
        // labelColor rather than secondary: this line is information, not decoration.
        subtitle.textColor = .labelColor
        subtitle.frame = NSRect(x: 14, y: 5, width: width - 28, height: 15)
        container.addSubview(subtitle)

        let item = NSMenuItem()
        item.view = container
        return item
    }

    private static func titleString(_ appName: String, _ brand: String) -> NSAttributedString {
        let result = NSMutableAttributedString(
            string: appName,
            attributes: [
                .font: NSFont.systemFont(ofSize: 13.5, weight: .bold),
                .foregroundColor: NSColor.labelColor,
            ])
        result.append(NSAttributedString(
            string: "  \(brand)",
            attributes: [
                .font: NSFont.systemFont(ofSize: 11, weight: .medium),
                .foregroundColor: NSColor.secondaryLabelColor,
            ]))
        return result
    }
}
