import tkinter as tk
from tkinter import ttk

class StrokePanel(ttk.Frame):
    def __init__(self, parent, highlight_callback):
        super().__init__(parent)
        self.highlight_callback = highlight_callback
        self.configure(width=120, relief='groove', borderwidth=2)
        
        self.title_label = ttk.Label(self, text="Strokes List", font=("Arial", 14, "bold"))
        self.title_label.pack(pady=(10, 5), fill=tk.X)
        
        ttk.Separator(self, orient='horizontal').pack(fill=tk.X, padx=5, pady=5)
        
        container = ttk.Frame(self)
        container.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        
        scrollbar = ttk.Scrollbar(container)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
        
        self.canvas = tk.Canvas(container, yscrollcommand=scrollbar.set)
        self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        
        scrollbar.config(command=self.canvas.yview)
        
        self.button_frame = ttk.Frame(self.canvas)
        self.canvas.create_window((0, 0), window=self.button_frame, anchor='nw', tags="self.button_frame")

        self.canvas.bind("<Enter>", self._bind_mousewheel)
        self.canvas.bind("<Leave>", self._unbind_mousewheel)

        
        self.button_frame.bind("<Configure>", self._on_frame_configure)
        
        self.strokes = {}
        
        self.info_label = ttk.Label(self, text="Waiting for the LLM...", foreground="gray")
        self.info_label.pack(pady=(5, 10), fill=tk.X)

    def _on_frame_configure(self, event):
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

    def update_strokes(self, stroke_ids, stroke_names=None):
        for widget in self.button_frame.winfo_children():
            widget.destroy()
        self.strokes = {}
        
        if not stroke_ids:
            self.info_label.config(text="Waiting for the LLM...")
            return
        
        self.info_label.config(text=f"total {len(stroke_ids)} strokes")
        
        for i, stroke_id in enumerate(stroke_ids):
            display_name = stroke_names[i] if stroke_names and i < len(stroke_names) else f"stroke {stroke_id}"
            
            btn = ttk.Button(self.button_frame, text=display_name,
                             command=lambda sid=stroke_id: self.highlight_callback(sid))
            btn.pack(fill=tk.X, padx=2, pady=2)
            self.strokes[stroke_id] = btn
        
        self.canvas.configure(scrollregion=self.canvas.bbox("all")) 
    
    def _bind_mousewheel(self, event):
        self.canvas.bind_all("<MouseWheel>", self._on_mousewheel)

    def _unbind_mousewheel(self, event):
        self.canvas.unbind_all("<MouseWheel>")
    
    def _on_mousewheel(self, event):
        self.canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")

class LayoutPanel(ttk.Frame):
    def __init__(self, parent, highlight_callback):
        super().__init__(parent)
        self.highlight_callback = highlight_callback
        self.configure(width=180, relief='groove', borderwidth=2)

        self.title_label = ttk.Label(self, text="Layout List", font=("Arial", 14, "bold"))
        self.title_label.pack(pady=(10, 5), fill=tk.X)

        ttk.Separator(self, orient='horizontal').pack(fill=tk.X, padx=5, pady=5)

        container = ttk.Frame(self)
        container.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)

        scrollbar = ttk.Scrollbar(container)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

        self.canvas = tk.Canvas(container, yscrollcommand=scrollbar.set)
        self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        scrollbar.config(command=self.canvas.yview)

        self.button_frame = ttk.Frame(self.canvas)
        self.canvas.create_window((0, 0), window=self.button_frame, anchor='nw', tags="self.button_frame")

        self.canvas.bind("<Enter>", self._bind_mousewheel)
        self.canvas.bind("<Leave>", self._unbind_mousewheel)
        self.button_frame.bind("<Enter>", self._bind_mousewheel)
        self.button_frame.bind("<Leave>", self._unbind_mousewheel)

        self.button_frame.bind("<Configure>", self._on_frame_configure)

        self.layouts = {}

        self.info_label = ttk.Label(self, text="Waiting for the LLM...", foreground="gray")
        self.info_label.pack(pady=(5, 10), fill=tk.X)

    def _on_frame_configure(self, event):
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

    def update_layouts(self, layout_ids, layout_names=None):
        for widget in self.button_frame.winfo_children():
            widget.destroy()
        self.layouts = {}

        if not layout_ids:
            self.info_label.config(text="Waiting for the LLM...")
            return

        self.info_label.config(text=f"total {len(layout_ids)} layout")

        for i, layout_id in enumerate(layout_ids):
            display_name = layout_names[i] if layout_names and i < len(layout_names) else f"layout {layout_id}"
            btn = ttk.Button(self.button_frame, text=display_name,
                             command=lambda lid=layout_id: self.highlight_callback(lid))
            btn.pack(fill=tk.X, padx=2, pady=2)
            self.layouts[layout_id] = btn

        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

    def _bind_mousewheel(self, event):
        self.canvas.bind_all("<MouseWheel>", self._on_mousewheel)

    def _unbind_mousewheel(self, event):
        self.canvas.unbind_all("<MouseWheel>")

    def _on_mousewheel(self, event):
        self.canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
