# -*- coding: utf-8 -*-
"""Generate the NACEF POS product logo with Pillow (no SVG rasterizer needed).

A modern "fiscal receipt with a verified check" mark on a teal rounded square.
Run inside the odoo image: python3 make_logo.py
"""
import os

from PIL import Image, ImageDraw

OUT = os.path.dirname(os.path.abspath(__file__))
TEAL_D = (15, 118, 110)     # #0f766e
TEAL = (13, 148, 136)       # #0d9488
TEAL_L = (20, 184, 166)     # #14b8a6
WHITE = (255, 255, 255)


def _gradient(size, top, bottom):
    base = Image.new("RGB", (1, size), 0)
    for y in range(size):
        t = y / max(size - 1, 1)
        base.putpixel((0, y), tuple(
            int(top[i] + (bottom[i] - top[i]) * t) for i in range(3)))
    return base.resize((size, size))


def _rounded_mask(size, radius):
    m = Image.new("L", (size, size), 0)
    d = ImageDraw.Draw(m)
    d.rounded_rectangle([0, 0, size - 1, size - 1], radius=radius, fill=255)
    return m


def make_icon(size=512):
    S = size
    img = Image.new("RGBA", (S, S), (0, 0, 0, 0))
    tile = _gradient(S, TEAL_L, TEAL_D)
    img.paste(tile, (0, 0), _rounded_mask(S, int(S * 0.22)))
    d = ImageDraw.Draw(img)

    # receipt body (white rounded rect with a torn/zigzag bottom)
    rx0, ry0, rx1, ry1 = int(S*0.30), int(S*0.20), int(S*0.70), int(S*0.74)
    d.rounded_rectangle([rx0, ry0, rx1, ry1], radius=int(S*0.05), fill=WHITE)
    # zigzag bottom edge
    teeth = 6
    step = (rx1 - rx0) / teeth
    pts = [(rx0, ry1)]
    for i in range(teeth):
        x = rx0 + step * (i + 0.5)
        pts.append((x, ry1 + int(S*0.03)))
        pts.append((rx0 + step * (i + 1), ry1))
    pts += [(rx1, ry1 + int(S*0.06)), (rx0, ry1 + int(S*0.06))]
    d.polygon(pts, fill=WHITE)

    # receipt text lines
    ly = ry0 + int(S*0.09)
    for w, gap in [(0.30, 0.075), (0.22, 0.06), (0.26, 0.06)]:
        d.rounded_rectangle(
            [rx0 + int(S*0.06), ly, rx0 + int(S*0.06) + int(S*w), ly + int(S*0.02)],
            radius=int(S*0.012), fill=(203, 213, 225))
        ly += int(S * gap)

    # verified check badge (bottom-right)
    cx, cy, cr = int(S*0.66), int(S*0.66), int(S*0.135)
    d.ellipse([cx-cr, cy-cr, cx+cr, cy+cr], fill=TEAL_D, outline=WHITE,
              width=int(S*0.018))
    d.line([(cx-cr*0.45, cy+cr*0.02), (cx-cr*0.10, cy+cr*0.38),
            (cx+cr*0.5, cy-cr*0.35)], fill=WHITE, width=int(S*0.05),
           joint="curve")

    img.save(os.path.join(OUT, "logo.png"))

    # small favicon
    img.resize((64, 64)).save(os.path.join(OUT, "favicon.png"))
    print("wrote logo.png (%dx%d) + favicon.png" % (S, S))


if __name__ == "__main__":
    make_icon(512)
