#!/usr/bin/env python3
"""Sweep candidate commands after a known RCP-TX7 discovery response.

Current working sequence:

    primer -> discovery query -> RCP response -> candidate next-stage command

By default this uses:

    primer: 00 00 00 00 80 DA
    query:  00 00 B5 00 80 6F

Use --prompt-power-cycle for clean testing because the RCP appears to latch after
one selected query response per powered session.
"""

from __future__ import annotations

import argparse
import datetime as dt
import sys
import time

try:
    import serial
except ImportError:
    print(
        "Missing dependency: pyserial\n"
        "Install it with: python -m pip install pyserial",
        file=sys.stderr,
    )
    raise SystemExit(2)


HEARTBEAT = bytes.fromhex("00 00 00 00 80 DA")


def parse_byte(text: str) -> int:
    value = int(text, 0)
    if not 0 <= value <= 0xFF:
        raise argparse.ArgumentTypeError(f"must be a byte: {text}")
    return value


def parse_byte_set(text: str) -> list[int]:
    values: list[int] = []
    for part in text.replace(",", " ").split():
        if "-" in part:
            start_text, end_text = part.split("-", 1)
            start = parse_byte(start_text)
            end = parse_byte(end_text)
            if end < start:
                raise argparse.ArgumentTypeError(f"bad range: {part}")
            values.extend(range(start, end + 1))
        else:
            values.append(parse_byte(part))
    if not values:
        raise argparse.ArgumentTypeError("empty byte set")
    return values


def build_frame(prefix1: int, prefix2: int, command: int, state: int, value: int) -> bytes:
    body = bytes([prefix1, prefix2, command, state, value])
    checksum = 0x5A
    for byte in body:
        checksum ^= byte
    return body + bytes([checksum])


def hex_preview(data: bytes) -> str:
    return " ".join(f"{byte:02X}" for byte in data)


def make_logger(path: str | None):
    log_file = open(path, "a", encoding="utf-8") if path else None

    def emit(line: str) -> None:
        print(line)
        if log_file:
            log_file.write(line + "\n")
            log_file.flush()

    return emit, log_file


def heartbeat_offset(data: bytes) -> int | None:
    if not data:
        return 0
    for offset in range(len(HEARTBEAT)):
        if all(byte == HEARTBEAT[(offset + index) % len(HEARTBEAT)] for index, byte in enumerate(data)):
            return offset
    return None


def first_mismatch(data: bytes, offset: int) -> tuple[int, int, int] | None:
    for index, byte in enumerate(data):
        expected = HEARTBEAT[(offset + index) % len(HEARTBEAT)]
        if byte != expected:
            return index, byte, expected
    return None


def classify_rx(data: bytes) -> tuple[bool, str]:
    if not data:
        return False, "no RX bytes"

    offset = heartbeat_offset(data)
    if offset is not None:
        full = len(data) // len(HEARTBEAT)
        extra = len(data) % len(HEARTBEAT)
        return False, f"heartbeat-compatible RX: {len(data)} bytes, offset {offset}, {full} frames + {extra} bytes"

    best_offset = min(
        range(len(HEARTBEAT)),
        key=lambda candidate: sum(
            byte != HEARTBEAT[(candidate + index) % len(HEARTBEAT)]
            for index, byte in enumerate(data)
        ),
    )
    mismatch = first_mismatch(data, best_offset)
    if mismatch is None:
        return False, "heartbeat-compatible RX"

    index, byte, expected = mismatch
    return (
        True,
        f"ANOMALY {len(data)} RX bytes; first mismatch at byte {index}: "
        f"got {byte:02X}, heartbeat offset {best_offset} expected {expected:02X}",
    )


def read_window(ser: serial.Serial, duration: float) -> bytes:
    stop_at = time.monotonic() + duration
    data = bytearray()
    while time.monotonic() < stop_at:
        chunk = ser.read(128)
        if chunk:
            data.extend(chunk)
    return bytes(data)


def emit_rx(emit, label: str, data: bytes) -> bool:
    is_anomaly, note = classify_rx(data)
    emit(f"{label} {note}")
    if is_anomaly:
        emit(f"{label} raw {hex_preview(data)}")
    return is_anomaly


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Sweep next-stage commands after a known discovery query."
    )
    parser.add_argument("--port", required=True, help="serial port, for example COM5")
    parser.add_argument("--baud", type=int, default=38400)
    parser.add_argument("--prefix1", type=parse_byte, default=0x00)
    parser.add_argument("--prefix2", type=parse_byte, default=0x00)
    parser.add_argument("--primer-command", type=parse_byte, default=0x00)
    parser.add_argument("--primer-state", type=parse_byte, default=0x00)
    parser.add_argument("--primer-value", type=parse_byte, default=0x80)
    parser.add_argument("--query-command", type=parse_byte, default=0xB5)
    parser.add_argument("--query-state", type=parse_byte, default=0x00)
    parser.add_argument("--query-value", type=parse_byte, default=0x80)
    parser.add_argument("--candidates", type=parse_byte_set, required=True)
    parser.add_argument("--candidate-state", type=parse_byte, default=0x00)
    parser.add_argument("--candidate-value", type=parse_byte, default=0x80)
    parser.add_argument("--settle", type=float, default=3.0)
    parser.add_argument("--between", type=float, default=0.8)
    parser.add_argument("--after-query", type=float, default=1.2)
    parser.add_argument("--after-candidate", type=float, default=2.0)
    parser.add_argument("--timeout", type=float, default=0.03)
    parser.add_argument("--log", help="append sweep log to this file")
    parser.add_argument("--dry-run", action="store_true")
    parser.add_argument("--prompt-power-cycle", action="store_true")
    parser.add_argument("--prompt-screen", action="store_true")
    parser.add_argument("--stop-on-candidate-anomaly", action="store_true")
    return parser.parse_args()


def main() -> int:
    args = parse_args()
    primer = build_frame(
        args.prefix1,
        args.prefix2,
        args.primer_command,
        args.primer_state,
        args.primer_value,
    )
    query = build_frame(
        args.prefix1,
        args.prefix2,
        args.query_command,
        args.query_state,
        args.query_value,
    )
    candidates = [
        (
            command,
            build_frame(
                args.prefix1,
                args.prefix2,
                command,
                args.candidate_state,
                args.candidate_value,
            ),
        )
        for command in args.candidates
    ]

    if args.dry_run:
        print(f"primer cmd 0x{args.primer_command:02X}: {hex_preview(primer)}")
        print(f"query  cmd 0x{args.query_command:02X}: {hex_preview(query)}")
        for command, frame in candidates:
            print(f"candidate 0x{command:02X}: {hex_preview(frame)}")
        return 0

    emit, log_file = make_logger(args.log)
    try:
        with serial.Serial(
            port=args.port,
            baudrate=args.baud,
            bytesize=serial.EIGHTBITS,
            parity=serial.PARITY_NONE,
            stopbits=serial.STOPBITS_ONE,
            timeout=args.timeout,
            write_timeout=1.0,
            rtscts=False,
            dsrdtr=False,
            xonxoff=False,
        ) as ser:
            emit(
                f"Post-discovery sweep: primer {hex_preview(primer)}, "
                f"query {hex_preview(query)}, {len(candidates)} candidates "
                f"on {ser.port} at {ser.baudrate} 8N1"
            )

            for index, (command, candidate) in enumerate(candidates, start=1):
                if args.prompt_power_cycle:
                    answer = input(
                        f"Power-cycle RCP for candidate 0x{command:02X}, wait for heartbeat, "
                        "then press Enter (q then Enter to stop): "
                    ).strip()
                    if answer.lower() in {"q", "quit", "stop"}:
                        emit("Stopped before next candidate.")
                        break

                ser.reset_input_buffer()
                emit_rx(emit, f"CANDIDATE 0x{command:02X} BASELINE", read_window(ser, args.settle))

                stamp = dt.datetime.now().strftime("%H:%M:%S.%f")[:-3]
                emit(f"{stamp}  TX primer frame {len(primer):03d}  {hex_preview(primer)}")
                ser.write(primer)
                ser.flush()
                emit_rx(emit, f"{stamp}  PRIMER RX", read_window(ser, args.between))

                stamp = dt.datetime.now().strftime("%H:%M:%S.%f")[:-3]
                emit(f"{stamp}  TX query 0x{args.query_command:02X} frame {len(query):03d}  {hex_preview(query)}")
                ser.write(query)
                ser.flush()
                emit_rx(emit, f"{stamp}  QUERY RX", read_window(ser, args.after_query))

                stamp = dt.datetime.now().strftime("%H:%M:%S.%f")[:-3]
                emit(f"{stamp}  TX candidate 0x{command:02X} frame {len(candidate):03d}  {hex_preview(candidate)}")
                ser.write(candidate)
                ser.flush()
                candidate_anomaly = emit_rx(
                    emit,
                    f"{stamp}  CANDIDATE 0x{command:02X} RX",
                    read_window(ser, args.after_candidate),
                )

                if args.prompt_screen:
                    screen = input(
                        f"Screen after candidate 0x{command:02X} "
                        "(blank = no change, q = stop): "
                    ).strip()
                    if screen:
                        stamp = dt.datetime.now().strftime("%H:%M:%S.%f")[:-3]
                        emit(f"{stamp}  SCREEN candidate=0x{command:02X}  {screen}")
                    if screen.lower() in {"q", "quit", "stop"}:
                        break

                if candidate_anomaly and args.stop_on_candidate_anomaly:
                    emit("Stopping after candidate anomaly.")
                    break

                emit(f"Completed candidate {index}/{len(candidates)}")
    except KeyboardInterrupt:
        emit("Stopped.")
        return 0
    except serial.SerialException as exc:
        print(f"Serial error: {exc}", file=sys.stderr)
        return 1
    finally:
        if log_file:
            log_file.close()

    return 0


if __name__ == "__main__":
    raise SystemExit(main())
