"""Sinh bo icon cho Tauri v2 + PyInstaller (chay 1 lan, co the tai chay).

Usage: python tools/gen_icons.py
Output: src-tauri/icons/{32x32,128x128,128x128@2x}.png, icon.png, icon.ico
"""
import os
from PIL import Image, ImageDraw, ImageFont

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ICONS = os.path.join(ROOT, "src-tauri", "icons")
BG = (18, 20, 26, 255)          # #12141a — trùng màu nền loader
FG = (77, 182, 255, 255)        # xanh dương sáng
RING = (46, 52, 66, 255)


def _font(size):
    for p in ("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
              "C:/Windows/Fonts/segoeuib.ttf"):
        if os.path.exists(p):
            return ImageFont.truetype(p, size)
    return ImageFont.load_default()


def _draw(size):
    img = Image.new("RGBA", (size, size), (0, 0, 0, 0))
    d = ImageDraw.Draw(img)
    # nền bo góc + viền
    r = size // 8
    d.rounded_rectangle([0, 0, size - 1, size - 1], radius=r, fill=BG)
    d.rounded_rectangle([size // 32, size // 32, size - 1 - size // 32, size - 1 - size // 32],
                        radius=max(1, r - size // 32), outline=RING, width=max(2, size // 64))
    # chữ "SF"
    font = _font(int(size * 0.42))
    text = "SF"
    bbox = d.textbbox((0, 0), text, font=font)
    w, h = bbox[2] - bbox[0], bbox[3] - bbox[1]
    d.text(((size - w) / 2 - bbox[0], (size - h) / 2 - bbox[1]), text,
           font=font, fill=FG)
    return img


def main():
    os.makedirs(ICONS, exist_ok=True)
    sizes = {"32x32.png": 32, "128x128.png": 128, "128x128@2x.png": 256, "icon.png": 512}
    imgs = []
    for name, size in sizes.items():
        img = _draw(size)
        img.save(os.path.join(ICONS, name))
        imgs.append((img, size))
        print("generated", name)
    # icon.ico: multi-size
    base = _draw(256)
    base.save(os.path.join(ICONS, "icon.ico"),
              sizes=[(16, 16), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)])
    print("generated icon.ico")


if __name__ == "__main__":
    main()
