#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Remove the orphaned old DisplayOverlay tail (lines 3652-3687) from UI.lua"""
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()

total = len(lines)
print(f"Total lines: {total}")

# Find the orphaned block: look for "o.introAnim:Stop()" which is outside any function
# It should be right after the new DisplayOverlay's closing "end"
orphan_start = None
orphan_end   = None

for i, line in enumerate(lines):
    stripped = line.strip()
    # The orphan starts with the GDL_ScaleLerp call that's now outside the function
    if stripped.startswith("o.introAnim:Stop()") and orphan_start is None:
        orphan_start = i
    # It ends right before "function UI:CreateOverlay()"
    if stripped.startswith("function UI:CreateOverlay()") and orphan_start is not None:
        orphan_end = i
        break

if orphan_start is None or orphan_end is None:
    # Fallback: search by line content more broadly
    for i, line in enumerate(lines):
        if "GDL_ScaleLerp(o," in line and orphan_start is None:
            # Check if this is outside a function (no indent = likely top level)
            orphan_start = i
        if "function UI:CreateOverlay()" in line and orphan_start is not None:
            orphan_end = i
            break

if orphan_start is None or orphan_end is None:
    print(f"ERROR: could not locate orphan block. orphan_start={orphan_start}, orphan_end={orphan_end}")
    # Dump suspect area
    for i in range(3648, min(3695, total)):
        print(f"{i+1}| {lines[i]}", end="")
else:
    print(f"Removing orphaned lines {orphan_start+1} to {orphan_end} (exclusive)")
    for i in range(orphan_start, orphan_end):
        print(f"  DEL {i+1}: {lines[i].rstrip()}")
    lines = lines[:orphan_start] + lines[orphan_end:]
    with open(path, 'w', encoding='utf-8') as f:
        f.writelines(lines)
    print(f"Saved. New total: {len(lines)} lines")
