#!/usr/bin/env python
import click
import aws_layers


@click.group()
def cli():
    pass


@click.command(help="Bundles package for deployment.")
@click.argument("filepath", type=click.Path(exists=True), default=".")
def build(filepath):
    click.echo("Building package")
    click.echo(aws_layers.build_layer_zip(working_dir=filepath))


@click.command(help="Deploy layer to aws")
@click.argument("filepath", type=click.Path(exists=True))
@click.option("-d", "--description", default="Empty desc", help="Publish description")
@click.option("--runtime", default="python3.6", help="set python runtime")
@click.option("-p", "--profile")
@click.option("-r", "--region")
def deploy(filepath, description, runtime, profile, region):
    click.echo(
        aws_layers.deploy_layer_zip(
            path_to_zip_file=filepath,
            description=description,
            runtime=runtime,
            aws_profile=profile,
            region=region,
        )
    )


@click.command(help="Download a layer")
@click.argument("layer", type=click.STRING)
@click.option("-v", "--version", "version", type=int)
@click.option("-p", "--profile")
@click.option("-r", "--region")
def download(layer, version, profile, region):
    click.echo(
        aws_layers.download_layer_zip(
            layer_name=layer, version_number=version, aws_profile=profile, region=region
        )
    )


@click.command(
    help="Set comma or space separated layers in the latest version to a function",
    name="set",
)
@click.argument("layer", nargs=-1)
@click.argument("func", nargs=1)
@click.option("-d", "--delete", is_flag=True)
@click.option("-p", "--profile")
@click.option("-r", "--region")
def set_layer(layer, func, delete, profile, region):
    if "," in layer[0][:]:
        layer = tuple(filter(None, layer[0].split(",")))
    click.echo(
        aws_layers.set_layer_to_lambda(
            list(layer), func, delete_old=delete, aws_profile=profile, region=region
        )
    )


@click.command(help="List all layers", name="list")
@click.option("-p", "--profile")
@click.option("-r", "--region")
def list_layers(profile, region):
    layer_list = aws_layers.list_all_layers(aws_profile=profile, region=region)
    if layer_list:
        for layer in layer_list:
            click.echo(layer)
    else:
        click.echo("No layers in this region")


if __name__ == "__main__":
    cli.add_command(build)
    cli.add_command(deploy)
    cli.add_command(download)
    cli.add_command(set_layer)
    cli.add_command(list_layers)
    cli()
