local _, PUP = ...

-- Tooltip
CreateFrame("GameTooltip", "LRTooltip", UIParent, "GameTooltipTemplate")
PUP.Tooltip = _G["LRTooltip"]
PUP.Tooltip.TextLeft1:SetFontObject(PUFont13)

local windowWidth, windowHeight = 640, 400
local updateButton, auraCheckButton, otherCheckButton, shareButton

local allTabs

local raidlead =  "Player-3691-0590D89F" --"Player-5764-004A5A17" für PTR

local isRaidLead = false

local function ResizeHeaderButtons(_, width)
    -- Zähle die sichtbaren Tabs
    local visibleCount = 0
    for _, btn in ipairs(allTabs) do
        if btn and btn:IsShown() then
            visibleCount = visibleCount + 1
        end
    end
    if visibleCount == 0 then return end

    local w = (width - 16) / visibleCount

    -- Setze die Breite für alle sichtbaren Tabs
    for _, btn in ipairs(allTabs) do
        if btn and btn:IsShown() then
            btn:SetWidth(w)
        end
    end
end

function PUP:InitializeInterface()
    isRaidLead = UnitGUID("player") == raidlead
    -- Window
    PUP.window = PUP:CreateWindow("Main", true, true, true)
    PUP.window:SetFrameStrata("HIGH")
    PUP.window:SetResizeBounds(windowWidth, windowHeight)
    PUP.window:SetPoint("CENTER")
    PUP.window:Hide()

    PUP.window:AddButton(
        "Interface\\Addons\\PrepullUpdater\\Media\\Textures\\Cogwheel.tga",
        "Einstellungen",
        function()
            PUP.settingsWindow:SetShown(not PUP.settingsWindow:IsShown())
        end
    )

    -- Button frame
    local buttonFrame = CreateFrame("Frame", nil, PUP.window)

    buttonFrame:SetPoint("TOPLEFT", PUP.window.moverFrame, "BOTTOMLEFT")
    buttonFrame:SetPoint("TOPRIGHT", PUP.window.moverFrame, "BOTTOMRIGHT")

    buttonFrame:SetHeight(32)
    buttonFrame:SetScript("OnSizeChanged", ResizeHeaderButtons)

    local borderColor = PUP.gs.visual.borderColor

    -- Tab‑Erzeugung
    local function CreateTab(text, anchor, onClick, pos)
        local btn = CreateFrame("Frame", nil, PUP.window)
        btn:SetSize((windowWidth - 16) / 4, 32)
        if anchor then
            btn:SetPoint("TOPLEFT", anchor, "TOPRIGHT", 4, 0)
        else
            btn:SetPoint("TOPLEFT", buttonFrame, "TOPLEFT", 4, -4)
        end

        btn:EnableMouse(true)
        btn.highlight = btn:CreateTexture(nil, "HIGHLIGHT")
        btn.highlight:SetColorTexture(1, 1, 1, 0.05)
        btn.highlight:SetAllPoints()
        btn.text = btn:CreateFontString(nil, "OVERLAY")
        btn.text:SetFontObject(PUFont17)
        btn.text:SetPoint("CENTER")
        btn.text:SetText(string.format("|cff%s%s|r", PUP.gs.visual.colorStrings.white, text))
        PUP:AddBorder(btn)
        btn:SetBorderColor(borderColor.r, borderColor.g, borderColor.b)
        btn:SetScript("OnMouseDown", onClick)
        return btn
    end

    -- Die vier Tabs
    updateButton = CreateTab("Update", nil, function()
        PUP.updateWindow:Show()
        PUP.auraCheckWindow:Hide()
        PUP.otherCheckWindow:Hide()
        PUP.shareWindow:Hide()
    end, 1)

    auraCheckButton = CreateTab("Aura checken", updateButton, function()
        PUP.updateWindow:Hide()
        PUP.auraCheckWindow:Show()
        PUP.otherCheckWindow:Hide()
        PUP.shareWindow:Hide()
    end, 2)

    otherCheckButton = CreateTab("Anderes checken", auraCheckButton, function()
        PUP.updateWindow:Hide()
        PUP.auraCheckWindow:Hide()
        PUP.otherCheckWindow:Show()
        PUP.shareWindow:Hide()
    end, 3)

    -- Der Share‑Tab wird nur angezeigt, wenn der Spieler Raid‑Leader ist
    shareButton = CreateTab("Aura teilen", otherCheckButton, function()
        PUP.updateWindow:Hide()
        PUP.auraCheckWindow:Hide()
        PUP.otherCheckWindow:Hide()
        PUP.shareWindow:Show()
    end, 4)


    allTabs = { updateButton, auraCheckButton, otherCheckButton, shareButton }

    shareButton:SetShown(isRaidLead)

    -- Erneute Verteilung der sichtbaren Tabs
    local function LayoutTabs()
        -- Sammle die sichtbaren Tabs
        local visible = {}
        for _, btn in ipairs(allTabs) do
            if btn:IsShown() then
                tinsert(visible, btn)
            end
        end

        local count = #visible

        if count == 0 then return end

        -- Berechne neue Breite
        local totalWidth = PUP.window:GetWidth() - 16
        local w = totalWidth / count

        -- Setze Position & Größe
        for i, btn in ipairs(visible) do
            btn:ClearAllPoints()
            btn:SetSize(w, 32)
            if i == 1 then
                btn:SetPoint("TOPLEFT", buttonFrame, "TOPLEFT", 4, -4)
            else
                btn:SetPoint("TOPLEFT", visible[i - 1], "TOPRIGHT", 4, 0)
            end
            if i == count then
                btn:SetPoint("TOPRIGHT", PUP.window.moverFrame, "BOTTOMRIGHT", -4, -4)
            end
        end
    end

    -- Rufe LayoutTabs auf, wann immer das Fenster gezeigt wird
    PUP.window:HookScript("OnShow", LayoutTabs)
    -- Und auch nach dem Setzen der Sichtbarkeit des Share‑Tabs
    shareButton:SetScript("OnShow", LayoutTabs)
    shareButton:SetScript("OnHide", LayoutTabs)


    -- Sub‑Frames für Tabs
    PUP.updateWindow = CreateFrame("Frame", nil, PUP.window)
    PUP.updateWindow:SetPoint("TOPLEFT", buttonFrame, "BOTTOMLEFT")
    PUP.updateWindow:SetPoint("BOTTOMRIGHT", PUP.window, "BOTTOMRIGHT")

    PUP.auraCheckWindow = CreateFrame("Frame", nil, PUP.window)
    PUP.auraCheckWindow:SetPoint("TOPLEFT", buttonFrame, "BOTTOMLEFT")
    PUP.auraCheckWindow:SetPoint("BOTTOMRIGHT", PUP.window, "BOTTOMRIGHT")

    PUP.otherCheckWindow = CreateFrame("Frame", nil, PUP.window)
    PUP.otherCheckWindow:SetPoint("TOPLEFT", buttonFrame, "BOTTOMLEFT")
    PUP.otherCheckWindow:SetPoint("BOTTOMRIGHT", PUP.window, "BOTTOMRIGHT")

    PUP.shareWindow = CreateFrame("Frame", nil, PUP.window)
    PUP.shareWindow:SetPoint("TOPLEFT", buttonFrame, "BOTTOMLEFT")
    PUP.shareWindow:SetPoint("BOTTOMRIGHT", PUP.window, "BOTTOMRIGHT")

    -- Jetzt kann InitializeAuraShare den shareWindow parenten
    self:InitializeAuraShare()

    -- Anfangszustand: nur Update‑Tab sichtbar
    PUP.updateWindow:Show()
    PUP.auraCheckWindow:Hide()
    PUP.otherCheckWindow:Hide()
    PUP.shareWindow:Hide()

    -- Final Größe
    PUP.window:SetSize(windowWidth, windowHeight)

    -- Weitere Initialisierungen
    PUP:InitializeSettings()

    -- ESC‑Handler
    PUP.window:SetScript("OnKeyDown", function(_, key)
        if InCombatLockdown() then return end
        if key == "ESCAPE" then
            PUP.window:SetPropagateKeyboardInput(false)
            PUP.window:Hide()
        else
            PUP.window:SetPropagateKeyboardInput(true)
        end
    end)
end
