import Cocoa

@main
class AppDelegate: NSObject, NSApplicationDelegate {
    
    static func main() {
        let app = NSApplication.shared
        let delegate = AppDelegate()
        app.delegate = delegate
        app.setActivationPolicy(.accessory)
        app.run()
    }

    // MARK: - Varaibles
    
    var statusItem: NSStatusItem!
    var statusMenuItem: NSMenuItem!
    var outdatedSubmenu: NSMenu!
    var versionItem: NSMenuItem!

    var logWindow: LogWindowController?
    var logBuffer: String = ""

    var brewPath: String?
    
    var refreshTimer: Timer?
    //var refreshInterval: TimeInterval = 3600
    var refreshInterval: TimeInterval {
        get {
            let saved = UserDefaults.standard.double(forKey: "refreshInterval")
            return saved > 0 ? saved : 3600 // fallback 1h si jamais défini
        }
        set {
            UserDefaults.standard.set(newValue, forKey: "refreshInterval")
            UserDefaults.standard.synchronize()
        }
    }

    // ✅ cache to prevent duplicate outdated calls
    var cachedOutdated: [String] = []
    var lastOutdatedFetch: Date?
    
    // MARK: - Menu

    func applicationDidFinishLaunching(_ notification: Notification) {

        statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
        setMenuBarIcon("brewbar-uptodate")

        let menu = NSMenu()

        statusMenuItem = NSMenuItem(title: "Checking...", action: nil, keyEquivalent: "")
        menu.addItem(statusMenuItem)

        menu.addItem(NSMenuItem.separator())

        menu.addItem(NSMenuItem(title: "🔄 Refresh", action: #selector(refreshAction), keyEquivalent: "r"))
        menu.addItem(NSMenuItem(title: "📦 Upgrade All", action: #selector(upgradeAll), keyEquivalent: "u"))
        let outdatedItem = NSMenuItem(title: "📋 Outdated", action: nil, keyEquivalent: "")
            outdatedSubmenu = NSMenu()
            outdatedSubmenu.addItem(NSMenuItem(title: "Refresh List", action: #selector(forceRefreshOutdated), keyEquivalent: ""))
            outdatedItem.submenu = outdatedSubmenu
            menu.addItem(outdatedItem)
        menu.addItem(NSMenuItem(title: "📜 Logs", action: #selector(showLogs), keyEquivalent: "l"))

        menu.addItem(NSMenuItem.separator())
        
        
        
        let intervalItem = NSMenuItem(title: "⏱ Refresh Interval", action: nil, keyEquivalent: "")
        let intervalSubmenu = NSMenu()

        let options: [(String, TimeInterval)] = [
            ("1 heure", 3600),
            ("6 heures", 21600),
        ]

        for (label, interval) in options {
            let item = NSMenuItem(title: label, action: #selector(setRefreshInterval(_:)), keyEquivalent: "")
            item.representedObject = interval
            intervalSubmenu.addItem(item)
        }
        
                let customMenuItem = NSMenuItem()
                let customView = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 30))

                let textField = NSTextField(frame: NSRect(x: 10, y: 4, width: 120, height: 22))
                textField.placeholderString = "Secondes..."
                textField.stringValue = "\(Int(refreshInterval))"

                let confirmButton = NSButton(frame: NSRect(x: 138, y: 4, width: 52, height: 22))
                confirmButton.title = "✓ Set"
                confirmButton.bezelStyle = .rounded
                confirmButton.target = self
                confirmButton.action = #selector(applyCustomInterval(_:))

                customView.addSubview(textField)
                customView.addSubview(confirmButton)
                customMenuItem.view = customView
                intervalSubmenu.addItem(customMenuItem)

        intervalItem.submenu = intervalSubmenu
        menu.addItem(intervalItem)
        
        
        

        versionItem = NSMenuItem(title: "🏷 Brew: ...", action: nil, keyEquivalent: "")
        menu.addItem(versionItem)

        menu.addItem(NSMenuItem.separator())
        
        menu.addItem(NSMenuItem(title: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))

        statusItem.menu = menu

        refreshAll()
        refreshBrewVersion()
        
        refreshTimer = Timer.scheduledTimer(withTimeInterval: refreshInterval, repeats: true) { _ in
            self.refreshAll()
        } // pour arrêter : refreshTimer?.invalidate()
    }
    
    // MARK: - Env func

    func setMenuBarIcon(_ name: String) {
        DispatchQueue.main.async {
            if let button = self.statusItem.button {
                if let icon = NSImage(named: name) {
                    icon.size = NSSize(width: 22, height: 22)
                    icon.isTemplate = true
                    button.image = icon
                }
            }
        }
    }
    
    func restartTimer() {
        refreshTimer?.invalidate() // stop running timer
        refreshTimer = Timer.scheduledTimer(withTimeInterval: refreshInterval, repeats: true) { _ in
            self.refreshAll()
        }
    }
    
    @objc func setRefreshInterval(_ sender: NSMenuItem) {
        guard let interval = sender.representedObject as? TimeInterval else { return }
        refreshInterval = interval  // ← le set sauvegarde dans UserDefaults
        restartTimer()
    }
    
    @objc func applyCustomInterval(_ sender: NSButton) {
        guard let view = sender.superview,
              let textField = view.subviews.first(where: { $0 is NSTextField }) as? NSTextField,
              let seconds = Int(textField.stringValue), seconds > 0 else { return }
        
        refreshInterval = TimeInterval(seconds)
        restartTimer()
        statusItem.menu?.cancelTracking()
    }
    
    func resolveBrewPath() -> String {
        if let cached = brewPath {
            return cached
        }

        let paths = [
            "/opt/homebrew/bin/brew",
            "/usr/local/bin/brew"
        ]

        for path in paths {
            if FileManager.default.isExecutableFile(atPath: path) {
                brewPath = path
                return path
            }
        }

        //fallback
        brewPath = "brew"
        return "brew"
    }

    func runBrew(_ command: String, completion: @escaping (String) -> Void = { _ in }) {

        DispatchQueue.global().async {

            let brew = self.resolveBrewPath()
            let cleaned = command.replacingOccurrences(of: "brew ", with: "")
            let fullCommand = "\(brew) \(cleaned)"

            self.log("→ \(fullCommand)\n")

            let process = Process()
            process.executableURL = URL(fileURLWithPath: "/bin/zsh")
            process.arguments = ["-c", fullCommand]

            let pipe = Pipe()
            process.standardOutput = pipe
            process.standardError = pipe

            do {
                try process.run()
            } catch {
                self.log("ERROR: \(error)\n")
                completion("")
                return
            }

            let data = pipe.fileHandleForReading.readDataToEndOfFile()
            process.waitUntilExit()

            let output = String(data: data, encoding: .utf8) ?? ""

            self.log(output)
            completion(output)
        }
    }

    // MARK: - Actions

    @objc func refreshAction() {
        refreshAll()
    }

    @objc func upgradeAll() {
        statusMenuItem.title = "Upgrading..."

        runBrew("update") { _ in
            self.runBrew("upgrade") { _ in
                self.fetchOutdated()
            }
        }
    }

    // ✅ SMART refresh (uses cache)
    @objc func refreshOutdatedList() {

        // if fetched in last 5s → uses cache
        if let last = lastOutdatedFetch, Date().timeIntervalSince(last) < 5 {
            updateOutdatedMenu(with: cachedOutdated)
            return
        }

        fetchOutdated()
    }

    // ✅ FORCE refresh (menu button)
    @objc func forceRefreshOutdated() {
        fetchOutdated()
    }

    func fetchOutdated() {
        setMenuBarIcon("brewbar-updating")
        
        runBrew("outdated") { output in

            let lines = output.split(separator: "\n").map { String($0) }

            self.cachedOutdated = lines
            self.lastOutdatedFetch = Date()

            DispatchQueue.main.async {
                self.updateOutdatedMenu(with: lines)
                self.updateStatus(count: lines.count)
                if lines.isEmpty {
                    self.setMenuBarIcon("brewbar-uptodate")
                } else {
                    self.setMenuBarIcon("brewbar-outdated")
                }
            }
        }
    }

    func updateOutdatedMenu(with lines: [String]) {

        outdatedSubmenu.removeAllItems()
        outdatedSubmenu.addItem(NSMenuItem(title: "Refresh List", action: #selector(forceRefreshOutdated), keyEquivalent: ""))

        if lines.isEmpty {
            outdatedSubmenu.addItem(NSMenuItem(title: "✅ All up to date", action: nil, keyEquivalent: ""))
            return
        }

        for formula in lines {
            let item = NSMenuItem(title: formula, action: #selector(self.upgradeSingle(_:)), keyEquivalent: "")
            item.representedObject = formula
            outdatedSubmenu.addItem(item)
        }
    }

    func updateStatus(count: Int) {
        if count == 0 {
            statusMenuItem.title = "✅ All up to date"
        } else {
            statusMenuItem.title = "⚠️ \(count) outdated"
        }
    }

    @objc func upgradeSingle(_ sender: NSMenuItem) {
        guard let formula = sender.representedObject as? String else { return }

        runBrew("upgrade \(formula)") { _ in
            self.refreshAll()
        }
    }

    func refreshAll() {
        statusMenuItem.title = "Updating..."

        runBrew("update") { _ in
            self.fetchOutdated() // ✅ ONLY place calling outdated
        }
    }

    // ✅ Brew version (correct)
    func refreshBrewVersion() {
        runBrew("--version") { output in
            let firstLine = output.split(separator: "\n").first.map(String.init) ?? "Unknown"

            DispatchQueue.main.async {
                self.versionItem.title = "🏷 \(firstLine)"
            }
        }
    }

    // MARK: - Logs

    func log(_ text: String) {
        DispatchQueue.main.async {
            self.logBuffer += text
            self.logWindow?.update(text: self.logBuffer)
        }
    }

    @objc func showLogs() {
        if logWindow == nil {
            logWindow = LogWindowController()
        }
        logWindow?.showWindow(nil)
        logWindow?.update(text: logBuffer)
    }
}
