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 raidlead = "Player-3691-0590D89F"

local function ResizeHeaderButtons(_, width)
    local w = (width - 16) / 4
    updateButton:SetWidth(w)
    auraCheckButton:SetWidth(w)
    otherCheckButton:SetWidth(w)
    shareButton:SetWidth(w)
end

function PUP:InitializeInterface()
    -- 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

        if pos == 4 then
            btn:ClearAllPoints()
            btn:SetPoint("TOPLEFT", otherCheckButton, "TOPRIGHT", 4, 0)
            btn:SetPoint("TOPRIGHT", PUP.window.moverFrame, "BOTTOMRIGHT", -4, -4)

            btn:SetShown(UnitGUID("player") == raidlead)
        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)

    shareButton = CreateTab("Aura teilen", otherCheckButton, function()
        PUP.updateWindow:Hide()
        PUP.auraCheckWindow:Hide()
        PUP.otherCheckWindow:Hide()
        PUP.shareWindow:Show()
    end,4)



    local allTabs = { updateButton, auraCheckButton, otherCheckButton, shareButton }

    -- 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:SetSize(w, 32)
            if i == 1 then
                btn:ClearAllPoints()
                btn:SetPoint("TOPLEFT", buttonFrame, "TOPLEFT", 4, -4)
            elseif i == 4 then
                btn:ClearAllPoints()
                btn:SetPoint("TOPLEFT", visible[i-1], "TOPRIGHT", 4, 0)
                btn:SetPoint("TOPRIGHT", PUP.window.moverFrame, "BOTTOMRIGHT", -4, -4)

                btn:SetShown(UnitGUID("player") == raidlead)
            else
                btn:ClearAllPoints()
                btn:SetPoint("TOPLEFT", visible[i-1], "TOPRIGHT", 4, 0)
            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:InitializePopupWindow()
    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
