import tkinter as tk
from tkinter import ttk
import time
import logging

class ToolTip:
    """
    Creates a floating tooltip for any Tkinter widget.
    """
    def __init__(self, widget, text):
        self.widget = widget
        self.text = text
        self.tip_window = None
        self.widget.bind("<Enter>", self.show_tip)
        self.widget.bind("<Leave>", self.hide_tip)

    def show_tip(self, event=None):
        if self.tip_window or not self.text:
            return
        x, y, _cx, cy = self.widget.bbox("insert")
        x = x + self.widget.winfo_rootx() + 25
        y = y + cy + self.widget.winfo_rooty() + 25
        self.tip_window = tw = tk.Toplevel(self.widget)
        tw.wm_overrideredirect(True)
        tw.wm_geometry(f"+{x}+{y}")
        label = tk.Label(
            tw, text=self.text, justify=tk.LEFT,
            background="#ffffe0", relief=tk.SOLID, borderwidth=1,
            font=("Helvetica", 9)
        )
        label.pack(ipadx=1)

    def hide_tip(self, event=None):
        tw = self.tip_window
        self.tip_window = None
        if tw:
            tw.destroy()

    def update_text(self, new_text):
        self.text = new_text

class BaseCommandFrame(tk.Frame):
    """
    Base class for all command widgets (Checkbox, ComboBox, etc.).
    Handles common properties like tellback thresholds and logging.
    """
    def __init__(self, parent, command_info, column_widths=None, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.info = command_info
        self.logger = logging.getLogger('PyMsc')
        self.column_widths = column_widths or [20, 12, 12]
        
        # Tellback logic parameters
        self.start_time = 0
        self.threshold_ms = 1000
        self.update_interval_ms = 50
        
        # Script manager integration (will be injected later)
        from pymsc.gui.script_manager import get_script_manager
        self.script_manager = get_script_manager()