import urllib.request, time
from rembg import remove
from PIL import Image
from pathlib import Path

CHARS = Path(r"C:\allgifted\mathapi11v2\public\assets\math\chars")
LOGO  = Path(r"C:\allgifted\logo-reveal")
base  = "https://d8j0ntlcm91z4.cloudfront.net/user_3ENYB9gUptp5V9uUGoiaDV631pi/"
badge_src = Image.open(CHARS / "ag_badge.png").convert("RGBA")

# clean (badge-less) source per final filename
SRC = {
 "ags-math-char-sheep.png":     "hf_20260529_053920_52848134-6d7d-4dce-acb1-ce593b7963af.png",
 "ags-math-char-elephant.png":  "hf_20260529_054229_c919d129-8022-4dfe-acb7-27726febee99.png",
 "ags-math-char-goldfish.png":  "hf_20260529_054239_d2f4fa78-7d99-44ab-a0d8-c79f244fb5f9.png",
 "ags-math-char-fish.png":      "hf_20260529_054251_704bfd5b-8b67-41f0-a07e-27143e487899.png",
 "ags-math-char-bee.png":       "hf_20260529_054300_864ba0f6-2c8d-4188-8847-c2b902ccb1da.png",
 "ags-math-char-ladybird.png":  "hf_20260529_054311_f91fb48d-3053-47ff-9a69-b6e3951e314b.png",
 "ags-math-char-butterfly.png": "hf_20260529_054323_15abde38-746b-4564-83c3-d2fa6d4a3635.png",
 "ags-math-char-alligator.png": "hf_20260529_054333_d046ebe6-2ebb-44be-9d13-7b8e9aeb62c2.png",
 "ags-math-char-carrot.png":     "hf_20260529_090647_5af1f35c-7573-4c3d-bbd8-48fcfc296877.png",
 "ags-math-char-lucky.png":      "hf_20260529_093458_fbedbba0-d334-4586-a887-cd6b36adf7d2.png",
 "ags-math-char-mimi.png":       "hf_20260529_095035_0d2f8199-259d-4cfb-b409-ef4333363548.png",
 "ags-math-char-snail-small.png":"hf_20260529_090716_40101b51-2e67-44a3-b058-6f3aa182ee3e.png",
 "ags-math-char-snail-big.png":  "hf_20260529_090722_e0b43514-4b54-4636-bc69-d0391133c791.png",
 "ags-math-char-rabbit.png":     "hf_20260529_090741_65948944-f0ce-4df9-99a8-37db8269a0f5.png",
 "ags-math-char-mouse.png":      "hf_20260529_090900_f885a62d-b4ce-4973-8e97-c3191cf94a2f.png",
 "ags-math-char-dragonfly.png":  "hf_20260529_092614_9880e3f8-bc21-4d86-b4f5-96c905cf7b66.png",
 "ags-math-char-bear.png":       "hf_20260529_090938_0060e0fa-01d0-44c1-94ad-c660d42bb0e5.png",
}

def fetch(url, dest):
    for i in range(5):
        try:
            urllib.request.urlretrieve(url, dest); return True
        except Exception:
            time.sleep(2)
    return False

def add_badge(rgba, frac=0.07, pad=14, opacity=0.55):
    tw = int(rgba.width * frac)
    b = badge_src.resize((tw, int(badge_src.height * tw / badge_src.width)), Image.LANCZOS)
    r, g, bl, a = b.split()
    a = a.point(lambda v: int(v * opacity))
    b = Image.merge("RGBA", (r, g, bl, a))
    x = rgba.width - b.width - pad
    y = rgba.height - b.height - pad
    layer = Image.new("RGBA", rgba.size, (0, 0, 0, 0))
    layer.paste(b, (x, y))
    return Image.alpha_composite(rgba, layer)

def process(src_img, out_path):
    cut = remove(src_img.convert("RGBA"))      # RGBA, transparent bg
    cut = add_badge(cut)
    cut.save(out_path, optimize=True)
    return out_path.stat().st_size

print("Processing 17 chars + Mei...", flush=True)
for fname, srcfile in SRC.items():
    if fname == "ags-math-char-lucky.png":
        # use the locally hand-edited Lucky (one fang removed)
        src_img = Image.open(LOGO / "lucky_v5.png")
        sz = process(src_img, CHARS / fname)
        print(f"{fname:30s} {sz/1024:7.1f}KB  (local edited)", flush=True)
        continue
    tmp = CHARS / ("_src_" + fname)
    if not fetch(base + srcfile, tmp):
        print(f"  FETCH FAIL {fname}", flush=True); continue
    sz = process(Image.open(tmp), CHARS / fname)
    tmp.unlink()
    print(f"{fname:30s} {sz/1024:7.1f}KB", flush=True)

# Mei (local source in logo-reveal)
mei_src = LOGO / "mei_test_v2.png"
if mei_src.exists():
    sz = process(Image.open(mei_src), LOGO / "mei_transparent.png")
    print(f"{'mei_transparent.png (logo-reveal)':30s} {sz/1024:7.1f}KB", flush=True)

print("DONE", flush=True)
