-- ~/.hammerspoon/init.lua
require('SearchWindows')
require('Caffeine')
require('AppBorders')
-- require('System_Tweaks')

-- Load the window management module
windowMgr = require("WindowManager")


-- Load Spoon Files
hs.loadSpoon('SpoonInstall')
hs.loadSpoon('SpeedMenu')
hs.loadSpoon('BrewInfo')
-- hs.loadSpoon('Seal')

--- 3. Run/Configure Spoons

---- SpeedMenu Config
if spoon.SpeedMenu then
    -- 1. Define the Fix Function (includes MAC and IPv6)
    local function applyFullMenuFix()
        local interface = spoon.SpeedMenu.interface or "en0"
        local ssid = hs.wifi.currentNetwork() or "Disconnected"
        local details = hs.network.interfaceDetails(interface)
        
        local ipv4 = (details and details.IPv4) and details.IPv4.Addresses[1] or "N/A"
        local ipv6 = (details and details.IPv6) and details.IPv6.Addresses[1] or "N/A"
        
        -- Get MAC Address via shell
        local macaddr = hs.execute('ifconfig ' .. interface .. ' | grep ether | awk \'{print $2}\''):gsub("%s+", "")

        local menuitems = {
            { title = "SSID: " .. ssid, fn = function() hs.pasteboard.setContents(ssid) end },
            { title = "IPv4: " .. ipv4, fn = function() hs.pasteboard.setContents(ipv4) end },
            { title = "IPv6: " .. ipv6, fn = function() hs.pasteboard.setContents(ipv6) end },
            { title = "MAC: " .. macaddr, fn = function() hs.pasteboard.setContents(macaddr) end },
            { title = "-" },
            { title = "Rescan Network Interfaces", fn = function() spoon.SpeedMenu:rescan() end }
        }
        spoon.SpeedMenu.menubar:setMenu(menuitems)
    end

    -- 2. Hook the rescan method
    local oldRescan = spoon.SpeedMenu.rescan
    spoon.SpeedMenu.rescan = function(self)
        oldRescan(self)
        applyFullMenuFix()
    end

    -- 3. Toggle Logic (Starts as OFF)
    local speedMenuRunning = false
    hs.hotkey.bind({"cmd", "alt", "ctrl"}, "W", function()
        if speedMenuRunning then
            spoon.SpeedMenu:stop()
            hs.alert.show("SpeedMenu Stopped")
        else
            spoon.SpeedMenu:start() -- This puts it in the menu bar
            applyFullMenuFix()      -- This populates the data
            hs.alert.show("SpeedMenu Started")
        end
        speedMenuRunning = not speedMenuRunning
    end)
end
---- SpeedMenu Config END

---- BrewInfo
if spoon.BrewInfo then
    hs.hotkey.bind({"cmd", "alt", "ctrl"}, "I", function()
        spoon.BrewInfo:showBrewInfo()
    end)
    hs.hotkey.bind({"cmd", "alt", "ctrl"}, "J", function()
        spoon.BrewInfo:showBrewInfoCurSel()
    end)
end
---- BrewInfo END

hs.alert.show("Hammerspoon Config Reloaded")