"""Analog clock SVG generator for AGS Math instruments.

generate(hour, minute) -> str  (self-contained SVG)

Geometry: 12 at top, clockwise. Angles measured clockwise from 12 o'clock.
  - minute hand angle = minute * 6 deg
  - hour hand angle   = (hour%12 + minute/60) * 30 deg  (offset by minutes)
A clockwise angle `a` (deg) from the top maps to canvas coords via:
  x = cx + r * sin(rad(a))
  y = cy - r * cos(rad(a))
"""
import math

STYLE = (
    "<style>svg{--crimson:#960000;--gold:#BF9237;--lime:#88C808;"
    "--steel:#4A6488;--cream:#FAF5EE;--ink:#2A2A2A;--border:#D0ACAC;}</style>"
)


def _pt(cx, cy, r, ang_deg):
    """Point at clockwise angle (deg from top) and radius r."""
    a = math.radians(ang_deg)
    return cx + r * math.sin(a), cy - r * math.cos(a)


def generate(hour=3, minute=0, size=320):
    hour = int(hour) % 12
    minute = int(minute) % 60
    cx = cy = size / 2
    R = size / 2 - 10          # outer face radius
    parts = []
    parts.append(
        f'<svg xmlns="http://www.w3.org/2000/svg" width="{size}" height="{size}" '
        f'viewBox="0 0 {size} {size}">'
    )
    parts.append(STYLE)

    # Border frame (rounded rect)
    parts.append(
        f'<rect x="3" y="3" width="{size-6}" height="{size-6}" rx="18" ry="18" '
        f'fill="none" stroke="var(--border)" stroke-width="4"/>'
    )
    # Cream face + steel ring
    parts.append(
        f'<circle cx="{cx}" cy="{cy}" r="{R}" fill="var(--cream)" '
        f'stroke="var(--border)" stroke-width="3"/>'
    )

    # 60 minute ticks (crimson), 12 hour ticks (steel, bolder)
    for i in range(60):
        ang = i * 6
        if i % 5 == 0:
            x1, y1 = _pt(cx, cy, R - 4, ang)
            x2, y2 = _pt(cx, cy, R - 18, ang)
            parts.append(
                f'<line x1="{x1:.2f}" y1="{y1:.2f}" x2="{x2:.2f}" y2="{y2:.2f}" '
                f'stroke="var(--steel)" stroke-width="4" stroke-linecap="round"/>'
            )
        else:
            x1, y1 = _pt(cx, cy, R - 4, ang)
            x2, y2 = _pt(cx, cy, R - 11, ang)
            parts.append(
                f'<line x1="{x1:.2f}" y1="{y1:.2f}" x2="{x2:.2f}" y2="{y2:.2f}" '
                f'stroke="var(--crimson)" stroke-width="2" stroke-linecap="round"/>'
            )

    # Numerals 1..12
    for n in range(1, 13):
        ang = n * 30
        tx, ty = _pt(cx, cy, R - 36, ang)
        parts.append(
            f'<text x="{tx:.2f}" y="{ty:.2f}" font-family="sans-serif" '
            f'font-size="{int(size*0.09)}" font-weight="bold" fill="var(--ink)" '
            f'text-anchor="middle" dominant-baseline="central">{n}</text>'
        )

    # Hands. Hour offset by minutes.
    hour_ang = (hour + minute / 60.0) * 30.0
    min_ang = minute * 6.0
    hx, hy = _pt(cx, cy, R * 0.52, hour_ang)
    mx, my = _pt(cx, cy, R * 0.78, min_ang)
    parts.append(
        f'<line x1="{cx}" y1="{cy}" x2="{hx:.2f}" y2="{hy:.2f}" '
        f'stroke="var(--ink)" stroke-width="7" stroke-linecap="round"/>'
    )
    parts.append(
        f'<line x1="{cx}" y1="{cy}" x2="{mx:.2f}" y2="{my:.2f}" '
        f'stroke="var(--ink)" stroke-width="4" stroke-linecap="round"/>'
    )
    # Center hub
    parts.append(f'<circle cx="{cx}" cy="{cy}" r="6" fill="var(--ink)"/>')

    parts.append('</svg>')
    return '\n'.join(parts)


if __name__ == '__main__':
    import os
    out = os.path.join(os.path.dirname(__file__), '..', 'samples')
    out = os.path.abspath(out)
    os.makedirs(out, exist_ok=True)
    samples = [(3, 0), (6, 30), (10, 15)]
    for i, (h, m) in enumerate(samples, 1):
        p = os.path.join(out, f'clock_{i}.svg')
        with open(p, 'w', encoding='utf-8') as f:
            f.write(generate(h, m))
        print('wrote', p)
