local _, PUP = ...

-- Tooltip
CreateFrame("GameTooltip", "LRTooltip", UIParent, "GameTooltipTemplate")

PUP.Tooltip = _G["LRTooltip"]
PUP.Tooltip.TextLeft1:SetFontObject(PUFont13)

-- Main window
local windowWidth = 640
local windowHeight = 400

local updateButton, auraCheckButton, otherCheckButton

local function ResizeHeaderButtons(_, buttonFrameWidth)
    local combinedButtonWidth = buttonFrameWidth - 16

    updateButton:SetWidth(combinedButtonWidth / 3)
    auraCheckButton:SetWidth(combinedButtonWidth / 3)
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)

    -- Update button
    updateButton = CreateFrame("Frame", nil, PUP.window)

    updateButton:SetPoint("TOPLEFT", buttonFrame, "TOPLEFT", 4, -4)
    updateButton:SetPoint("BOTTOMLEFT", buttonFrame, "BOTTOMLEFT", 4, 0)
    updateButton:EnableMouse(true)

    updateButton.highlight = updateButton:CreateTexture(nil, "HIGHLIGHT")
    updateButton.highlight:SetColorTexture(1, 1, 1, 0.05)
    updateButton.highlight:SetAllPoints()

    updateButton.text = updateButton:CreateFontString(nil, "OVERLAY")
    updateButton.text:SetFontObject(PUFont17)
    updateButton.text:SetPoint("CENTER", updateButton, "CENTER")
    updateButton.text:SetText(string.format("|cff%sUpdate|r", PUP.gs.visual.colorStrings.white))

    updateButton:SetScript(
        "OnMouseDown",
        function()
            PUP.updateWindow:Show()
            PUP.auraCheckWindow:Hide()
            PUP.otherCheckWindow:Hide()
        end
    )

    local borderColor = PUP.gs.visual.borderColor
    PUP:AddBorder(updateButton)
    updateButton:SetBorderColor(borderColor.r, borderColor.g, borderColor.b)

    -- Aura check button
    auraCheckButton = CreateFrame("Frame", nil, PUP.window)

    auraCheckButton:SetPoint("TOPLEFT", updateButton, "TOPRIGHT", 4, 0)
    auraCheckButton:SetPoint("BOTTOMLEFT", updateButton, "BOTTOMRIGHT", 4, 0)
    auraCheckButton:EnableMouse(true)

    auraCheckButton.highlight = auraCheckButton:CreateTexture(nil, "HIGHLIGHT")
    auraCheckButton.highlight:SetColorTexture(1, 1, 1, 0.05)
    auraCheckButton.highlight:SetAllPoints()

    auraCheckButton.text = auraCheckButton:CreateFontString(nil, "OVERLAY")
    auraCheckButton.text:SetFontObject(PUFont17)
    auraCheckButton.text:SetPoint("CENTER", auraCheckButton, "CENTER")
    auraCheckButton.text:SetText(string.format("|cff%sAura checken|r", PUP.gs.visual.colorStrings.white))

    auraCheckButton:SetScript(
        "OnMouseDown",
        function()
            PUP.updateWindow:Hide()
            PUP.auraCheckWindow:Show()
            PUP.otherCheckWindow:Hide()
        end
    )

    PUP:AddBorder(auraCheckButton)
    auraCheckButton:SetBorderColor(borderColor.r, borderColor.g, borderColor.b)

    -- Other check button
    otherCheckButton = CreateFrame("Frame", nil, PUP.window)

    otherCheckButton:SetPoint("TOPLEFT", auraCheckButton, "TOPRIGHT", 4, 0)
    otherCheckButton:SetPoint("BOTTOMRIGHT", buttonFrame, "BOTTOMRIGHT", -4, 0)
    otherCheckButton:EnableMouse(true)

    otherCheckButton.highlight = otherCheckButton:CreateTexture(nil, "HIGHLIGHT")
    otherCheckButton.highlight:SetColorTexture(1, 1, 1, 0.05)
    otherCheckButton.highlight:SetAllPoints()

    otherCheckButton.text = otherCheckButton:CreateFontString(nil, "OVERLAY")
    otherCheckButton.text:SetFontObject(PUFont17)
    otherCheckButton.text:SetPoint("CENTER", otherCheckButton, "CENTER")
    otherCheckButton.text:SetText(string.format("|cff%sAnderes checken|r", PUP.gs.visual.colorStrings.white))

    otherCheckButton:SetScript(
        "OnMouseDown",
        function()
            PUP.updateWindow:Hide()
            PUP.auraCheckWindow:Hide()
            PUP.otherCheckWindow:Show()
        end
    )

    PUP:AddBorder(otherCheckButton)
    otherCheckButton:SetBorderColor(borderColor.r, borderColor.g, borderColor.b)

    -- Sub windows
    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.auraCheckWindow:Hide()
    PUP.otherCheckWindow:Hide()

    PUP.window:SetSize(windowWidth, windowHeight)

    PUP:InitializePopupWindow()
    PUP:InitializeSettings()

    -- When escape is pressed, close the main window
    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