#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Patch DisplayOverlay in UI.lua - replace old cinematic block with DeathCinematic"""
import re, sys

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

with open(path, "rb") as f:
    raw = f.read()

src = raw.decode("utf-8", errors="replace")

# Find line range 3607-3662 (the old sound+cinematic+hide block)
lines = src.splitlines(keepends=True)

# Find start: line with "EPISCHER SOUND" (or similar sound line)
start_idx = None
end_idx   = None
for i, line in enumerate(lines):
    if start_idx is None and ("EPISCHER SOUND" in line or "PlaySound(8959" in line and "Master" in line):
        # go back to find the closing "end" of lastWords block
        start_idx = i
    if start_idx is not None and i > start_idx and line.strip() == "end" and "DisplayOverlay" not in line:
        end_idx = i
        break

if start_idx is None or end_idx is None:
    print(f"ERROR: could not find block. start={start_idx} end={end_idx}")
    # dump lines around 3605
    for i in range(3600, min(3670, len(lines))):
        print(f"{i+1}| {lines[i]}", end="")
    sys.exit(1)

print(f"Found block: lines {start_idx+1}-{end_idx+1}")

NEW_BLOCK = """
    -- \u2500\u2500 OVERLAY VORBEREITEN (Alpha=0, DeathCinematic steuert Erscheinen) \u2500\u2500\u2500\u2500
    o:SetAlpha(0)
    o:SetScale(o.currentScale or 1.0)
    o:Show()

    -- \u2500\u2500 TYPEWRITER: Name nach 0.92s enth\u00fcllent \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
    local nameStr = death.name or ""
    C_Timer.After(0.92, function()
        if o:IsShown() then
            if FX and FX.TypewriterText then
                FX.TypewriterText(o.nameText, nameStr, 18)
            else
                o.nameText:SetText(nameStr)
            end
        end
    end)

    -- \u2500\u2500 CINEMATIC DEATH EFFECTS (Master-Orchestrator) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
    if FX then
        FX.DeathCinematic(o, r, g, b)
    end

    -- Zusatz-Sound (Erg\u00e4nzung zu BellTolls)
    C_Timer.After(0.15, function()
        local snd = (SOUNDKIT and SOUNDKIT.RAID_WARNING) or 8959
        pcall(function() PlaySound(snd, "Master") end)
    end)

    -- \u2500\u2500 HIDE NACH 12 SEKUNDEN \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
    o.hideTimer = C_Timer.NewTimer(12, function()
        if not o.outroAnim then
            o.outroAnim = o:CreateAnimationGroup()
            local outroFade = o.outroAnim:CreateAnimation("Alpha")
            outroFade:SetFromAlpha(1); outroFade:SetToAlpha(0)
            outroFade:SetDuration(1.5); outroFade:SetSmoothing("IN")
            o.outroAnim:SetScript("OnFinished", function()
                o:Hide(); o:SetAlpha(1)
                local FX2 = GDL:GetModule("AnimFX")
                if FX2 then
                    FX2.StopOverlayAmbience(o._ambienceHandles)
                    FX2.CinematicBars(false, 0.55)
                end
                o._ambienceHandles = nil
                C_Timer.After(0.3, function() UI:ShowNextOverlay() end)
            end)
        end
        o.outroAnim:Stop()
        o.outroAnim:Play()
    end)
end
"""

# Replace from start_idx to end_idx (inclusive)
new_lines = lines[:start_idx] + [NEW_BLOCK] + lines[end_idx+1:]
result = "".join(new_lines)

with open(path, "wb") as f:
    f.write(result.encode("utf-8"))

print("OK: DisplayOverlay patched successfully")
