#!/usr/bin/env python3
import argparse
from pathlib import Path
from subprocess import check_call

from common import DATA_DIR

SOURCE_DIR = DATA_DIR / "images"
TARGET_DIR = DATA_DIR / "ship/data/images"


def format_size(size: int) -> str:
    return f"{size/1024/1024:.02f} MB"


def format_size_change(source_size: int, target_size: int) -> str:
    ratio = target_size / source_size if target_size > 0 else 1.0
    return (
        f"{format_size(source_size)} → {format_size(target_size)}"
        f" ({ratio:.02%})"
    )


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Converts image assets to WEBP format for better file sizes"
    )
    parser.add_argument(
        "quality",
        type=int,
        default=100,
        nargs="?",
        help="output quality (100 for lossless)",
    )
    parser.add_argument(
        "-f",
        "--force",
        action="store_true",
        help="force overwrite existing files",
    )
    return parser.parse_args()


def main() -> None:
    args = parse_args()

    TARGET_DIR.mkdir(parents=True, exist_ok=True)

    total_source_size = 0
    total_target_size = 0

    for source_path in SOURCE_DIR.rglob("*.png"):
        target_path = TARGET_DIR / f"{source_path.stem}.webp"

        print(
            f"Converting {source_path.relative_to(SOURCE_DIR)} "
            f"to {target_path.relative_to(TARGET_DIR)}..."
        )

        if target_path.exists() and not args.force:
            print("    target already exists, skipping")
            continue

        check_call(
            [
                "convert",
                str(source_path),
                "-quality",
                str(args.quality),
                str(target_path),
            ]
        )

        source_size = source_path.stat().st_size
        target_size = target_path.stat().st_size
        total_source_size += source_size
        total_target_size += target_size
        print("    " + format_size_change(source_size, target_size))

    print()
    print("Total:")
    print("    " + format_size_change(total_source_size, total_target_size))


if __name__ == "__main__":
    main()
