local selector = {}
local json = require("hs.json")
local styledtext = require("hs.styledtext")

-- ==========================================
-- CONFIGURATION
-- ==========================================
selector.storageDir = os.getenv("HOME") .. "/.hammerspoon/layouts/"
selector.hotkeys = {} 

if not hs.fs.attributes(selector.storageDir) then hs.fs.mkdir(selector.storageDir) end

local stubbornAppsList = {
    ["Gemini"] = true, ["AFFiNE"] = true, ["Terminal"] = true,
    ["System Settings"] = true, ["Hammerspoon"] = true
}

local ignoreListItems = {
    ["TheBoringNotch"] = true, ["Control Center"] = true, ["Notification Center"] = true,
    ["Dock"] = true, ["BetterDisplay"] = true, ["Stats"] = true, ["DockDoor"] = true
}

-- ==========================================
-- INTERNAL UTILITIES
-- ==========================================

local function wrapText(text, limit)
    local lines = {}
    local currentLine = ""
    for word in text:gmatch("%S+") do
        if #currentLine + #word >= limit then
            table.insert(lines, currentLine)
            currentLine = "            " .. word 
        else
            currentLine = currentLine == "" and "      ↳ " .. word or currentLine .. " " .. word
        end
    end
    table.insert(lines, currentLine)
    return lines
end

-- ==========================================
-- CORE RESTORE LOGIC (SORTED MATCHING)
-- ==========================================

local function executeRestore(filePath, layoutName)
    local data = hs.json.read(filePath)
    if not data or not data.windows then return end
    
    local launchedAny = false
    for _, winData in ipairs(data.windows) do
        local app = hs.application.get(winData.bundleID) or hs.application.get(winData.appName)
        if not app then
            hs.application.launchOrFocusByBundleID(winData.bundleID)
            launchedAny = true
        end
    end

    local function moveWindows()
        local savedByApp = {}
        for _, winData in ipairs(data.windows) do
            savedByApp[winData.appName] = savedByApp[winData.appName] or {}
            table.insert(savedByApp[winData.appName], winData)
        end

        for appName, entries in pairs(savedByApp) do
            table.sort(entries, function(a, b)
                if a.y == b.y then return a.x < b.x end
                return a.y < b.y
            end)
        end

        for appName, savedEntries in pairs(savedByApp) do
            local app = hs.application.get(savedEntries[1].bundleID) or hs.application.get(appName)
            if app then
                local physicalWins = {}
                for _, w in ipairs(app:allWindows()) do
                    if w:isVisible() and w:frame().w > 0 then table.insert(physicalWins, w) end
                end

                table.sort(physicalWins, function(a, b)
                    local af, bf = a:frame(), b:frame()
                    if af.y == bf.y then return af.x < bf.x end
                    return af.y < bf.y
                end)

                for i, winData in ipairs(savedEntries) do
                    local win = physicalWins[i]
                    if win then
                        if win:isMinimized() then win:unminimize() end
                        local x, y, w, h = winData.x, winData.y, winData.w, winData.h
                        local isStubborn = stubbornAppsList[appName] or (app:path() or ""):find("Electron")

                        local function moveAction()
                            if isStubborn then
                                local safeTitle = win:title():gsub('"', '\\"')
                                local winTarget = (appName == "Gemini") and "window 1" or string.format('first window whose name is "%s"', safeTitle)
                                local script = string.format([[
                                    tell application "System Events" to tell (first process whose unix id is %d)
                                        try
                                            set targetWin to %s
                                            set position of targetWin to {%d, %d}
                                            set size of targetWin to {%d, %d}
                                        end try
                                    end tell
                                ]], app:pid(), winTarget, x, y, w, h)
                                hs.applescript.applescript(script)
                            else
                                win:setFrame({x=x, y=y, w=w, h=h}, 0)
                            end
                        end

                        moveAction()
                        hs.timer.doAfter(0.5, moveAction)
                        hs.timer.doAfter(1.5, moveAction)
                    end
                end
            end
        end
        hs.alert.show("Restored: " .. layoutName, 1.5)
    end

    if launchedAny then
        hs.alert.show("Syncing Apps...", 3)
        hs.timer.doAfter(4.5, moveWindows)
    else
        moveWindows()
    end
end

-- ==========================================
-- MENU BAR & UI
-- ==========================================

local function captureCurrentLayout()
    local screens = hs.screen.allScreens()
    local layout = { 
        saveTime = os.date("%Y-%m-%d %H:%M:%S"), 
        screenCount = #screens,
        mode = (#screens > 1) and "Docked" or "Laptop",
        windows = {} 
    }
    for _, win in ipairs(hs.window.allWindows()) do
        local app = win:application()
        if app and win:isVisible() and win:frame().w > 0 then
            local appName = app:name() or ""
            if not (ignoreListItems[appName] or appName:find("TheBoringNotch")) then
                table.insert(layout.windows, {
                    appName = appName, bundleID = app:bundleID(), winTitle = win:title(),
                    x = math.floor(win:frame().x), y = math.floor(win:frame().y),
                    w = math.floor(win:frame().w), h = math.floor(win:frame().h)
                })
            end
        end
    end
    return layout
end

selector.barItem = hs.menubar.new()

function selector.refreshMenu()
    for _, hk in pairs(selector.hotkeys) do hk:delete() end
    selector.hotkeys = {}
    
    local screens = hs.screen.allScreens()
    selector.barItem:setTitle(#screens > 1 and "🖥️" or "💻")

    -- Define global save hotkey
    selector.hotkeys["save"] = hs.hotkey.bind({"shift", "ctrl", "alt", "cmd"}, "S", function()
        local _, name = hs.dialog.textPrompt("New Layout", "Enter name:", "", "Save", "Cancel")
        if name and name ~= "" then
            hs.json.write(captureCurrentLayout(), selector.storageDir .. name .. ".json", true, true)
            selector.refreshMenu()
        end
    end)

    local menuTable = {
        { title = "📸 Save New Layout... (⇧⌃⌥⌘S)", fn = function() hs.eventtap.keyStroke({"shift", "ctrl", "alt", "cmd"}, "S") end },
        { title = "-" }
    }
    
    local files = {}
    local iter, dir_obj = hs.fs.dir(selector.storageDir)
    if iter then for f in iter, dir_obj do if f:find("%.json$") then table.insert(files, f) end end end
    table.sort(files)

    local boldStyle = { font = { name = ".AppleSystemUIFontBold", size = 13 } }

    for i, file in ipairs(files) do
        local path, label = selector.storageDir .. file, file:gsub("%.json$", "")
        local data = hs.json.read(path)

        if i <= 9 then 
            selector.hotkeys[i] = hs.hotkey.bind({"ctrl", "alt", "cmd"}, tostring(i), function() executeRestore(path, label) end) 
        end

        -- Layout Header
        table.insert(menuTable, { 
            title = hs.styledtext.new("Layout: " .. label .. (i<=9 and " (⌃⌥⌘"..i..")" or ""), boldStyle), 
            fn = function() executeRestore(path, label) end 
        })

        if data then
            -- Context Metadata
            local modeStr = (data.mode == "Docked") and "      🖥️ Docked (".. (data.screenCount or "?") .." Screens)" or "      💻 Laptop Mode"
            table.insert(menuTable, { title = modeStr, disabled = true })
            table.insert(menuTable, { title = "      📅 Saved: " .. (data.saveTime or "Unknown"), disabled = true })
            
            -- App List (Wrapped)
            if data.windows then
                local seen, names = {}, {}
                for _, w in ipairs(data.windows) do 
                    if not seen[w.appName] then 
                        table.insert(names, w.appName .. ", ") 
                        seen[w.appName] = true 
                    end 
                end
                local appString = table.concat(names):gsub(", $", "")
                for _, line in ipairs(wrapText(appString, 45)) do 
                    table.insert(menuTable, { title = line, disabled = true }) 
                end
            end
        end

        -- Actions
        table.insert(menuTable, { 
            title = hs.styledtext.new("      🔄 Update " .. label, boldStyle), 
            fn = function() 
                hs.json.write(captureCurrentLayout(), path, true, true)
                selector.refreshMenu()
                hs.alert.show("Updated: " .. label)
            end 
        })
        table.insert(menuTable, { title = "      🗑️ Delete", fn = function() if hs.dialog.blockAlert("Delete", "Delete "..label.."?", "Delete", "Cancel", "critical") == "Delete" then os.remove(path); selector.refreshMenu() end end })
        table.insert(menuTable, { title = "-" })
    end
    selector.barItem:setMenu(menuTable)
end

selector.screenWatcher = hs.screen.watcher.new(selector.refreshMenu):start()
selector.refreshMenu()

return selector