"""Shared LUI bootstrap and imports."""

import os
import sys
from pathlib import Path

import panda3d.core as p3d
from panda3d.core import NodePath, CardMaker
from imgui_bundle import imgui, imgui_ctx

UI_DIR = Path(__file__).resolve().parents[1]
if str(UI_DIR) not in sys.path:
    sys.path.insert(0, str(UI_DIR))

BUILTIN_DIR = UI_DIR / "Builtin"
if str(BUILTIN_DIR) not in sys.path:
    sys.path.insert(0, str(BUILTIN_DIR))

import panda3d
panda_dir = os.path.dirname(panda3d.__file__)
if hasattr(os, "add_dll_directory"):
    try:
        os.add_dll_directory(panda_dir)
        os.add_dll_directory(str(UI_DIR))
    except Exception as e:
        print(f"Warning: Failed to add DLL directory: {e}")

try:
    import lui
    panda3d.lui = lui
    sys.modules["panda3d.lui"] = lui

    from Builtin.LUIRegion import LUIRegion
    from Builtin.LUIInputHandler import LUIInputHandler
    from Builtin.LUIButton import LUIButton
    from Builtin.LUILabel import LUILabel
    from Builtin.LUIInputField import LUIInputField
    from Builtin.LUISlider import LUISlider
    from Builtin.LUIFrame import LUIFrame
    from Builtin.LUISkin import LUIDefaultSkin
    from Builtin.LUISprite import LUISprite
    from Builtin.LUIObject import LUIObject
    from Builtin.LUICheckbox import LUICheckbox
    from Builtin.LUIProgressbar import LUIProgressbar
    from Builtin.LUISelectbox import LUISelectbox
    from Builtin.LUIScrollableRegion import LUIScrollableRegion
    from Builtin.LUITabbedFrame import LUITabbedFrame
    from Builtin.LUIVerticalLayout import LUIVerticalLayout
    from Builtin.LUIHorizontalLayout import LUIHorizontalLayout
except ImportError as e:
    print(f"Error: Failed to import LUI: {e}")
    lui = None
    LUIRegion = None
    LUIInputHandler = None
    LUIButton = None
    LUILabel = None
    LUIInputField = None
    LUISlider = None
    LUIFrame = None
    LUIDefaultSkin = None
    LUISprite = None
    LUIObject = None
    LUICheckbox = None
    LUIProgressbar = None
    LUISelectbox = None
    LUIScrollableRegion = None
    LUITabbedFrame = None
    LUIVerticalLayout = None
    LUIHorizontalLayout = None
