#!/usr/bin/env python3
# Minimal fake Winboard (xboard protocol) engine, used only to smoke-test
# the Linux port of epd2wb end-to-end (process spawn, pipes, threading,
# event synchronization).
import sys
import threading
import time

analyzing = threading.Event()
stop = threading.Event()

def out(s):
    sys.stdout.write(s + "\n")
    sys.stdout.flush()

def analysis_loop():
    depth = 0
    while not stop.is_set():
        if analyzing.is_set():
            depth += 1
            out(f"{depth} 0 {depth*10} {depth*100} e2e4 e7e5")
            time.sleep(0.2)
        else:
            time.sleep(0.05)

threading.Thread(target=analysis_loop, daemon=True).start()

for line in sys.stdin:
    cmd = line.strip()
    if cmd == "protover 2":
        out("feature ping=1 setboard=1 analyze=1 colors=0 usermove=1 done=1")
    elif cmd.startswith("ping"):
        out("pong" + cmd[4:])
    elif cmd == "analyze":
        analyzing.set()
    elif cmd in ("exit", "force", "new"):
        analyzing.clear()
    elif cmd == "quit":
        stop.set()
        break
    # setboard, xboard, easy, etc: no response needed.
