"""Create the multi-resolution Windows application icon."""

from __future__ import annotations

import argparse
import math
from pathlib import Path

from PIL import Image, ImageDraw


def _wave(
    draw: ImageDraw.ImageDraw,
    center: float,
    amplitude: float,
    color: str,
    phase: float,
    width: int,
) -> None:
    points = []
    for x in range(26, 231):
        angle = (x - 26) / 205.0 * math.tau * 2.4 + phase
        points.append((x, center + amplitude * math.sin(angle)))
    draw.line(points, fill=color, width=width, joint="curve")


def create_icon(path: Path) -> None:
    image = Image.new("RGBA", (256, 256), "#202328")
    draw = ImageDraw.Draw(image)
    draw.rounded_rectangle(
        (7, 7, 248, 248),
        radius=45,
        fill="#2D3137",
        outline="#737B84",
        width=5,
    )
    _wave(draw, 78, 19, "#F3A712", 0.0, 9)
    _wave(draw, 120, 17, "#62B6CB", 0.7, 9)
    draw.line((28, 157, 228, 157), fill="#5A626B", width=3)
    _wave(draw, 190, 12, "#8FE3B0", -0.3, 10)
    draw.polygon(
        ((28, 173), (40, 194), (28, 215), (20, 215), (32, 194), (20, 173)),
        fill="#FFFFFF",
    )
    path.parent.mkdir(parents=True, exist_ok=True)
    image.save(
        path,
        format="ICO",
        sizes=[(16, 16), (24, 24), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)],
    )


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("--output", type=Path, required=True)
    arguments = parser.parse_args()
    create_icon(arguments.output)
    print(f"Wrote {arguments.output}")
    return 0


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