import tkinter as tk
import random
import time
import sys

# Constants
BRIGHT_GREEN = "#00FF00"
DIM_GREEN = "#007F00"
FLASH_COLOR = "#00FF00"
BACKGROUND_COLOR = "black"
MAX_LINES = 70
DELAY = 100  # milliseconds between line stages

class TektronixLinesScreensaver:
    def __init__(self, root):
        self.root = root
        self.root.title("Tektronix 4052 Lines! Screensaver")
        self.root.configure(bg=BACKGROUND_COLOR)
        self.root.attributes("-fullscreen", True)
        self.root.config(cursor="none")  # Hide the cursor

        self.screen_width = self.root.winfo_screenwidth()
        self.screen_height = self.root.winfo_screenheight()

        self.canvas = tk.Canvas(root, width=self.screen_width, height=self.screen_height,
                                bg=BACKGROUND_COLOR, highlightthickness=0)
        self.canvas.pack(fill="both", expand=True)

        # Exit on key press or mouse click
        self.root.bind("<Key>", lambda e: self.root.destroy())
        self.root.bind("<Button>", lambda e: self.root.destroy())

        # Mouse movement detection after short delay
        self.mouse_check_delay = 1000  # ms
        self.mouse_check_enabled = False
        self.last_mouse_pos = self.root.winfo_pointerxy()
        self.root.after(self.mouse_check_delay, self.enable_mouse_check)
        self.root.bind("<Motion>", self.on_mouse_move)

        max_step_x = max(1, self.screen_width // 25)
        max_step_y = max(1, self.screen_height // 25)

        self.x = random.randint(0, self.screen_width)
        self.y = random.randint(0, self.screen_height)
        self.x2 = random.randint(0, self.screen_width)
        self.y2 = random.randint(0, self.screen_height)
        self.dx = random.randint(max_step_x // 2, max_step_x)
        self.dy = random.randint(max_step_y // 2, max_step_y)
        self.dx2 = random.randint(max_step_x // 2, max_step_x)
        self.dy2 = random.randint(max_step_y // 2, max_step_y)

        self.line_count = 0
        self.lines = []
        self.draw_lines()

    def enable_mouse_check(self):
        self.mouse_check_enabled = True
        self.last_mouse_pos = self.root.winfo_pointerxy()

    def on_mouse_move(self, event):
        if not self.mouse_check_enabled:
            return
        current_pos = self.root.winfo_pointerxy()
        if current_pos != self.last_mouse_pos:
            self.root.destroy()

    def draw_lines(self):
        line = self.canvas.create_line(self.x, self.y, self.x2, self.y2, fill=BRIGHT_GREEN, width=2)
        self.lines.append(line)
        self.root.after(DELAY, lambda l=line: self.dim_line(l))

        self.x, self.dx = self.bounce(self.x, self.dx, self.screen_width)
        self.y, self.dy = self.bounce(self.y, self.dy, self.screen_height)
        self.x2, self.dx2 = self.bounce(self.x2, self.dx2, self.screen_width)
        self.y2, self.dy2 = self.bounce(self.y2, self.dy2, self.screen_height)

        self.line_count += 1

        if self.line_count >= MAX_LINES:
            self.root.after(DELAY, self.flash_and_clear)
            self.line_count = 0
        else:
            self.root.after(DELAY, self.draw_lines)

    def bounce(self, pos, delta, limit):
        if pos + delta >= limit or pos + delta < 0:
            delta = -delta
        return pos + delta, delta

    def dim_line(self, line):
        self.canvas.itemconfig(line, fill=DIM_GREEN, width=1)

    def flash_and_clear(self):
        self.canvas.delete("all")
        self.canvas.configure(bg=FLASH_COLOR)
        self.root.update()
        time.sleep(0.25)
        self.canvas.configure(bg=BACKGROUND_COLOR)
        self.lines.clear()
        self.draw_lines()

if __name__ == "__main__":
    def run_screensaver():
        root = tk.Tk()
        app = TektronixLinesScreensaver(root)
        root.mainloop()

    if len(sys.argv) > 1:
        arg = sys.argv[1].lower().strip()
        if arg.startswith("/c"):
            print("No configuration available.")
        elif arg.startswith("/p"):
            print("Preview not supported.")
        elif arg.startswith("/s"):
            run_screensaver()
        else:
            run_screensaver()
    else:
        run_screensaver()
