import customtkinter as ctk

class Tooltip:
    def __init__(self, widget, text, fg_color=("gray70", "gray20"), text_color=("black", "white"), font=None, default_font=None):
        self.widget = widget
        self.text = text
        self.fg_color = fg_color
        self.text_color = text_color
        self.font = font
        self.default_font = default_font if default_font is not None else ctk.CTkFont(size=12) # Use a sensible default
        self.tooltip_window = None
        self.widget.bind("<Enter>", self.show_tooltip)
        self.widget.bind("<Leave>", self.hide_tooltip)
        self.widget.bind("<ButtonPress>", self.hide_tooltip) # Hide tooltip if button is pressed

    def show_tooltip(self, event=None):
        if self.tooltip_window or not self.text:
            return

        x = self.widget.winfo_rootx() + self.widget.winfo_width()
        y = self.widget.winfo_rooty() + self.widget.winfo_height() // 2

        self.tooltip_window = ctk.CTkToplevel(self.widget)
        self.tooltip_window.wm_overrideredirect(True) # Remove window decorations
        self.tooltip_window.wm_geometry(f"+{x}+{y}")

        label = ctk.CTkLabel(
            self.tooltip_window,
            text=self.text,
            fg_color=self.fg_color,
            text_color=self.text_color,
            font=self.font if self.font is not None else self.default_font,
            corner_radius=5,
            padx=5,
            pady=3
        )
        label.pack(ipadx=1, ipady=1)

    def hide_tooltip(self, event=None):
        if self.tooltip_window:
            self.tooltip_window.destroy()
        self.tooltip_window = None
