#!/usr/bin/env python
""" nf-core: Helper tools for use with nf-core Nextflow pipelines. """

from __future__ import print_function

import click
import sys
import os

import nf_core
import nf_core.lint, nf_core.list, nf_core.download, nf_core.release

import logging

@click.group()
@click.version_option(nf_core.__version__)
@click.option(
    '-v', '--verbose',
    is_flag = True,
    help = "Verbose output (print debug statements)"
)
def nf_core_cli(verbose):
    if verbose:
        logging.basicConfig(level=logging.DEBUG, format="\n%(levelname)s: %(message)s")
    else:
        logging.basicConfig(level=logging.INFO, format="\n%(levelname)s: %(message)s")

@nf_core_cli.command()
@click.argument(
    'pipeline_dir',
    type = click.Path(exists=True),
    required = True,
    metavar = "<pipeline directory>"
)
@click.option(
    '--release',
    is_flag = True,
    default = os.environ.get('TRAVIS_BRANCH') == 'master' and os.environ.get('TRAVIS_REPO_SLUG', '').startswith('nf-core/'),
    help = "Execute additional checks for release-ready workflows."
)
def lint(pipeline_dir, release):
    """ Check pipeline against nf-core guidelines """

    # Run the lint tests!
    lint_obj = nf_core.lint.run_linting(pipeline_dir, release)
    if len(lint_obj.failed) > 0:
        sys.exit(1)

@nf_core_cli.command()
@click.option(
    '--json',
    is_flag = True,
    default = False,
    help = "Print full output as JSON"
)
def list(json):
    """ List nf-core pipelines with local info """
    nf_core.list.list_workflows(json)

@nf_core_cli.command()
@click.argument(
    'pipeline',
    required = True,
    metavar = "<pipeline name>"
)
@click.option(
    '-r', '--release',
    type = str,
    help = "Pipeline release"
)
@click.option(
    '-s', '--singularity',
    is_flag = True,
    default = False,
    help = "Download singularity containers"
)
@click.option(
    '-o', '--outdir',
    type = str,
    help = "Output directory"
)
def download(pipeline, release, singularity, outdir):
    """ Download a pipeline and singularity container """
    dl = nf_core.download.DownloadWorkflow(pipeline, release, singularity, outdir)
    dl.download_workflow()

@nf_core_cli.command()
@click.argument(
    'pipeline_dir',
    type = click.Path(exists=True),
    required = True,
    metavar = "<pipeline directory>"
)
@click.argument(
    'new_version',
    required = True,
    metavar = "<new version number>"
)
def release(pipeline_dir, new_version):
    """ Update nf-core pipeline version number """

    # First, lint the pipeline to check everything is in order
    logging.info("Running nf-core lint tests")
    lint_obj = nf_core.lint.run_linting(pipeline_dir, False)

    # Bump the version number in relevant files
    nf_core.release.make_release(lint_obj, new_version)

if __name__ == '__main__':
    print("""
                                          ,--./,-.
          ___     __   __   __   ___     /,-._.--~\\
    |\ | |__  __ /  ` /  \ |__) |__         }  {
    | \| |       \__, \__/ |  \ |___     \`-._,-`-,
                                          `._,._,'
    """, file=sys.stderr)
    nf_core_cli()
