#!/usr/bin/env python3
"""Generate the pvtcoms app icon — a lime (citrus cross-section) in the Forest+Lime palette.

Outputs:
  demo/assets/icon.ico   — multi-size Windows icon (embedded into the .exe via build.rs/windres)
  demo/assets/icon.png   — 256px reference
  demo/assets/favicon.svg — scalable favicon (used by the browser GUI + download page)
"""
import math
import os
from PIL import Image, ImageDraw

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ASSETS = os.path.join(ROOT, "demo", "assets")
os.makedirs(ASSETS, exist_ok=True)

BG = (244, 247, 245, 255)     # #F4F7F5 Crisp-Mint light tile (matches the app background)
RIND = (31, 175, 114, 255)    # #1FAF72 forest green rind (defines the disc against the light tile)
FLESH = (183, 243, 74, 255)   # #B7F34A lime flesh
PITH = (255, 255, 255, 255)   # white segments (crisp on the lime flesh)


def render(size, sup=4):
    S = size * sup
    img = Image.new("RGBA", (S, S), (0, 0, 0, 0))
    d = ImageDraw.Draw(img)
    d.rounded_rectangle([0, 0, S - 1, S - 1], radius=int(S * 0.22), fill=BG)
    cx = cy = S / 2
    R = S * 0.37
    d.ellipse([cx - R, cy - R, cx + R, cy + R], fill=RIND)           # rind
    Rf = R * 0.84
    d.ellipse([cx - Rf, cy - Rf, cx + Rf, cy + Rf], fill=FLESH)      # flesh
    n = 10                                                           # citrus segments
    w = max(2, int(S * 0.013))
    for k in range(n):
        a = 2 * math.pi * k / n - math.pi / 2
        x = cx + Rf * 0.97 * math.cos(a)
        y = cy + Rf * 0.97 * math.sin(a)
        d.line([cx, cy, x, y], fill=PITH, width=w)
    cr = S * 0.035
    d.ellipse([cx - cr, cy - cr, cx + cr, cy + cr], fill=PITH)       # center pith
    return img.resize((size, size), Image.LANCZOS)

base = render(256)
sizes = [(16, 16), (24, 24), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)]
base.save(os.path.join(ASSETS, "icon.ico"), sizes=sizes)
base.save(os.path.join(ASSETS, "icon.png"))

# Scalable favicon (same lime, drawn as SVG so it stays crisp in the browser tab).
spokes = []
import math as _m
for k in range(10):
    a = 2 * _m.pi * k / 10 - _m.pi / 2
    x = 32 + 18 * _m.cos(a)
    y = 32 + 18 * _m.sin(a)
    spokes.append(f'<line x1="32" y1="32" x2="{x:.1f}" y2="{y:.1f}"/>')
svg = (
    '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">'
    '<rect width="64" height="64" rx="14" fill="#F4F7F5"/>'
    '<circle cx="32" cy="32" r="24" fill="#1FAF72"/>'
    '<circle cx="32" cy="32" r="20" fill="#B7F34A"/>'
    '<g stroke="#FFFFFF" stroke-width="1.5" stroke-linecap="round">' + "".join(spokes) + "</g>"
    '<circle cx="32" cy="32" r="2.2" fill="#FFFFFF"/></svg>'
)
with open(os.path.join(ASSETS, "favicon.svg"), "w") as f:
    f.write(svg)

print("wrote icon.ico, icon.png, favicon.svg to", ASSETS)
print("FAVICON_SVG:" + svg)
