#!/usr/bin/env python3
"""Convert every visible CFP date to American English, preserving the artwork."""

from pathlib import Path
from PIL import Image, ImageDraw, ImageFont, ImageFilter

ROOT = Path(__file__).parent


def sora(size: int, weight: int = 700):
    font = ImageFont.truetype(ROOT / "Sora.ttf", size)
    try:
        font.set_variation_by_axes([weight])
    except Exception:
        pass
    return font


def erase_colored_text(image, box, selector):
    x0, y0, x1, y1 = box
    crop = image.crop(box)
    mask = Image.new("L", crop.size)
    mp = mask.load()
    cp = crop.load()
    for y in range(crop.height):
        for x in range(crop.width):
            mp[x, y] = 255 if selector(cp[x, y]) else 0
    mask = mask.filter(ImageFilter.MaxFilter(7))
    mp = mask.load()
    # Fill each text run by interpolating its immediate left/right background pixels.
    for y in range(crop.height):
        x = 0
        while x < crop.width:
            if not mp[x, y]:
                x += 1
                continue
            start = x
            while x < crop.width and mp[x, y]:
                x += 1
            end = x
            left = cp[max(0, start - 1), y]
            right = cp[min(crop.width - 1, end), y]
            width = max(1, end - start)
            for xx in range(start, end):
                t = (xx - start) / width
                cp[xx, y] = tuple(round(left[c] * (1 - t) + right[c] * t) for c in range(3))
    image.paste(crop, box)
    return image


# Page 1: remove only the light/gold date glyphs; nearest-background filling preserves
# the photograph and the fine circular linework behind the text.
p1 = Image.open(ROOT / "cfp_p1.jpg").convert("RGB")
bright = lambda p: p[0] > 145 and p[1] > 135 and p[2] > 95
p1 = erase_colored_text(p1, (120, 795, 570, 865), bright)
p1 = erase_colored_text(p1, (120, 2060, 530, 2130), bright)
p1 = erase_colored_text(p1, (625, 2060, 1050, 2130), bright)
d = ImageDraw.Draw(p1)
d.text((136, 847), "JULY 13–16, 2027", font=sora(48), fill=(211, 180, 101), anchor="ls")
d.text((136, 2112), "September 1, 2026", font=sora(42), fill=(255, 255, 255), anchor="ls")
d.text((640, 2112), "February 12, 2027", font=sora(42), fill=(211, 180, 101), anchor="ls")
p1.save(ROOT / "cfp_p1.jpg", quality=96, subsampling=0)

# Page 2: the key-date panel and header have flat backgrounds, so redraw them cleanly.
p2 = Image.open(ROOT / "cfp_p2_fixed.png").convert("RGB")
d = ImageDraw.Draw(p2)
cream = p2.getpixel((1600, 1100))
navy = p2.getpixel((1600, 100))
d.rectangle((120, 88, 1250, 155), fill=navy)
d.text((130, 140), "Niagara Falls, Canada · July 13–16, 2027", font=sora(34, 500), fill=(235, 234, 226), anchor="ls")
d.rectangle((820, 975, 1350, 1310), fill=cream)
dates = [
    (1035, "September 1, 2026", (13, 31, 43)),
    (1084, "February 12, 2027", (164, 126, 42)),
    (1132, "February 19, 2027", (13, 31, 43)),
    (1181, "April 23, 2027", (13, 31, 43)),
    (1230, "June 5, 2027", (13, 31, 43)),
    (1279, "July 13–16, 2027", (164, 126, 42)),
]
for y, text, color in dates:
    d.text((830, y), text, font=sora(39), fill=color, anchor="ls")
p2.save(ROOT / "cfp_p2_fixed.png")

print("CFP artwork converted to American date style")
