"""Draw a 32 x 32 pixel archery bow. Requires Pillow: pip install pillow."""

from pathlib import Path

from PIL import Image, ImageDraw


OUT_DIR = Path(__file__).resolve().parent
PALETTE = {
    "outline": "#30232C",
    "wood": "#C17C38",
    "wood_light": "#F1BB64",
    "wood_shadow": "#875039",
    "leather": "#6B3B35",
    "leather_light": "#A26647",
    "gold": "#E4AD57",
    "string": "#B5A88D",
    "nock": "#F4DFAC",
}


def draw_bow():
    """Use whole pixels only, with an empty, transparent background."""
    sprite = Image.new("RGBA", (32, 32), (0, 0, 0, 0))
    pen = ImageDraw.Draw(sprite)

    # A taut, one-pixel bowstring, secured at both limb tips.
    pen.line([(8, 3), (8, 28)], fill=PALETTE["string"])

    # Inclusive horizontal spans for the upper limb. Mirror them below.
    # A dark outline and a small palette keep the silhouette readable at 1x.
    upper_limb = [
        (2, 7, 9),
        (3, 7, 11),
        (4, 9, 13),
        (5, 11, 15),
        (6, 13, 16),
        (7, 15, 18),
        (8, 16, 19),
        (9, 17, 20),
        (10, 18, 21),
        (11, 19, 21),
        (12, 20, 22),
        (13, 20, 23),
        (14, 21, 23),
        (15, 21, 24),
    ]
    for y, left, right in upper_limb:
        for row in (y, 31 - y):
            pen.line((left, row, right, row), fill=PALETTE["outline"])
            if y > 2:
                pen.line((left + 1, row, right - 1, row), fill=PALETTE["wood"])
                pen.point((left + 1, row), fill=PALETTE["wood_light"])
                if right - left >= 4:
                    pen.point((right - 1, row), fill=PALETTE["wood_shadow"])

    # Pale bindings at the string nocks.
    for point in ((8, 2), (9, 3), (9, 28), (8, 29)):
        pen.point(point, fill=PALETTE["nock"])

    # Leather-wrapped grip, with narrow brass collars at either end.
    pen.rectangle((20, 13, 24, 18), fill=PALETTE["outline"])
    pen.rectangle((21, 14, 23, 17), fill=PALETTE["leather"])
    for row in (13, 18):
        pen.line((21, row, 23, row), fill=PALETTE["gold"])
    for point in ((21, 14), (22, 15), (23, 16), (21, 16), (22, 17)):
        pen.point(point, fill=PALETTE["leather_light"])

    return sprite


if __name__ == "__main__":
    bow = draw_bow()
    bow.save(OUT_DIR / "bow.png")
    # Nearest-neighbor scaling preserves the exact pixel shapes for viewing.
    bow.resize((512, 512), Image.Resampling.NEAREST).save(OUT_DIR / "bow_preview.png")
    print(f"Saved {OUT_DIR / 'bow.png'} (32 x 32, RGBA)")
