from pathlib import Path
from PIL import Image
from reportlab.lib.colors import HexColor, black, white
from reportlab.lib.pagesizes import A3, landscape
from reportlab.graphics.barcode import qr
from reportlab.pdfgen import canvas

ROOT = Path('/home/sebas/work/tasks/T-bbx-print-20260520')
SRC_LOGO = Path('/home/sebas/work/projects/ballbox/public/images/ballbox-logo.png')
OUT_LOGO = ROOT / 'ballbox-logo-clean.png'
OUT_PDF = ROOT / 'ballbox-plot-a3-horizontal.pdf'
OUT_NOTES = ROOT / 'review-notes.txt'
URL = 'https://ballbox.app'
BRAND = HexColor('#B7D334')
DARK = HexColor('#111111')
LIGHT = HexColor('#F8F8F5')


def clean_logo(src: Path, out: Path):
    img = Image.open(src).convert('RGBA')
    px = img.load()
    for y in range(img.height):
        for x in range(img.width):
            r, g, b, a = px[x, y]
            if r < 20 and g < 20 and b < 20:
                px[x, y] = (0, 0, 0, 0)
    bbox = img.getbbox()
    img = img.crop(bbox)
    target_w = 2200
    target_h = int(img.height * (target_w / img.width))
    img = img.resize((target_w, target_h), Image.LANCZOS)
    img.save(out)
    return img.size


def make_pdf():
    page_w, page_h = landscape(A3)
    c = canvas.Canvas(str(OUT_PDF), pagesize=(page_w, page_h))

    margin = 42
    safe_x = margin
    safe_y = margin
    safe_w = page_w - margin * 2
    safe_h = page_h - margin * 2

    c.setFillColor(white)
    c.rect(0, 0, page_w, page_h, stroke=0, fill=1)

    # subtle framing
    c.setStrokeColor(BRAND)
    c.setLineWidth(2)
    c.roundRect(safe_x, safe_y, safe_w, safe_h, 18, stroke=1, fill=0)

    # logo block
    logo_max_w = safe_w * 0.50
    logo_max_h = safe_h * 0.60
    from reportlab.lib.utils import ImageReader
    logo = ImageReader(str(OUT_LOGO))
    iw, ih = Image.open(OUT_LOGO).size
    scale = min(logo_max_w / iw, logo_max_h / ih)
    draw_w = iw * scale
    draw_h = ih * scale
    logo_x = safe_x + 48
    logo_y = (page_h - draw_h) / 2 + 24
    c.drawImage(logo, logo_x, logo_y, width=draw_w, height=draw_h, mask='auto')

    # qr block
    qr_size = safe_h * 0.48
    qr_x = safe_x + safe_w - qr_size - 70
    qr_y = (page_h - qr_size) / 2 + 38
    widget = qr.QrCodeWidget(URL)
    bounds = widget.getBounds()
    bw = bounds[2] - bounds[0]
    bh = bounds[3] - bounds[1]
    d = qr_size
    from reportlab.graphics.shapes import Drawing
    from reportlab.graphics import renderPDF
    drawing = Drawing(d, d, transform=[d / bw, 0, 0, d / bh, 0, 0])
    drawing.add(widget)

    # qr frame
    pad = 18
    c.setFillColor(white)
    c.setStrokeColor(DARK)
    c.setLineWidth(1.2)
    c.roundRect(qr_x - pad, qr_y - pad, qr_size + 2*pad, qr_size + 2*pad, 14, stroke=1, fill=1)
    renderPDF.draw(drawing, c, qr_x, qr_y)

    # connector/accent
    line_y = page_h / 2
    c.setStrokeColor(BRAND)
    c.setLineWidth(4)
    c.line(logo_x + draw_w + 24, line_y, qr_x - 36, line_y)

    c.setFillColor(DARK)
    c.setFont('Helvetica-Bold', 28)
    c.drawCentredString(qr_x + qr_size / 2, qr_y - 46, 'Escaneá y entrá')
    c.setFont('Helvetica', 18)
    c.drawCentredString(qr_x + qr_size / 2, qr_y - 72, URL)

    c.showPage()
    c.save()
    return page_w, page_h, qr_size


if __name__ == '__main__':
    logo_size = clean_logo(SRC_LOGO, OUT_LOGO)
    page_w, page_h, qr_size = make_pdf()
    OUT_NOTES.write_text(
        '\n'.join([
            'Ballbox A3 horizontal print deliverable',
            f'Page points: {page_w} x {page_h}',
            'Page mm: 420 x 297',
            f'Cleaned logo size px: {logo_size[0]} x {logo_size[1]}',
            f'QR square points: {qr_size:.2f}',
            f'URL: {URL}',
        ]) + '\n'
    )
    print(OUT_PDF)
