from pathlib import Path
import importlib.util
import sys
import tkinter as tk


APP = Path(__file__).resolve().parent
V38_PATH = APP / "racecore_studio_v38.py"
if str(APP) not in sys.path:
    sys.path.insert(0, str(APP))

spec = importlib.util.spec_from_file_location("racecore_v38_for_v39", V38_PATH)
v38 = importlib.util.module_from_spec(spec)
spec.loader.exec_module(v38)


class StudioV39(v38.StudioV38):
    """Vaste canvaswerkbalk die nooit meer over het ontwerp heen ligt."""

    CANVAS_TOOLBAR_HEIGHT = 50

    def __init__(self):
        super().__init__()
        self.title("RaceCore Livery Studio v3.9 - Clear Canvas")

    def build_layout(self):
        super().build_layout()
        header = self.winfo_children()[0]
        for child in header.winfo_children():
            if isinstance(child, tk.Label) and "Livery Studio" in child.cget("text"):
                child.configure(text="Livery Studio  v3.9")

        viewer = self.preview_label.master
        body = viewer.master

        # v3.3 en v3.4 plaatsten deze knoppen met `place` boven op de UV.
        # De oude widgets verdwijnen volledig; alleen de functies en variabelen
        # worden opnieuw gebruikt in de vaste balk buiten het canvas.
        old_toolbox_button = self.toolbox_button
        old_focus_tools = self.focus_tools
        old_toolbox_button.destroy()
        old_focus_tools.destroy()

        toolbar = tk.Frame(
            body, bg="#0B1220", height=self.CANVAS_TOOLBAR_HEIGHT,
            bd=1, relief="solid",
        )
        toolbar.pack(side="top", fill="x", before=viewer, pady=(0, 8))
        toolbar.pack_propagate(False)
        self.canvas_toolbar = toolbar

        add_zone = tk.Frame(toolbar, bg="#0B1220")
        add_zone.pack(side="left", fill="y", padx=(9, 0))
        self.toolbox_button = tk.Button(
            add_zone, text="+ TOEVOEGEN", command=self.toggle_design_toolbox,
            font=("Segoe UI", 9, "bold"), fg="#08152C", bg="#67E8F9",
            activeforeground="#08152C", activebackground="#A5F3FC",
            relief="flat", padx=13, pady=6, cursor="hand2",
        )
        self.toolbox_button.pack(side="left", pady=7)
        tk.Label(
            add_zone, text="ONTWERPGEREEDSCHAP",
            font=("Segoe UI", 8, "bold"), fg="#64748B", bg="#0B1220",
        ).pack(side="left", padx=(12, 0), pady=7)

        view_zone = tk.Frame(toolbar, bg="#0B1220")
        view_zone.pack(side="right", fill="y", padx=(0, 9))
        self.focus_tools = view_zone

        self.design_button = self._mode_button(view_zone, "ONTWERP", "design")
        self.lmu_button = self._mode_button(view_zone, "LMU", "lmu")
        self._separator(view_zone)
        self.uv_toggle = tk.Checkbutton(
            view_zone, text="UV", variable=self.focus_show_uv,
            command=self.focus_overlay_changed, indicatoron=False,
            font=("Segoe UI", 8, "bold"), fg="#E5E7EB", bg="#252A35",
            activeforeground="#FFFFFF", activebackground="#374151",
            selectcolor="#6D28D9", relief="flat", padx=8, pady=5,
        )
        self.uv_toggle.pack(side="left", padx=(3, 0), pady=7)
        self.sticker_toggle = tk.Checkbutton(
            view_zone, text="STICKERS", variable=self.focus_show_stickers,
            command=self.focus_overlay_changed, indicatoron=False,
            font=("Segoe UI", 8, "bold"), fg="#E5E7EB", bg="#252A35",
            activeforeground="#FFFFFF", activebackground="#374151",
            selectcolor="#6D28D9", relief="flat", padx=8, pady=5,
        )
        self.sticker_toggle.pack(side="left", padx=(3, 0), pady=7)
        self._separator(view_zone)
        self._focus_button(view_zone, "PASSEND", self.focus_fit)
        self._focus_button(
            view_zone, "−", lambda: self.focus_zoom_step(1 / 1.25), width=2
        )
        tk.Label(
            view_zone, textvariable=self.focus_zoom_text,
            font=("Segoe UI", 8, "bold"), fg="#C4B5FD", bg="#0B1220",
            width=5,
        ).pack(side="left", padx=2)
        self._focus_button(
            view_zone, "+", lambda: self.focus_zoom_step(1.25), width=2
        )
        self._refresh_mode_buttons()


if __name__ == "__main__":
    StudioV39().mainloop()
