from tkinter import messagebox
import tkinter as tk

_used_shortcuts = {}
_enabled = True


# =========================
# CORE SAFETY FUNCTION
# =========================

def _safe_event(root, event):
    widget = root.focus_get()
    if widget:
        try:
            widget.event_generate(event)
        except:
            pass


# =========================
# BASIC SHORTCUT SYSTEM
# =========================

def add_shortcut(root, key, event):
    if key in _used_shortcuts:
        messagebox.showwarning(
            "Hotkey Warning",
            f"{key} already used for { _used_shortcuts[key] }"
        )
        return False

    def handler(e):
        if not _enabled:
            return
        _safe_event(root, event)

    root.bind_all(key, handler)
    _used_shortcuts[key] = event
    return True


def remove_shortcut(root, key):
    if key in _used_shortcuts:
        root.unbind_all(key)
        del _used_shortcuts[key]
        return True
    return False


def disable_all():
    global _enabled
    _enabled = False


def enable_all():
    global _enabled
    _enabled = True


def reset_all(root):
    for key in list(_used_shortcuts.keys()):
        root.unbind_all(key)
    _used_shortcuts.clear()


def list_shortcuts():
    return _used_shortcuts.copy()
def copy(root): add_shortcut(root, "<Control-c>", "<<Copy>>")
def paste(root): add_shortcut(root, "<Control-v>", "<<Paste>>")
def cut(root): add_shortcut(root, "<Control-x>", "<<Cut>>")
def select_all(root): add_shortcut(root, "<Control-a>", "<<SelectAll>>")
def undo(root): add_shortcut(root, "<Control-z>", "<<Undo>>")
def redo(root): add_shortcut(root, "<Control-y>", "<<Redo>>")
def find(root): add_shortcut(root, "<Control-f>", "<<Find>>")
def replace(root): add_shortcut(root, "<Control-h>", "<<Replace>>")
def goto(root): add_shortcut(root, "<Control-g>", "<<Goto>>")
def duplicate(root): add_shortcut(root, "<Control-d>", "<<Duplicate>>")
def select_line(root): add_shortcut(root, "<Control-l>", "<<SelectLine>>")
def delete_word(root): add_shortcut(root, "<Control-BackSpace>", "<<DeleteWord>>")
def delete_line(root): add_shortcut(root, "<Control-Delete>", "<<DeleteLine>>")
def cut_alt(root): add_shortcut(root, "<Shift-Delete>", "<<Cut>>")
def home(root): add_shortcut(root, "<Home>", "<<Home>>")
def end(root): add_shortcut(root, "<End>", "<<End>>")
def word_left(root): add_shortcut(root, "<Control-Left>", "<<WordLeft>>")
def word_right(root): add_shortcut(root, "<Control-Right>", "<<WordRight>>")
def page_up(root): add_shortcut(root, "<Control-Up>", "<<PageUp>>")
def page_down(root): add_shortcut(root, "<Control-Down>", "<<PageDown>>")
def select_left(root): add_shortcut(root, "<Shift-Left>", "<<SelectLeft>>")
def select_right(root): add_shortcut(root, "<Shift-Right>", "<<SelectRight>>")
def select_up(root): add_shortcut(root, "<Shift-Up>", "<<SelectUp>>")
def select_down(root): add_shortcut(root, "<Shift-Down>", "<<SelectDown>>")
def select_word_left(root): add_shortcut(root, "<Control-Shift-Left>", "<<SelectWordLeft>>")
def select_word_right(root): add_shortcut(root, "<Control-Shift-Right>", "<<SelectWordRight>>")
def save(root): add_shortcut(root, "<Control-s>", "<<Save>>")
def open_file(root): add_shortcut(root, "<Control-o>", "<<Open>>")
def new_file(root): add_shortcut(root, "<Control-n>", "<<New>>")
def print_file(root): add_shortcut(root, "<Control-p>", "<<Print>>")
def refresh(root): add_shortcut(root, "<F5>", "<<Refresh>>")
def use_all(root):
    copy(root)
    paste(root)
    cut(root)
    select_all(root)
    undo(root)
    redo(root)
    find(root)
    replace(root)
    goto(root)
    duplicate(root)
    select_line(root)
    delete_word(root)
    delete_line(root)
    cut_alt(root)
    home(root)
    end(root)
    word_left(root)
    word_right(root)
    page_up(root)
    page_down(root)
    select_left(root)
    select_right(root)
    select_up(root)
    select_down(root)
    select_word_left(root)
    select_word_right(root)
    save(root)
    open_file(root)
    new_file(root)
    print_file(root)
    refresh(root)
def show_all_shortcuts():
    shortcuts = list_shortcuts()
    if not shortcuts:
        messagebox.showinfo("Hotkeys", "No shortcuts defined.")
        return

    msg = "Current Shortcuts:\n\n"
    for key, event in shortcuts.items():
        msg += f"{key} -> {event}\n"

    messagebox.showinfo("Hotkeys", msg)
def shortcuts_popup_list(root):
    shortcuts = list_shortcuts()
    if not shortcuts:
        messagebox.showinfo("Hotkeys", "No shortcuts defined.")
        return

    msg = "Current Shortcuts:\n\n"
    for key, event in shortcuts.items():
        msg += f"{key} -> {event}\n"

    
    shortcuts_list = tk.Listbox(root)


    for key, event in shortcuts.items():
        shortcuts_list.insert(tk.END, f"{key} -> {event}")
    root.bind_all("<Button-3>", lambda e: shortcuts_list.tk_popup(e.x_root, e.y_root))

class ToolTip:
    def __init__(self, widget, text):
        self.widget = widget
        self.text = text
        self.tip = None

        widget.bind("<Enter>", self.show)
        widget.bind("<Leave>", self.hide)

    def show(self, event):
        if self.tip:
            return

        self.tip = tk.Toplevel(self.widget)
        self.tip.wm_overrideredirect(True)
        self.tip.geometry(f"+{event.x_root+10}+{event.y_root+10}")

        tk.Label(
            self.tip,
            text=self.text,
            bg="black",
            fg="white",
            padx=5,
            pady=3
        ).pack()

    def hide(self, event):
        if self.tip:
            self.tip.destroy()
            self.tip = None