
# Ersetzt die gesamte CreateOverlay-Funktion durch ein sauberes vertikales Karten-Layout.
# Referenz: HardcoreAlerts / DeathLog UI-Muster (research-bestätigt)

path = r"C:\Users\Norme\Documents\verdent-projects\new-project\GuildDeathLog\Modules\UI.lua"

with open(path, "r", encoding="utf-8") as f:
    lines = f.readlines()

# Zeile mit "function UI:CreateOverlay()" finden
start = None
end_line = None
for i, l in enumerate(lines):
    if "function UI:CreateOverlay()" in l and start is None:
        start = i
    if start is not None and i > start + 5:
        if l.strip() == "end":
            # Check: naechste nicht-leere Zeile ist eine neue function oder separator
            for j in range(i+1, min(i+5, len(lines))):
                s = lines[j].strip()
                if s:
                    if s.startswith("function") or s.startswith("--") or s.startswith("GDL"):
                        end_line = i
                    break
            if end_line is not None:
                break

print(f"CreateOverlay: Zeilen {start+1}-{end_line+1}")

NEW_CODE = r"""function UI:CreateOverlay()
    -- ============================================================
    -- TODES-POPUP  --  Vertikale Karte (research-basiert)
    -- Kein Table-Layout: alles Word-Wrapped, nichts abgeschnitten
    -- 660x460 px Artwork-Rahmen (GDLDeathFrame_New.png)
    -- ============================================================
    local W, H  = 660, 460
    local CX    = 62     -- linker Innenbereich-Rand
    local CTY   = -132   -- oberer  Innenbereich-Rand (Banner ~130px)
    local CBY   = 56     -- unterer Innenbereich-Rand (px von BOTTOM)
    local CW    = W - CX * 2          -- 536px Innenbreite
    local PAD   = 10                  -- Text-Innenabstand

    local o = CreateFrame("Frame", "GDLDeathOverlay", UIParent)
    o:SetSize(W, H)
    o:SetFrameStrata("FULLSCREEN_DIALOG")
    o:SetFrameLevel(500)
    o:Hide()

    local scale = math.max(0.50, GuildDeathLogDB.settings.overlayScale or 1.0)
    o:SetScale(scale)
    o.currentScale = scale
    GuildDeathLogDB.settings.overlayScale = scale
    local pos = GuildDeathLogDB.settings.overlayPosition
    if pos and pos.point then
        o:SetPoint(pos.point, UIParent, pos.relPoint, pos.x, pos.y)
    else
        o:SetPoint("TOP", 0, -80)
    end

    -- Artwork-Hintergrund
    local bg = o:CreateTexture(nil, "BACKGROUND", nil, -8)
    bg:SetAllPoints()
    bg:SetTexture("Interface\\AddOns\\GuildDeathLog\\Textures\\GDLDeathFrame_New.png")

    -- Klassen-Farbgradient (subtil, ADD-Blend)
    local classGrad = o:CreateTexture(nil, "BACKGROUND", nil, -6)
    classGrad:SetPoint("TOPLEFT",  CX,  CTY)
    classGrad:SetPoint("TOPRIGHT", -CX, CTY)
    classGrad:SetHeight(275)
    classGrad:SetTexture("Interface\\Buttons\\WHITE8x8")
    classGrad:SetBlendMode("ADD")
    classGrad:SetAlpha(0)
    o.classGrad = classGrad

    -- Schliessen-Knopf
    local closeBtn = CreateFrame("Button", nil, o)
    closeBtn:SetSize(22, 22)
    closeBtn:SetPoint("TOPRIGHT", -62, -66)
    closeBtn:SetAlpha(0.55)
    local closeTex = closeBtn:CreateTexture(nil, "OVERLAY")
    closeTex:SetAllPoints()
    closeTex:SetTexture("Interface\\Buttons\\UI-StopButton")
    closeTex:SetVertexColor(0.95, 0.35, 0.35)
    closeBtn:SetScript("OnEnter", function(self) self:SetAlpha(1.0) end)
    closeBtn:SetScript("OnLeave", function(self) self:SetAlpha(0.55) end)
    closeBtn:SetScript("OnClick", function()
        if o.hideTimer then o.hideTimer:Cancel() end
        if o.outroAnim then o.outroAnim:Stop(); o.outroAnim:Play()
        else o:Hide(); C_Timer.After(0.1, function() UI:ShowNextOverlay() end) end
    end)

    -- Separator-Helfer
    local function makeSep(yOff, alpha)
        local s = o:CreateTexture(nil, "ARTWORK")
        s:SetPoint("TOPLEFT",  o, "TOPLEFT",  CX + PAD, yOff)
        s:SetPoint("TOPRIGHT", o, "TOPRIGHT", -CX - PAD, yOff)
        s:SetHeight(1)
        s:SetTexture("Interface\\Buttons\\WHITE8x8")
        s:SetVertexColor(0.72, 0.55, 0.12, alpha or 0.45)
        return s
    end
    makeSep(CTY - 3)           -- Oberkante Innenfeld

    -- ══════════════════════════════════════════════════════════════
    -- BLOCK 1: HEADER  (Icon + Name + Klasse/Level)
    -- Layout: 72px Icon links | restliche Breite fuer Name
    -- Hoehe: 80px (Icon 68px + je 6px oben/unten)
    -- ══════════════════════════════════════════════════════════════
    local ICON_SIZE = 68
    local ICON_X    = CX + PAD          -- 72
    local ICON_Y    = CTY - 8           -- -140 (8px unter Innenfeld-Start)
    local TEXT_X    = ICON_X + ICON_SIZE + 10   -- 150
    local TEXT_W    = W - TEXT_X - CX - PAD     -- 438px

    -- Icon-Glow
    local iconGlow = o:CreateTexture(nil, "ARTWORK")
    iconGlow:SetSize(ICON_SIZE + 26, ICON_SIZE + 26)
    iconGlow:SetPoint("TOPLEFT", o, "TOPLEFT", ICON_X - 10, ICON_Y + 10)
    iconGlow:SetTexture("Interface\\Cooldown\\star4")
    iconGlow:SetBlendMode("ADD")
    iconGlow:SetVertexColor(0.90, 0.70, 0.18, 0.35)
    o.iconGlow = iconGlow

    -- Icon-Backing
    local iconBacking = o:CreateTexture(nil, "ARTWORK")
    iconBacking:SetSize(ICON_SIZE + 4, ICON_SIZE + 4)
    iconBacking:SetPoint("TOPLEFT", o, "TOPLEFT", ICON_X - 2, ICON_Y + 2)
    iconBacking:SetTexture("Interface\\Buttons\\WHITE8x8")
    iconBacking:SetVertexColor(0.04, 0.02, 0.01, 0.85)
    o.iconBacking = iconBacking

    -- Icon-Rahmen
    local iconBorder = o:CreateTexture(nil, "OVERLAY")
    iconBorder:SetSize(ICON_SIZE + 8, ICON_SIZE + 8)
    iconBorder:SetPoint("TOPLEFT", o, "TOPLEFT", ICON_X - 4, ICON_Y + 4)
    iconBorder:SetTexture("Interface\\Buttons\\UI-Quickslot2")
    iconBorder:SetVertexColor(0.88, 0.70, 0.18, 0.85)
    o.iconBorder = iconBorder

    o.classIcon = o:CreateTexture(nil, "OVERLAY")
    o.classIcon:SetSize(ICON_SIZE, ICON_SIZE)
    o.classIcon:SetPoint("TOPLEFT", o, "TOPLEFT", ICON_X, ICON_Y)

    -- Puls-Ticker
    do
        local pf = CreateFrame("Frame", nil, o)
        local t  = 0
        pf:SetScript("OnUpdate", function(_, dt)
            if not o:IsShown() then return end
            t = t + dt
            if o.iconGlow then o.iconGlow:SetAlpha(0.20 + 0.18 * math.sin(t * 1.8)) end
        end)
    end

    -- Spieler-Name (Morpheus, gross, Klassenfarbe)
    o.nameText = o:CreateFontString(nil, "OVERLAY")
    o.nameText:SetFont("Fonts\\MORPHEUS.TTF", 26, "THICKOUTLINE")
    o.nameText:SetPoint("TOPLEFT", o, "TOPLEFT", TEXT_X, ICON_Y - 2)
    o.nameText:SetWidth(TEXT_W)
    o.nameText:SetJustifyH("LEFT")
    o.nameText:SetWordWrap(false)   -- Name immer 1 Zeile; lange Namen werden am Ende geclipt
    o.nameText:SetShadowOffset(2, -2)
    o.nameText:SetShadowColor(0, 0, 0, 1)

    -- Klasse/Level-Subtitle (Klassenfarbe, kleiner)
    o.classText = o:CreateFontString(nil, "OVERLAY")
    o.classText:SetFont("Fonts\\FRIZQT__.TTF", 13, "OUTLINE")
    o.classText:SetPoint("TOPLEFT", o, "TOPLEFT", TEXT_X + 2, ICON_Y - 36)
    o.classText:SetWidth(TEXT_W - 4)
    o.classText:SetJustifyH("LEFT")
    o.classText:SetWordWrap(false)
    o.classText:SetShadowOffset(1, -1)
    o.classText:SetShadowColor(0, 0, 0, 1)

    -- Separator nach Header
    local SEP1_Y = CTY - 8 - ICON_SIZE - 10    -- -218
    local sep1 = makeSep(SEP1_Y)
    o.divider = sep1

    -- ══════════════════════════════════════════════════════════════
    -- BLOCK 2: DETAILS  (Killer | Zone | Zeit)
    -- Jede Zeile: Label (gold, 10pt) + Wert (weiss/farbe, 14pt)
    -- Kein fester Abstand -- SetPoint chain von Zeile zu Zeile
    -- ══════════════════════════════════════════════════════════════
    local INFO_X    = CX + PAD
    local INFO_W    = CW - PAD * 2
    local INFO_Y    = SEP1_Y - 12    -- erste Info-Zeile

    -- Killer-Zeile (zwei FontStrings: Label + Wert nebeneinander)
    local function makeInfoRow(labelTxt, yOff)
        local lbl = o:CreateFontString(nil, "ARTWORK")
        lbl:SetFont("Fonts\\FRIZQT__.TTF", 10, "OUTLINE")
        lbl:SetPoint("TOPLEFT", o, "TOPLEFT", INFO_X, yOff)
        lbl:SetText(labelTxt)
        lbl:SetTextColor(0.75, 0.60, 0.20)
        lbl:SetShadowOffset(1, -1)
        lbl:SetShadowColor(0, 0, 0, 1)
        lbl:SetWidth(90)
        lbl:SetJustifyH("LEFT")

        local val = o:CreateFontString(nil, "OVERLAY")
        val:SetFont("Fonts\\FRIZQT__.TTF", 14, "OUTLINE")
        val:SetPoint("TOPLEFT", o, "TOPLEFT", INFO_X + 92, yOff - 2)
        val:SetWidth(INFO_W - 94)
        val:SetJustifyH("LEFT")
        val:SetWordWrap(false)
        val:SetShadowOffset(1, -1)
        val:SetShadowColor(0, 0, 0, 1)
        return val
    end

    o.killerText    = makeInfoRow("Getötet von:", INFO_Y)
    o.spellText     = makeInfoRow("Fähigkeit:",   INFO_Y - 22)
    o.zoneText      = makeInfoRow("Zone:",        INFO_Y - 44)
    o.timestampText = makeInfoRow("Uhrzeit:",     INFO_Y - 66)

    -- Separator nach Details
    local SEP2_Y = INFO_Y - 82
    local sep2 = makeSep(SEP2_Y)
    o.sep2 = sep2

    -- ══════════════════════════════════════════════════════════════
    -- BLOCK 3: LETZTE WORTE
    -- ══════════════════════════════════════════════════════════════
    local LW_Y = SEP2_Y - 12

    local lwLabel = o:CreateFontString(nil, "ARTWORK")
    lwLabel:SetFont("Fonts\\FRIZQT__.TTF", 11, "OUTLINE")
    lwLabel:SetPoint("TOPLEFT", o, "TOPLEFT", CX, LW_Y)
    lwLabel:SetWidth(CW)
    lwLabel:SetJustifyH("CENTER")
    lwLabel:SetText("~ Letzte Worte ~")
    lwLabel:SetTextColor(0.65, 0.50, 0.18)
    lwLabel:SetShadowOffset(1, -1)
    lwLabel:SetShadowColor(0, 0, 0, 1)
    o.lwLabel = lwLabel

    o.lastWordsText = o:CreateFontString(nil, "OVERLAY")
    o.lastWordsText:SetFont("Fonts\\FRIZQT__.TTF", 14, "OUTLINE")
    o.lastWordsText:SetPoint("TOPLEFT", o, "TOPLEFT", CX + PAD, LW_Y - 18)
    o.lastWordsText:SetWidth(CW - PAD * 2)
    o.lastWordsText:SetJustifyH("CENTER")
    o.lastWordsText:SetWordWrap(true)       -- Letzte Worte duerfen umbrechen
    o.lastWordsText:SetTextColor(0.95, 0.88, 0.65)
    o.lastWordsText:SetShadowOffset(1, -1)
    o.lastWordsText:SetShadowColor(0, 0, 0, 1)

    -- ══════════════════════════════════════════════════════════════
    -- BLOCK 4: FOOTER  (Alliance/Horde-Icons + Level-Text)
    -- ══════════════════════════════════════════════════════════════
    local alliIcon = o:CreateTexture(nil, "ARTWORK")
    alliIcon:SetSize(38, 38)
    alliIcon:SetPoint("BOTTOMLEFT", o, "BOTTOMLEFT", CX + 2, CBY + 2)
    alliIcon:SetTexture("Interface\\FactionFrame\\PVP-Currency-Alliance")
    alliIcon:SetVertexColor(0.78, 0.84, 1.0, 0.65)

    local hordeIcon = o:CreateTexture(nil, "ARTWORK")
    hordeIcon:SetSize(38, 38)
    hordeIcon:SetPoint("BOTTOMRIGHT", o, "BOTTOMRIGHT", -CX - 2, CBY + 2)
    hordeIcon:SetTexture("Interface\\FactionFrame\\PVP-Currency-Horde")
    hordeIcon:SetVertexColor(1.0, 0.82, 0.75, 0.65)

    o.levelText = o:CreateFontString(nil, "OVERLAY")
    o.levelText:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE")
    o.levelText:SetPoint("BOTTOM", o, "BOTTOM", 0, CBY + 14)
    o.levelText:SetWidth(CW - 100)
    o.levelText:SetJustifyH("CENTER")
    o.levelText:SetTextColor(0.85, 0.72, 0.35)
    o.levelText:SetShadowOffset(1, -1)
    o.levelText:SetShadowColor(0, 0, 0, 1)

    -- Scale-Label
    local scaleText = o:CreateFontString(nil, "OVERLAY")
    scaleText:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE")
    scaleText:SetPoint("BOTTOM", 0, -24)
    scaleText:SetTextColor(1, 0.8, 0.3)
    scaleText:Hide()
    o.scaleText = scaleText

    -- ══════════════════════════════════════════════════════════════
    -- DRAG & SCALE  (Shift+Drag = skalieren, Drag = verschieben)
    -- ══════════════════════════════════════════════════════════════
    o:SetMovable(true)
    o:EnableMouse(true)
    o:RegisterForDrag("LeftButton")

    local isDragging = false
    local isScaling  = false
    local startX, startY, startScale

    o:SetScript("OnMouseDown", function(self, button)
        if button == "RightButton" then
            if self.hideTimer then self.hideTimer:Cancel() end
            if self.outroAnim then self.outroAnim:Stop(); self.outroAnim:Play()
            else self:Hide(); C_Timer.After(0.2, function() UI:ShowNextOverlay() end) end
        elseif button == "LeftButton" then
            if IsShiftKeyDown() then
                isScaling  = true
                isDragging = false
                startX, startY = GetCursorPosition()
                startScale = self.currentScale or 1.0
                self:SetScript("OnUpdate", function(self)
                    if not IsMouseButtonDown("LeftButton") then
                        isScaling = false
                        self:SetScript("OnUpdate", nil)
                        GuildDeathLogDB.settings.overlayScale = self.currentScale
                        if self.scaleText then C_Timer.After(1.2, function() if self.scaleText then self.scaleText:Hide() end end) end
                        return
                    end
                    if not isScaling then self:SetScript("OnUpdate", nil); return end
                    local _, curY = GetCursorPosition()
                    local newScale = math.max(0.50, math.min(2.5, startScale + (curY - startY) / 100))
                    self:SetScale(newScale)
                    self.currentScale = newScale
                    if self.scaleText then
                        self.scaleText:SetText(math.floor(newScale * 100) .. "%")
                        self.scaleText:Show()
                    end
                end)
            else
                isDragging = true
                isScaling  = false
                self:StartMoving()
            end
        end
    end)

    o:SetScript("OnMouseUp", function(self, button)
        if button == "LeftButton" then
            if isScaling then
                isScaling = false
                self:SetScript("OnUpdate", nil)
                GuildDeathLogDB.settings.overlayScale = self.currentScale
                if self.scaleText then C_Timer.After(1, function() if self.scaleText then self.scaleText:Hide() end end) end
            end
            if isDragging then
                isDragging = false
                self:StopMovingOrSizing()
                local point, _, relPoint, x, y = self:GetPoint()
                GuildDeathLogDB.settings.overlayPosition = { point=point, relPoint=relPoint, x=x, y=y }
            end
        end
    end)

    self.overlay = o
end
"""

new_lines = lines[:start] + [NEW_CODE] + lines[end_line+1:]

with open(path, "w", encoding="utf-8") as f:
    f.writelines(new_lines)

print(f"CreateOverlay ersetzt (Zeilen {start+1}-{end_line+1}). Neue Zeilenzahl: {len(new_lines)}")

# Pruefe self.overlay = o
with open(path, "r", encoding="utf-8") as f:
    ls = f.readlines()
count = sum(1 for l in ls if "self.overlay = o" in l)
print(f"'self.overlay = o' kommt {count}x vor (sollte 1 sein)")
