import click
from arbol.arbol import aprint, asection

from dexp.cli.defaults import DEFAULT_CLEVEL, DEFAULT_CODEC, DEFAULT_STORE
from dexp.cli.parsing import _get_output_path, _parse_channels, _parse_chunks
from dexp.datasets.open_dataset import glob_datasets
from dexp.datasets.operations.crop import dataset_crop


@click.command()
@click.argument("input_paths", nargs=-1)  # ,  help='input path'
@click.option("--output_path", "-o")  # , help='output path'
@click.option("--channels", "-c", default=None, help="List of channels, all channels when ommited.")
@click.option(
    "--quantile",
    "-q",
    default=0.99,
    type=float,
    help="Quantile parameter for lower bound of brightness for thresholding.",
    show_default=True,
)
@click.option(
    "--reference-channel",
    "-rc",
    default=None,
    help="Reference channel to estimate cropping. If no provided it picks the first one.",
)
@click.option("--store", "-st", default=DEFAULT_STORE, help="Zarr store: ‘dir’, ‘ndir’, or ‘zip’", show_default=True)
@click.option("--chunks", "-chk", default=None, help="Dataset chunks dimensions, e.g. (1, 126, 512, 512).")
@click.option(
    "--codec",
    "-z",
    default=DEFAULT_CODEC,
    help="Compression codec: zstd for ’, ‘blosclz’, ‘lz4’, ‘lz4hc’, ‘zlib’ or ‘snappy’ ",
    show_default=True,
)
@click.option("--clevel", "-l", type=int, default=DEFAULT_CLEVEL, help="Compression level", show_default=True)
@click.option("--overwrite", "-w", is_flag=True, help="Forces overwrite of target", show_default=True)
@click.option(
    "--workers",
    "-wk",
    default=-4,
    help="Number of worker threads to spawn. Negative numbers n correspond to: number_of _cores / |n| ",
    show_default=True,
)  #
@click.option("--check", "-ck", default=True, help="Checking integrity of written file.", show_default=True)  #
def crop(
    input_paths,
    output_path,
    channels,
    quantile,
    reference_channel,
    store,
    chunks,
    codec,
    clevel,
    overwrite,
    workers,
    check,
):

    input_dataset, input_paths = glob_datasets(input_paths)
    output_path = _get_output_path(input_paths[0], output_path, "_crop")
    channels = _parse_channels(input_dataset, channels)
    if reference_channel is None:
        reference_channel = input_dataset.channels()[0]
    chunks = _parse_chunks(chunks)

    with asection(
        f"Cropping from: {input_paths} to {output_path} for channels: {channels}, "
        f"using channel {reference_channel} as a reference."
    ):

        dataset_crop(
            input_dataset,
            output_path,
            channels=channels,
            reference_channel=reference_channel,
            quantile=quantile,
            store=store,
            chunks=chunks,
            compression=codec,
            compression_level=clevel,
            overwrite=overwrite,
            workers=workers,
            check=check,
        )

        input_dataset.close()
        aprint("Done!")

[((10, 1, 10, 16), 'click.command', 'click.command', ({}, {}), '()', False, 'import click\n'), ((11, 1, 11, 40), 'click.argument', 'click.argument', (), '', False, 'import click\n'), ((12, 1, 12, 36), 'click.option', 'click.option', ({(12, 14, 12, 29): '"""--output_path"""', (12, 31, 12, 35): '"""-o"""'}, {}), "('--output_path', '-o')", False, 'import click\n'), ((13, 1, 13, 100), 'click.option', 'click.option', (), '', False, 'import click\n'), ((14, 1, 21, 1), 'click.option', 'click.option', (), '', False, 'import click\n'), ((22, 1, 27, 1), 'click.option', 'click.option', (), '', False, 'import click\n'), ((28, 1, 28, 129), 'click.option', 'click.option', (), '', False, 'import click\n'), ((29, 1, 29, 107), 'click.option', 'click.option', (), '', False, 'import click\n'), ((30, 1, 36, 1), 'click.option', 'click.option', (), '', False, 'import click\n'), ((37, 1, 37, 110), 'click.option', 'click.option', (), '', False, 'import click\n'), ((38, 1, 38, 102), 'click.option', 'click.option', (), '', False, 'import click\n'), ((39, 1, 45, 1), 'click.option', 'click.option', (), '', False, 'import click\n'), ((46, 1, 46, 108), 'click.option', 'click.option', (), '', False, 'import click\n'), ((62, 33, 62, 59), 'dexp.datasets.open_dataset.glob_datasets', 'glob_datasets', ({(62, 47, 62, 58): 'input_paths'}, {}), '(input_paths)', False, 'from dexp.datasets.open_dataset import glob_datasets\n'), ((63, 18, 63, 72), 'dexp.cli.parsing._get_output_path', '_get_output_path', ({(63, 35, 63, 49): 'input_paths[0]', (63, 51, 63, 62): 'output_path', (63, 64, 63, 71): '"""_crop"""'}, {}), "(input_paths[0], output_path, '_crop')", False, 'from dexp.cli.parsing import _get_output_path, _parse_channels, _parse_chunks\n'), ((64, 15, 64, 55), 'dexp.cli.parsing._parse_channels', '_parse_channels', ({(64, 31, 64, 44): 'input_dataset', (64, 46, 64, 54): 'channels'}, {}), '(input_dataset, channels)', False, 'from dexp.cli.parsing import _get_output_path, _parse_channels, _parse_chunks\n'), ((67, 13, 67, 34), 'dexp.cli.parsing._parse_chunks', '_parse_chunks', ({(67, 27, 67, 33): 'chunks'}, {}), '(chunks)', False, 'from dexp.cli.parsing import _get_output_path, _parse_channels, _parse_chunks\n'), ((69, 9, 72, 5), 'arbol.arbol.asection', 'asection', ({(70, 8, 71, 60): 'f"""Cropping from: {input_paths} to {output_path} for channels: {channels}, using channel {reference_channel} as a reference."""'}, {}), "(\n    f'Cropping from: {input_paths} to {output_path} for channels: {channels}, using channel {reference_channel} as a reference.'\n    )", False, 'from arbol.arbol import aprint, asection\n'), ((74, 8, 87, 9), 'dexp.datasets.operations.crop.dataset_crop', 'dataset_crop', (), '', False, 'from dexp.datasets.operations.crop import dataset_crop\n'), ((90, 8, 90, 23), 'arbol.arbol.aprint', 'aprint', ({(90, 15, 90, 22): '"""Done!"""'}, {}), "('Done!')", False, 'from arbol.arbol import aprint, asection\n')]