local addonName, TOAST = ...

TOAST.commands = {}

-- Single source of truth for the addon version, read from the .toc so it
-- never drifts out of sync with what's actually shipped.
local getAddOnMetadata = C_AddOns and C_AddOns.GetAddOnMetadata or GetAddOnMetadata
TOAST.VERSION = (getAddOnMetadata and getAddOnMetadata(addonName, "Version")) or "?"

local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_LOGIN")
f:SetScript("OnEvent", function()
    print("|cff33ff99TOAST|r v" .. TOAST.VERSION .. " geladen.")
end)

-- Persists top-level window positions (leaderboard, dashboard, ...) in
-- TOAST_DB so dragging a window sticks across /reload instead of resetting
-- to CENTER every time. Called by each window's header OnDragStop.
function TOAST.SaveWindowPosition(frame, key)
    local point, _, relativePoint, x, y = frame:GetPoint()
    TOAST_DB.windowPos = TOAST_DB.windowPos or {}
    TOAST_DB.windowPos[key] = { point = point, relativePoint = relativePoint, x = x, y = y }
end

function TOAST.RestoreWindowPosition(frame, key)
    local pos = TOAST_DB.windowPos and TOAST_DB.windowPos[key]
    if pos then
        frame:SetPoint(pos.point, UIParent, pos.relativePoint, pos.x, pos.y)
    else
        frame:SetPoint("CENTER")
    end
end

TOAST.DUNGEON_SHORT = {
    ["Algeth'ar Academy"]       = "AA",
    ["Seat of the Triumvirate"] = "SOTT",
    ["Skyreach"]                = "SKY",
    ["Pit of Saron"]            = "PIT",
    ["Windrunner Spire"]        = "SPIRE",
    ["Maisara Caverns"]         = "CAVERN",
    ["Nexus-Point Xenas"]       = "XENAS",
    ["Magisters' Terrace"]      = "MT",
}

TOAST.commands[""] = function()
    if TOAST.IsInCommunity then
        TOAST.inCommunity = TOAST.IsInCommunity()
    end

    local dismissed = TOAST_DB.dismissedWelcome or {}
    local charName = UnitName("player")

    if not TOAST.inCommunity then
        -- Niet in community = altijd popup
        if dismissed[charName] ~= true then
            local discordKnown = TOAST_DB.me ~= nil
            if discordKnown then
                -- Community link alleen
                if TOAST.ShowWelcome then
                    TOAST.ShowWelcome("community")
                end
            else
                -- Community + Discord link
                if TOAST.ShowWelcome then
                    TOAST.ShowWelcome("both")
                end
            end
        else
            TOAST.commands["dashboard"]()  -- dismissed, door naar dashboard
        end
        return
    end

    -- Wel in community
    if TOAST.me then
        -- In roster, herkend → dashboard
        TOAST.commands["dashboard"]()
        return
    end

    if TOAST_DB.me then
        -- Niet in roster maar discord bekend (alt) → dashboard
        TOAST.commands["dashboard"]()
        return
    end

    -- In community maar discord onbekend → discord popup
    if dismissed[charName] ~= true then
        if TOAST.ShowWelcome then
            TOAST.ShowWelcome("discord")
        end
    else
        TOAST.commands["dashboard"]()
    end
end

SLASH_TOAST1 = "/toast"
SlashCmdList["TOAST"] = function(input)
    if strtrim(input) == "" then
        TOAST.commands[""]()
    else
        print("|cff33ff99TOAST|r: /toast heeft geen subcommands meer.")
    end
end