import ctypes
from ctypes import wintypes
import tkinter as tk

# מזהים קבועים של ממשק שורת המשימות ב-Windows
CLSID_TaskbarList = "{56FDF344-FD6D-11d0-958A-006097C9A090}"
IID_ITaskbarList3 = "{ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf}"

TBPF_NOPROGRESS = 0x0      # כיבוי
TBPF_INDETERMINATE = 0x1   # גל ירוק רציף

class GUID(ctypes.Structure):
    _fields_ = [
        ("Data1", wintypes.DWORD),
        ("Data2", wintypes.WORD),
        ("Data3", wintypes.WORD),
        ("Data4", ctypes.c_ubyte * 8),
    ]

ole32 = ctypes.windll.ole32
ole32.CLSIDFromString.argtypes = [wintypes.LPCWSTR, ctypes.POINTER(GUID)]
ole32.CoInitialize.argtypes = [wintypes.LPVOID]
ole32.CoCreateInstance.argtypes = [
    ctypes.POINTER(GUID), wintypes.LPVOID, wintypes.DWORD,
    ctypes.POINTER(GUID), ctypes.POINTER(ctypes.c_void_p)
]

def set_taskbar_state(hwnd, state):
    ole32.CoInitialize(None)
    
    clsid = GUID()
    iid = GUID()
    ole32.CLSIDFromString(CLSID_TaskbarList, ctypes.byref(clsid))
    ole32.CLSIDFromString(IID_ITaskbarList3, ctypes.byref(iid))
    
    taskbar_ptr = ctypes.c_void_p()
    hr = ole32.CoCreateInstance(
        ctypes.byref(clsid), None, 1,  # CLSCTX_INPROC_SERVER = 1
        ctypes.byref(iid), ctypes.byref(taskbar_ptr)
    )
    if hr != 0:
        raise OSError(f"CoCreateInstance failed: {hr:#x}")

    vtable = ctypes.cast(
        ctypes.cast(taskbar_ptr, ctypes.POINTER(ctypes.c_void_p)).contents,
        ctypes.POINTER(ctypes.c_void_p)
    )
    
    # HrInit (אינדקס 3)
    ctypes.WINFUNCTYPE(ctypes.c_long, ctypes.c_void_p)(vtable[3])(taskbar_ptr)
    
    # SetProgressState (אינדקס 10)
    ctypes.WINFUNCTYPE(
        ctypes.c_long, ctypes.c_void_p, wintypes.HWND, wintypes.DWORD
    )(vtable[10])(taskbar_ptr, hwnd, state)

    # Release (אינדקס 2)
    ctypes.WINFUNCTYPE(ctypes.c_ulong, ctypes.c_void_p)(vtable[2])(taskbar_ptr)


# --- יצירת ממשק Tkinter ---
root = tk.Tk()
root.title("בדיקת גל ירוק בשורת המשימות")
root.geometry("320x140")

# עדכון החלון כדי לוודא שנוצר לו מזהה (HWND) ב-Windows
root.update()
hwnd = ctypes.windll.user32.GetParent(root.winfo_id()) or root.winfo_id()

def start_wave():
    set_taskbar_state(hwnd, TBPF_INDETERMINATE)
    status_label.config(text="מצב: גל ירוק פעיל", fg="green")

def stop_wave():
    set_taskbar_state(hwnd, TBPF_NOPROGRESS)
    status_label.config(text="מצב: כבוי", fg="black")

status_label = tk.Label(root, text="מצב: כבוי", font=("Arial", 11))
status_label.pack(pady=15)

btn_frame = tk.Frame(root)
btn_frame.pack(pady=5)

tk.Button(btn_frame, text="הפעל גל ירוק", command=start_wave, width=14).pack(side=tk.LEFT, padx=5)
tk.Button(btn_frame, text="כבה", command=stop_wave, width=10).pack(side=tk.LEFT, padx=5)

# הפעלה אוטומטית עם עליית החלון (אופציונלי)
start_wave()

root.mainloop()