import tkinter
import time

# === INIT ===
root = tkinter.Tk()
root.title("Tkinter Test Lab ULTIMATE (ASCII)")
root.geometry("900x720")
root.configure(bg="black")

# === CANVAS ===
canvas = tkinter.Canvas(root, width=900, height=520, bg="dark green", highlightthickness=0)
canvas.pack()

# === LOG AREA ===
log = tkinter.Text(root, height=6, bg="black", fg="green", font=("Courier", 10))
log.pack(fill="x")

# === CONTROL PANEL ===
control_frame = tkinter.Frame(root, bg="black")
control_frame.pack(pady=5)

draw_mode = [True]  # Use list for mutability
start_time = time.time()

def log_event(message):
    log.insert("end", message + "\n")
    log.see("end")

# === GLOBAL STATS ===
stats = {
    "left_clicks": 0,
    "right_clicks": 0,
    "middle_clicks": 0,
    "double_clicks": 0,
    "keys_pressed": 0,
    "drag_paths": 0,
    "elapsed_seconds": 0
}

# === DRAW HEADER ===
def draw_header():
    canvas.create_text(450, 30, text="+--------------------------------------------------+",
                       fill="white", font=("Courier", 16))
    canvas.create_text(450, 60, text="|        TKINTER TEST LAB ULTIMATE (ASCII)        |",
                       fill="white", font=("Courier", 16))
    canvas.create_text(450, 90, text="+--------------------------------------------------+",
                       fill="white", font=("Courier", 16))

def draw_test_zone():
    canvas.create_rectangle(60, 120, 840, 480, outline="white", width=2)
    canvas.create_text(450, 125, text="[ DRAW / CLICK / DRAG HERE ]", fill="white", font=("Courier", 12))

def draw_stats():
    canvas.delete("stats")
    canvas.create_text(150, 510, text="Left Clicks: {}".format(stats["left_clicks"]),
                       fill="white", font=("Courier", 10), anchor="w", tags="stats")
    canvas.create_text(350, 510, text="Right Clicks: {}".format(stats["right_clicks"]),
                       fill="white", font=("Courier", 10), anchor="w", tags="stats")
    canvas.create_text(550, 510, text="Middle Clicks: {}".format(stats["middle_clicks"]),
                       fill="white", font=("Courier", 10), anchor="w", tags="stats")
    canvas.create_text(150, 530, text="Double Clicks: {}".format(stats["double_clicks"]),
                       fill="white", font=("Courier", 10), anchor="w", tags="stats")
    canvas.create_text(350, 530, text="Keys Pressed: {}".format(stats["keys_pressed"]),
                       fill="white", font=("Courier", 10), anchor="w", tags="stats")
    canvas.create_text(550, 530, text="Drag Paths: {}".format(stats["drag_paths"]),
                       fill="white", font=("Courier", 10), anchor="w", tags="stats")
    canvas.create_text(750, 530, text="Time: {}s".format(stats["elapsed_seconds"]),
                       fill="white", font=("Courier", 10), anchor="e", tags="stats")

# === TIMER ===
def update_timer():
    stats["elapsed_seconds"] = int(time.time() - start_time)
    draw_stats()
    root.after(1000, update_timer)

# === EVENTS ===
def on_click(event):
    stats["left_clicks"] += 1
    canvas.create_text(event.x, event.y, text="*", fill="yellow", font=("Courier", 14))
    log_event("Left Click at ({}, {})".format(event.x, event.y))
    draw_stats()

def on_right_click(event):
    stats["right_clicks"] += 1
    canvas.create_oval(event.x - 5, event.y - 5, event.x + 5, event.y + 5, fill="red", outline="")
    log_event("Right Click at ({}, {})".format(event.x, event.y))
    draw_stats()

def on_middle_click(event):
    stats["middle_clicks"] += 1
    canvas.create_rectangle(event.x - 5, event.y - 5, event.x + 5, event.y + 5, fill="blue", outline="")
    log_event("Middle Click at ({}, {})".format(event.x, event.y))
    draw_stats()

def on_double_click(event):
    stats["double_clicks"] += 1
    canvas.create_text(event.x, event.y, text="X", fill="white", font=("Courier", 16))
    log_event("Double Click at ({}, {})".format(event.x, event.y))
    draw_stats()

def on_mouse_move(event):
    canvas.delete("mouse_label")
    canvas.create_text(880, 500, text="Mouse: ({}, {})".format(event.x, event.y),
                       fill="white", font=("Courier", 10), anchor="se", tags="mouse_label")

def on_key(event):
    stats["keys_pressed"] += 1
    log_event("Key Pressed: {}".format(event.keysym))
    draw_stats()

def on_key_release(event):
    log_event("Key Released: {}".format(event.keysym))

# === DRAGGING ===
drawing = False
last_x, last_y = None, None

def on_drag_start(event):
    global drawing, last_x, last_y
    drawing = True
    last_x, last_y = event.x, event.y

def on_drag(event):
    global last_x, last_y
    if drawing and draw_mode[0]:
        canvas.create_line(last_x, last_y, event.x, event.y, fill="green", width=2)
        last_x, last_y = event.x, event.y
        stats["drag_paths"] += 1
        draw_stats()

def on_drag_end(event):
    global drawing
    drawing = False
    log_event("Drag Ended at ({}, {})".format(event.x, event.y))

# === BUTTON ACTIONS ===
def clear_canvas():
    canvas.delete("all")
    draw_header()
    draw_test_zone()
    draw_stats()
    log_event("Canvas cleared.")

def toggle_draw():
    draw_mode[0] = not draw_mode[0]
    if draw_mode[0]:
        toggle_btn.config(text="Draw Mode: ON", bg="green")
        log_event("Draw mode ENABLED.")
    else:
        toggle_btn.config(text="Draw Mode: OFF", bg="gray")
        log_event("Draw mode DISABLED.")

def save_canvas():
    try:
        canvas.postscript(file="canvas_output.ps")
        log_event("Canvas saved as canvas_output.ps")
    except:
        log_event("ERROR saving canvas!")

# === BUTTONS ===
clear_btn = tkinter.Button(control_frame, text="Clear Canvas", command=clear_canvas,
                           font=("Courier", 10), width=20, bg="white")
clear_btn.grid(row=0, column=0, padx=5)

toggle_btn = tkinter.Button(control_frame, text="Draw Mode: ON", command=toggle_draw,
                            font=("Courier", 10), width=20, bg="green")
toggle_btn.grid(row=0, column=1, padx=5)

save_btn = tkinter.Button(control_frame, text="Save Canvas", command=save_canvas,
                          font=("Courier", 10), width=20, bg="white")
save_btn.grid(row=0, column=2, padx=5)

# === BINDINGS ===
canvas.bind("<Button-1>", on_click)
canvas.bind("<Button-2>", on_middle_click)
canvas.bind("<Button-3>", on_right_click)
canvas.bind("<Double-Button-1>", on_double_click)
canvas.bind("<Motion>", on_mouse_move)

canvas.bind("<ButtonPress-1>", on_drag_start)
canvas.bind("<B1-Motion>", on_drag)
canvas.bind("<ButtonRelease-1>", on_drag_end)

root.bind("<KeyPress>", on_key)
root.bind("<KeyRelease>", on_key_release)

# === INIT ===
draw_header()
draw_test_zone()
draw_stats()
log_event("Welcome to the Tkinter Test Lab ULTIMATE!")
log_event("Draw, click, type, or interact with the canvas.")
update_timer()

root.mainloop()  make it so it detects left clicks