import Cocoa
import ServiceManagement

class SettingsWindowController: NSWindowController {
    weak var appDelegate: AppDelegate?

    let presets: [(label: String, interval: TimeInterval)] = [
        ("30 minutes", 1800),
        ("1 hour", 3600),
        ("6 hours", 21600),
        ("12 hours", 43200),
        ("24 hours", 86400),
    ]

    var intervalPopup: NSPopUpButton!
    var customMinutesField: NSTextField!
    var launchAtLoginCheckbox: NSButton!
    var greedyCheckbox: NSButton!
    var autoUpgradeFormulaeCheckbox: NSButton!
    var autoUpgradeCasksCheckbox: NSButton!

    convenience init(appDelegate: AppDelegate) {
        let window = NSWindow(
            contentRect: NSRect(x: 0, y: 0, width: 380, height: 200),
            styleMask: [.titled, .closable],
            backing: .buffered,
            defer: false
        )
        window.title = "BrewBar Settings"
        // the controller keeps the window alive; without this, closing the
        // window would deallocate it and reopening would crash
        window.isReleasedWhenClosed = false

        self.init(window: window)
        self.appDelegate = appDelegate

        buildUI()
        syncUI()
        window.center()
    }

    // MARK: - Layout

    func buildUI() {
        guard let contentView = window?.contentView else { return }

        intervalPopup = NSPopUpButton()
        for preset in presets {
            intervalPopup.addItem(withTitle: preset.label)
        }
        intervalPopup.addItem(withTitle: "Custom")
        intervalPopup.target = self
        intervalPopup.action = #selector(presetPicked(_:))

        customMinutesField = NSTextField()
        customMinutesField.placeholderString = "60"
        customMinutesField.alignment = .right
        customMinutesField.target = self
        customMinutesField.action = #selector(customMinutesEntered(_:))
        customMinutesField.widthAnchor.constraint(equalToConstant: 60).isActive = true

        let frequencyRow = NSStackView(views: [
            NSTextField(labelWithString: "Check for updates every:"),
            intervalPopup,
        ])
        frequencyRow.orientation = .horizontal

        let customRow = NSStackView(views: [
            customMinutesField,
            NSTextField(labelWithString: "minutes"),
        ])
        customRow.orientation = .horizontal

        launchAtLoginCheckbox = NSButton(
            checkboxWithTitle: "Launch BrewBar at login",
            target: self,
            action: #selector(toggleLaunchAtLogin(_:))
        )

        greedyCheckbox = NSButton(
            checkboxWithTitle: "Include self-updating casks (--greedy)",
            target: self,
            action: #selector(toggleGreedy(_:))
        )

        autoUpgradeFormulaeCheckbox = NSButton(
            checkboxWithTitle: "Automatically upgrade formulae (command-line tools)",
            target: self,
            action: #selector(toggleAutoUpgradeFormulae(_:))
        )

        autoUpgradeCasksCheckbox = NSButton(
            checkboxWithTitle: "Automatically upgrade casks (apps)",
            target: self,
            action: #selector(toggleAutoUpgradeCasks(_:))
        )

        let caskWarning = NSTextField(
            wrappingLabelWithString: "⚠️ Upgrading a cask can close and replace the app while it is running (e.g. your browser), without warning."
        )
        caskWarning.font = .systemFont(ofSize: NSFont.smallSystemFontSize)
        caskWarning.textColor = .secondaryLabelColor
        caskWarning.preferredMaxLayoutWidth = 320

        // indent the note so it reads as belonging to the checkbox above
        let spacer = NSView()
        spacer.widthAnchor.constraint(equalToConstant: 18).isActive = true
        let caskWarningRow = NSStackView(views: [spacer, caskWarning])
        caskWarningRow.orientation = .horizontal

        let stack = NSStackView(views: [
            frequencyRow,
            customRow,
            greedyCheckbox,
            separator(),
            autoUpgradeFormulaeCheckbox,
            autoUpgradeCasksCheckbox,
            caskWarningRow,
            separator(),
            launchAtLoginCheckbox,
        ])
        stack.orientation = .vertical
        stack.alignment = .leading
        stack.spacing = 12

        stack.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(stack)
        NSLayoutConstraint.activate([
            stack.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20),
            stack.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
            stack.trailingAnchor.constraint(lessThanOrEqualTo: contentView.trailingAnchor, constant: -20),
            stack.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -20),
        ])

        contentView.layoutSubtreeIfNeeded()
        window?.setContentSize(contentView.fittingSize)
    }

    /// Push current values into the controls. Called on every open, since
    /// values may have changed while the window was closed.
    func syncUI() {
        guard let appDelegate else { return }

        let interval = appDelegate.refreshInterval
        if let presetIndex = presets.firstIndex(where: { $0.interval == interval }) {
            intervalPopup.selectItem(at: presetIndex)
        } else {
            intervalPopup.selectItem(at: presets.count) // Custom
        }
        customMinutesField.stringValue = "\(Int(interval / 60))"

        // SMAppService is the source of truth — the user may have changed
        // this in System Settings > Login Items while we weren't looking
        launchAtLoginCheckbox.state = SMAppService.mainApp.status == .enabled ? .on : .off
        greedyCheckbox.state = Settings.includeGreedyCasks ? .on : .off
        autoUpgradeFormulaeCheckbox.state = Settings.autoUpgradeFormulae ? .on : .off
        autoUpgradeCasksCheckbox.state = Settings.autoUpgradeCasks ? .on : .off
    }

    func separator() -> NSBox {
        let box = NSBox()
        box.boxType = .separator
        return box
    }

    // MARK: - Actions

    @objc func presetPicked(_ sender: NSPopUpButton) {
        guard sender.indexOfSelectedItem < presets.count else {
            window?.makeFirstResponder(customMinutesField)
            return
        }
        applyInterval(presets[sender.indexOfSelectedItem].interval)
    }

    @objc func customMinutesEntered(_ sender: NSTextField) {
        guard let minutes = Int(sender.stringValue), minutes > 0 else {
            syncUI() // reject invalid input by restoring the current value
            return
        }
        applyInterval(TimeInterval(minutes * 60))
    }

    func applyInterval(_ interval: TimeInterval) {
        appDelegate?.refreshInterval = interval
        appDelegate?.restartTimer()
        syncUI()
    }

    @objc func toggleGreedy(_ sender: NSButton) {
        Settings.includeGreedyCasks = sender.state == .on
        appDelegate?.fetchOutdated() // the list contents just changed meaning
    }

    @objc func toggleAutoUpgradeFormulae(_ sender: NSButton) {
        Settings.autoUpgradeFormulae = sender.state == .on
    }

    @objc func toggleAutoUpgradeCasks(_ sender: NSButton) {
        Settings.autoUpgradeCasks = sender.state == .on
    }

    @objc func toggleLaunchAtLogin(_ sender: NSButton) {
        do {
            if sender.state == .on {
                try SMAppService.mainApp.register()
            } else {
                try SMAppService.mainApp.unregister()
            }
        } catch {
            sender.state = sender.state == .on ? .off : .on // revert on failure

            let alert = NSAlert()
            alert.alertStyle = .warning
            alert.messageText = "Could not change login item"
            alert.informativeText = error.localizedDescription
            alert.runModal()
        }
    }
}
