#!python
from typing import Tuple

import click
import booklet_splitter.booklets
import booklet_splitter.volumes
import logging


@click.group()
@click.option("--log", default="INFO", show_default=True, help="Log level at execution")
def cli(log: str):
    logging.basicConfig(level=logging.getLevelName(log))


@cli.command(
    help="For a given pdf, builds booklets to be printed, "
    "folded and eventualy assembled as a book"
)
@click.option(
    "--max_size",
    type=int,
    default=32,
    show_default=True,
    help="Max size for a booklet, must be multiple of 4",
)
@click.option(
    "--targetdir",
    default=".",
    show_default=True,
    help="Directory where the booklets PDF are written",
)
@click.option(
    "--layout",
    type=bool,
    default=True,
    show_default=True,
    help="Only splits your document in booklets",
)
@click.option(
    "--cover",
    type=bool,
    default=True,
    show_default=True,
    help="Adds a page at the very beginning and at the very end, to paste a cover",
)
@click.argument("input_pdf")
def booklets(max_size: int, input_pdf: str, targetdir: str, layout: bool, cover: bool):
    booklet_splitter.booklets.generate_booklets(
        input_pdf, cover, layout, max_size, targetdir
    )


@cli.command(help="Simply splits a large PDF in smaller PDFs")
@click.option(
    "--targetdir",
    default=".",
    show_default=True,
    help="Directory where the booklets PDF are written",
)
@click.argument("input_pdf")
@click.argument("limits", type=int, nargs=-1)
def volumes(targetdir: str, input_pdf: str, limits: Tuple[int]):
    booklet_splitter.volumes.generate_volumes(input_pdf, limits, targetdir)


cli()
