#!/usr/bin/env python3
"""gatos_comp.py: one folder per version -> a gatos.pics comparison link.
            una carpeta por versión -> el enlace de una comparación en gatos.pics.

    python3 gatos_comp.py [--title "My film"] [--frames 136259,140021] source/ x265/ av1/

Each folder is a version (its name is the version's name); the screenshots in
each folder, in name order, are frames 1, 2, 3... Every folder needs the same
number of images. A .json with the same name as a screenshot (x265_01.json next
to x265_01.png) carries its metrics: {"ssimulacra2": 81.4, "frame_type": "B"}
(numbers or short text). An info.json in a folder describes that whole version:
{"profile": "dark", "tonemap": "bt2390"}.

The images go up one per request, so a large page has no 300 MiB per-request cap.
The delete key is appended to gatos_comp.txt in the current folder: keep it, it
deletes or updates the page and cannot be recovered.

Optional environment variables:
    GATOS_API_KEY   your personal API key (without one, the hourly limit applies)
    GATOS_URL       another server (default https://gatos.pics)

Python 3 only, nothing to install. https://gatos.pics/api/
"""
import json
import os
import re
import sys
import time
import urllib.error
import urllib.request
import uuid
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime

BASE = os.environ.get("GATOS_URL", "https://gatos.pics").rstrip("/")
KEY = os.environ.get("GATOS_API_KEY", "").strip()
EXTS = (".png", ".jpg", ".jpeg", ".webp", ".avif", ".gif", ".bmp")
SPANISH = os.environ.get("LANG", "").lower().startswith("es")
TEXT = {
    "usage": ("usage: python3 gatos_comp.py [--title TITLE] [--frames N1,N2,...] FOLDER FOLDER [FOLDER...]",
              "uso: python3 gatos_comp.py [--title TÍTULO] [--frames N1,N2,...] CARPETA CARPETA [CARPETA...]"),
    "two": ("at least two folders are needed, one per version", "hacen falta al menos dos carpetas, una por versión"),
    "max": ("a page holds up to 12 versions", "una página admite hasta 12 versiones"),
    "notdir": ("not a folder: {}", "no es una carpeta: {}"),
    "empty": ("no images in {}", "no hay imágenes en {}"),
    "counts": ("every folder needs the same number of images: {}", "todas las carpetas llevan la misma cantidad de imágenes: {}"),
    "frames": ("--frames lists {} frames and each folder has {} images", "--frames trae {} cuadros y cada carpeta tiene {} imágenes"),
    "toomany": ("a page holds up to 200 frames", "una página admite hasta 200 cuadros"),
    "start": ("{} versions x {} frames, {:.1f} MiB", "{} versiones x {} cuadros, {:.1f} MiB"),
    "sent": ("\r{}/{} images sent", "\r{}/{} imágenes enviadas"),
    "fail": ("gatos.pics said no ({}): {}", "gatos.pics lo rechazó ({}): {}"),
    "net": ("could not reach {}: {}", "no se pudo llegar a {}: {}"),
    "notdone": ("all images were sent but the page is not ready yet; open {} in a minute", "se enviaron todas las imágenes pero la página todavía no está lista; abre {} en un minuto"),
    "saved": ("delete key saved in {}", "llave de borrado guardada en {}"),
}


def t(name, *args):
    return TEXT[name][1 if SPANISH else 0].format(*args)


def die(msg):
    print(msg, file=sys.stderr)
    sys.exit(1)


def natural(name):
    return [int(p) if p.isdigit() else p.lower() for p in re.split(r"(\d+)", name)]


def multipart(fields, files=()):
    """fields: [(name, text)], files: [(name, filename, bytes)] -> (body, content type)"""
    boundary = uuid.uuid4().hex
    out = []
    for name, value in fields:
        out.append(f'--{boundary}\r\nContent-Disposition: form-data; name="{name}"\r\n\r\n'.encode() + str(value).encode() + b"\r\n")
    for name, filename, data in files:
        safe = filename.replace('"', "_").replace("\r", "_").replace("\n", "_")
        out.append(f'--{boundary}\r\nContent-Disposition: form-data; name="{name}"; filename="{safe}"\r\n'
                   f"Content-Type: application/octet-stream\r\n\r\n".encode() + data + b"\r\n")
    out.append(f"--{boundary}--\r\n".encode())
    return b"".join(out), f"multipart/form-data; boundary={boundary}"


def post(path, fields, files=(), tries=5):
    """POST with a few retries on network errors and 5xx answers -> (status, json)"""
    body, ctype = multipart(fields, files)
    headers = {"Content-Type": ctype, "User-Agent": "gatos_comp.py/1"}
    if KEY:
        headers["x-api-key"] = KEY
    for attempt in range(tries):
        req = urllib.request.Request(BASE + path, data=body, headers=headers, method="POST")
        try:
            with urllib.request.urlopen(req, timeout=300) as r:
                return r.status, json.loads(r.read() or b"{}")
        except urllib.error.HTTPError as e:
            try:
                answer = json.loads(e.read() or b"{}")
            except ValueError:
                answer = {}
            if e.code < 500 or attempt == tries - 1:
                die(t("fail", e.code, answer.get("error", e.reason)))
        except (urllib.error.URLError, TimeoutError, ConnectionError) as e:
            if attempt == tries - 1:
                die(t("net", BASE, getattr(e, "reason", e)))
        time.sleep(min(60, 5 * 2 ** attempt))


def main():
    args, title, frames = [], "", None
    argv = sys.argv[1:]
    i = 0
    while i < len(argv):
        a = argv[i]
        if a in ("-h", "--help"):
            print(__doc__)
            return
        if a in ("--title", "--frames") and i + 1 < len(argv):
            if a == "--title":
                title = argv[i + 1]
            else:
                frames = [f.strip() for f in argv[i + 1].split(",")]
            i += 2
            continue
        args.append(a)
        i += 1
    if len(args) < 2:
        die(t("usage") + "\n" + t("two"))
    if len(args) > 12:
        die(t("max"))

    versions = []   # [(name, [image paths])]
    infos = {}      # version index -> text of its info.json
    used = set()
    for folder in args:
        if not os.path.isdir(folder):
            die(t("notdir", folder))
        name = os.path.basename(os.path.abspath(folder)) or "version"
        base, n = name, 2
        while name.lower() in used:
            name, n = f"{base} ({n})", n + 1
        used.add(name.lower())
        images = sorted((f for f in os.listdir(folder) if f.lower().endswith(EXTS) and not f.startswith(".")), key=natural)
        if not images:
            die(t("empty", folder))
        versions.append((name, [os.path.join(folder, f) for f in images]))
        about = os.path.join(folder, "info.json")   # the whole version's facts; not the metrics of a screenshot named info.*
        if os.path.isfile(about) and not any(os.path.splitext(f)[0].lower() == "info" for f in images):
            with open(about, "r", encoding="utf-8") as fh:
                infos[len(versions) - 1] = fh.read()
    counts = [len(imgs) for _, imgs in versions]
    if len(set(counts)) > 1:
        die(t("counts", ", ".join(f"{n}: {c}" for (n, _), c in zip(versions, counts))))
    count = counts[0]
    if count > 200:
        die(t("toomany"))
    if frames and len(frames) != count:
        die(t("frames", len(frames), count))
    labels = frames or [str(k + 1) for k in range(count)]
    total = sum(os.path.getsize(p) for _, imgs in versions for p in imgs)
    print(t("start", len(versions), count, total / 1048576))

    # the slow.pics-compatible staged upload: the comparison first, then one image per request
    fields = [("collectionName", title)]
    for f, label in enumerate(labels):
        fields.append((f"comparisons[{f}].name", label))
        for v, (name, _) in enumerate(versions):
            fields.append((f"comparisons[{f}].imageNames[{v}]", name))
    for v, text in infos.items():
        fields.append((f"info[{v}]", text))
    _, made = post("/upload/comparison", fields)
    collection, slots = made["collectionUuid"], made["images"]

    jobs = []
    for f in range(count):
        for v, (_, imgs) in enumerate(versions):
            jobs.append((slots[f][v], imgs[f]))
    sent, url = 0, None

    def send(job):
        image_id, path = job
        with open(path, "rb") as fh:
            data = fh.read()
        extra = []
        sidecar = os.path.splitext(path)[0] + ".json"
        if os.path.isfile(sidecar):
            with open(sidecar, "r", encoding="utf-8") as fh:
                extra.append(("metrics", fh.read()))
        return post(f"/upload/image/{image_id}", [("collectionUuid", collection), ("imageUuid", image_id)] + extra,
                    [("file", os.path.basename(path), data)])

    with ThreadPoolExecutor(max_workers=4) as pool:
        for _, answer in pool.map(send, jobs):
            sent += 1
            print(t("sent", sent, len(jobs)), end="", flush=True)
            if answer.get("done") and answer.get("url"):
                url = answer["url"]
    print()
    if not url:
        url = made.get("url") or f"{BASE}/c/{made['key']}"
        print(t("notdone", url))
    print(url)

    log = os.path.abspath("gatos_comp.txt")
    new = not os.path.exists(log)
    with open(log, "a", encoding="utf-8") as fh:
        if new:
            os.chmod(log, 0o600)
        fh.write(f"{datetime.now().isoformat(timespec='seconds')}\t{url}\tdelete_key={made.get('delete_key', '')}\t{title}\n")
    print(t("saved", log))


if __name__ == "__main__":
    main()
