
import sys

def insert_method(file_path):
    with open(file_path, 'r', encoding='utf-8', newline='') as f:
        lines = f.readlines()

    target_line = -1
    for i, line in enumerate(lines):
        if 'def _render_heavy_text' in line:
            # Find the end of this method
            for j in range(i + 1, len(lines)):
                if lines[j].startswith(' def '):
                    target_line = j
                    break
            if target_line != -1:
                break

    if target_line == -1:
        print("Could not find insertion point")
        sys.exit(1)

    new_method = [
        '\n',
        ' def _render_selectable_label(self, label: str, value: str, width: float = 0.0, multiline: bool = False, height: float = 0.0, color: Optional[imgui.ImVec4] = None) -> None:\n',
        '  imgui.push_id(label + str(hash(value)))\n',
        '  pops = 2\n',
        '  imgui.push_style_color(imgui.Col_.frame_bg, vec4(0, 0, 0, 0))\n',
        '  imgui.push_style_color(imgui.Col_.border, vec4(0, 0, 0, 0))\n',
        '  if color:\n',
        '   imgui.push_style_color(imgui.Col_.text, color)\n',
        '   pops += 1\n',
        '  if multiline:\n',
        '   imgui.input_text_multiline("##" + label, value, imgui.ImVec2(width, height), imgui.InputTextFlags_.read_only)\n',
        '  else:\n',
        '   if width > 0: imgui.set_next_item_width(width)\n',
        '   imgui.input_text("##" + label, value, imgui.InputTextFlags_.read_only)\n',
        '  imgui.pop_style_color(pops)\n',
        '  imgui.pop_id()\n'
    ]

    lines[target_line:target_line] = new_method

    with open(file_path, 'w', encoding='utf-8', newline='') as f:
        f.writelines(lines)
    print("Successfully inserted _render_selectable_label")

if __name__ == "__main__":
    insert_method("src/gui_2.py")
