"""Draw a 32 x 32 pixel-art archery bow. Requires Pillow."""

from pathlib import Path

from PIL import Image, ImageDraw, ImageFilter


def draw_bow() -> Image.Image:
    image = Image.new("RGBA", (32, 32), (0, 0, 0, 0))
    draw = ImageDraw.Draw(image)

    # A taut bowstring, behind the recurved wooden limbs.
    draw.line([(20, 3), (20, 28)], fill="#C8B99B", width=1)
    draw.line([(20, 8), (20, 23)], fill="#E6D8B9", width=1)

    spine = [
        (20, 3), (22, 4), (22, 5), (18, 7), (15, 9),
        (12, 12), (10, 15), (10, 16), (12, 19),
        (15, 22), (18, 24), (22, 26), (22, 27), (20, 28),
    ]
    wood = Image.new("L", image.size, 0)
    ImageDraw.Draw(wood).line(spine, fill=255, width=3, joint="curve")
    outline = wood.filter(ImageFilter.MaxFilter(3))
    image.paste("#382A28", (0, 0), outline)

    # One-pixel facets give the wood depth without antialiasing.
    paint = Image.new("RGBA", image.size, "#C7843B")
    facets = ImageDraw.Draw(paint)
    facets.line([(x + 1, y) for x, y in spine], fill="#92512E", width=1)
    facets.line([(x - 1, y) for x, y in spine], fill="#F6C66D", width=1)
    image.paste(paint, (0, 0), wood)

    # A dark leather handle with individual diagonal wraps.
    draw = ImageDraw.Draw(image)
    draw.polygon(
        [(9, 13), (12, 13), (12, 14), (11, 15), (11, 16),
         (12, 17), (12, 18), (9, 18), (8, 16), (8, 15)],
        fill="#382A28",
    )
    draw.line([(9, 14), (11, 13)], fill="#BE8052")
    draw.line([(9, 16), (11, 15)], fill="#98603F")
    draw.line([(10, 18), (11, 17)], fill="#BE8052")
    draw.point((9, 15), fill="#E0A767")

    # Light-colored string bindings at the tips.
    draw.line([(20, 3), (21, 4)], fill="#F8DEA0")
    draw.line([(21, 27), (20, 28)], fill="#F8DEA0")
    return image


if __name__ == "__main__":
    output = Path(__file__).resolve().parent
    bow = draw_bow()
    bow.save(output / "archery_bow.png")
    bow.resize((512, 512), Image.Resampling.NEAREST).save(
        output / "archery_bow_preview.png"
    )
    print(f"Saved {output / 'archery_bow.png'} (32 x 32, transparent RGBA)")
