import AppKit
import MastermindPOCCore

final class MenuBarController: NSObject {
    struct Actions {
        let showOverlay: () -> Void
        let hideOverlay: () -> Void
        let openSettings: () -> Void
        let toggleClickThrough: () -> Void
        let startScreenAndSystemAudio: () -> Void
        let startMicrophone: () -> Void
        let pauseAllCapture: () -> Void
        let quit: () -> Void
    }

    private let actions: Actions
    private let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
    private let statusMenuItem = NSMenuItem(title: "Mastermind: Idle", action: nil, keyEquivalent: "")
    private let showItem = NSMenuItem(title: "Show Assistant", action: #selector(showOverlay), keyEquivalent: "")
    private let hideItem = NSMenuItem(title: "Hide Assistant", action: #selector(hideOverlay), keyEquivalent: "")
    private let clickThroughItem = NSMenuItem(title: "Enable Click-Through", action: #selector(toggleClickThrough), keyEquivalent: "")

    init(actions: Actions) {
        self.actions = actions
        super.init()
        configureMenu()
        setStatus(.idle)
    }

    func setStatus(_ status: TrustStatus) {
        let title = status.menuBarTitle
        statusMenuItem.title = title
        statusItem.button?.title = title
    }

    func setOverlayVisible(_ visible: Bool) {
        showItem.isEnabled = !visible
        hideItem.isEnabled = visible
    }

    func setClickThrough(_ enabled: Bool) {
        clickThroughItem.title = enabled ? "Disable Click-Through" : "Enable Click-Through"
        clickThroughItem.state = enabled ? .on : .off
    }

    private func configureMenu() {
        statusItem.button?.title = "Mastermind: Idle"
        statusItem.button?.toolTip = "Mastermind AI helper status"

        let menu = NSMenu()
        statusMenuItem.isEnabled = false
        menu.addItem(statusMenuItem)
        menu.addItem(.separator())

        showItem.target = self
        hideItem.target = self
        clickThroughItem.target = self
        menu.addItem(showItem)
        menu.addItem(hideItem)
        menu.addItem(clickThroughItem)
        menu.addItem(.separator())

        let settingsItem = NSMenuItem(title: "Settings...", action: #selector(openSettings), keyEquivalent: ",")
        settingsItem.target = self
        menu.addItem(settingsItem)
        menu.addItem(.separator())

        let screenItem = NSMenuItem(title: "Start Screen + System Audio", action: #selector(startScreenAndSystemAudio), keyEquivalent: "")
        screenItem.target = self
        menu.addItem(screenItem)

        let microphoneItem = NSMenuItem(title: "Start Microphone", action: #selector(startMicrophone), keyEquivalent: "")
        microphoneItem.target = self
        menu.addItem(microphoneItem)

        let pauseItem = NSMenuItem(title: "Pause All Capture", action: #selector(pauseAllCapture), keyEquivalent: "")
        pauseItem.target = self
        menu.addItem(pauseItem)
        menu.addItem(.separator())

        let quitItem = NSMenuItem(title: "Quit Mastermind POC", action: #selector(quit), keyEquivalent: "q")
        quitItem.target = self
        menu.addItem(quitItem)

        statusItem.menu = menu
        setOverlayVisible(true)
        setClickThrough(false)
    }

    @objc private func showOverlay() {
        actions.showOverlay()
    }

    @objc private func hideOverlay() {
        actions.hideOverlay()
    }

    @objc private func openSettings() {
        actions.openSettings()
    }

    @objc private func toggleClickThrough() {
        actions.toggleClickThrough()
    }

    @objc private func startScreenAndSystemAudio() {
        actions.startScreenAndSystemAudio()
    }

    @objc private func startMicrophone() {
        actions.startMicrophone()
    }

    @objc private func pauseAllCapture() {
        actions.pauseAllCapture()
    }

    @objc private func quit() {
        actions.quit()
    }
}
