"""Generate tray icon PNG assets programmatically using PyQt6 QPainter.

Run from the frame/ directory:
    python tray/assets/generate_icons.py

Or with uv:
    uv run python tray/assets/generate_icons.py
"""
import pathlib
import sys

# Ensure we can import PyQt6 from the venv even when running standalone
HERE = pathlib.Path(__file__).parent

from PyQt6.QtWidgets import QApplication
from PyQt6.QtGui import QPixmap, QPainter, QColor, QPen, QBrush
from PyQt6.QtCore import Qt, QRect, QSize


def make_app() -> QApplication:
    """Create a minimal QApplication needed for QPainter/QPixmap."""
    app = QApplication.instance()
    if app is None:
        app = QApplication(sys.argv)
    return app


def draw_circle_icon(color: str, size: int = 64) -> QPixmap:
    """Draw a solid filled circle on a transparent background."""
    pixmap = QPixmap(QSize(size, size))
    pixmap.fill(QColor(0, 0, 0, 0))  # transparent

    painter = QPainter(pixmap)
    painter.setRenderHint(QPainter.RenderHint.Antialiasing)

    fill_color = QColor(color)
    painter.setBrush(QBrush(fill_color))
    painter.setPen(Qt.PenStyle.NoPen)

    margin = 4
    painter.drawEllipse(margin, margin, size - 2 * margin, size - 2 * margin)
    painter.end()

    return pixmap


def draw_processing_icon(angle_offset: int, size: int = 64) -> QPixmap:
    """Draw a blue circle with a rotating arc indicator for spinner animation.

    angle_offset: starting angle in degrees (0, 45, 90, ... 315)
    Qt angles are in 1/16ths of a degree, measured counter-clockwise from 3 o'clock.
    """
    pixmap = QPixmap(QSize(size, size))
    pixmap.fill(QColor(0, 0, 0, 0))  # transparent

    painter = QPainter(pixmap)
    painter.setRenderHint(QPainter.RenderHint.Antialiasing)

    margin = 4
    rect = QRect(margin, margin, size - 2 * margin, size - 2 * margin)

    # Base blue circle (dimmed)
    base_color = QColor("#1A66CC")
    painter.setBrush(QBrush(base_color))
    painter.setPen(Qt.PenStyle.NoPen)
    painter.drawEllipse(rect)

    # Bright arc overlay (quarter circle = 90 degrees = 1440 in Qt 1/16ths)
    arc_color = QColor("#3399FF")
    arc_pen = QPen(arc_color)
    arc_pen.setWidth(6)
    arc_pen.setCapStyle(Qt.PenCapStyle.RoundCap)
    painter.setPen(arc_pen)
    painter.setBrush(Qt.BrushStyle.NoBrush)

    # Qt measures angles counter-clockwise from 3 o'clock position
    # We want the arc to start at the top (12 o'clock = 90 degrees) and rotate
    # angle_offset 0 = top, rotating clockwise means decreasing Qt angle
    start_angle = (90 - angle_offset) * 16  # convert to Qt 1/16ths
    span_angle = 90 * 16  # quarter circle arc

    painter.drawArc(rect, start_angle, span_angle)
    painter.end()

    return pixmap


def main():
    app = make_app()
    output_dir = HERE

    # Idle: grey
    draw_circle_icon("#808080").save(str(output_dir / "icon_idle.png"))
    print("  icon_idle.png")

    # Recording: red
    draw_circle_icon("#FF0000").save(str(output_dir / "icon_recording.png"))
    print("  icon_recording.png")

    # Ready: green
    draw_circle_icon("#00CC00").save(str(output_dir / "icon_ready.png"))
    print("  icon_ready.png")

    # Processing: 8 spinner frames (0..315 degrees, step 45)
    for i in range(8):
        angle = i * 45
        pixmap = draw_processing_icon(angle)
        filename = f"icon_processing_{i}.png"
        pixmap.save(str(output_dir / filename))
        print(f"  {filename}")

    print("Done — 11 icons generated.")


if __name__ == "__main__":
    main()
