#!/opt/anaconda1anaconda2anaconda3/bin/python
# -*- coding: utf-8

import sys

import anvio
import anvio.terminal as terminal

from anvio.errors import ConfigError, FilesNPathsError
from anvio.workflows.contigs import ContigsDBWorkflow
from anvio.workflows.metagenomics import MetagenomicsWorkflow
from anvio.workflows.pangenomics import PangenomicsWorkflow
from anvio.workflows.metapangenomics import MetaPangenomicsWorkflow
from anvio.workflows.phylogenomics import PhylogenomicsWorkflow


__author__ = "Developers of anvi'o (see AUTHORS.txt)"
__copyright__ = "Copyleft 2015-2018, the Meren Lab (http://merenlab.org/)"
__license__ = "GPL 3.0"
__version__ = anvio.__version__
__maintainer__ = "Alon Shaiber"
__resources__ = [("Tutorial", "http://merenlab.org/2018/07/09/anvio-snakemake-workflows/")]
__tags__ = ["metagenomics", "phylogenomics", "contigs", "pangenomics", "metapangenomics"]
__email__ = "alon.shaiber@gmail.com"


run = terminal.Run()
progress = terminal.Progress()
pp = terminal.pretty_print

workflows_dict = {'contigs': ContigsDBWorkflow,
                  'metagenomics': MetagenomicsWorkflow,
                  'pangenomics': PangenomicsWorkflow,
                  'metapangenomics': MetaPangenomicsWorkflow,
                  'phylogenomics': PhylogenomicsWorkflow}


def main(args):
    if args.list_workflows:
        run.info("Available workflows", ", ".join(list(workflows_dict.keys())))
        sys.exit(0)

    # FIXME: Meren and Alon should discuss these next lines
    # we can't call the snake_file_path from the class so I think
    # we have to do it this way
    if (not args.workflow) or (not args.config_file) and (not args.get_default_config):
        raise ConfigError("You must provide a workflow name AND a config file. You can use --list-workflow\
                           to learn what workflows are available, and you can use --get-default-config\
                           if you need help writing your config file.")

    if args.workflow not in workflows_dict:
        raise ConfigError("anvi'o is not familiar with a workflow %s, you can use --list-workflow to \
                           learn what workflows are available." % args.workflow)

    M = workflows_dict[args.workflow](args)
    M.init()
    M.go()


if __name__ == '__main__':
    run.warning('If you publish results from this workflow, please do not forget to cite snakemake \
                 (doi:10.1038/nmeth.3176)', lc = 'yellow')

    import argparse
    parser = argparse.ArgumentParser(description="")

    groupA = parser.add_argument_group('ESSENTIAL INPUTS', "Things you must provide or this won't work")
    groupA.add_argument(*anvio.A("workflow"), **anvio.K("workflow"))

    groupB = parser.add_argument_group('ADDITIONAL STUFF', "additional stuff")
    groupB.add_argument(*anvio.A("get-default-config"), **anvio.K("get-default-config"))
    groupB.add_argument(*anvio.A("list-workflows"), **anvio.K("list-workflows"))
    groupB.add_argument(*anvio.A("list-dependencies"), **anvio.K("list-dependencies"))
    groupB.add_argument(*anvio.A("config-file"), **anvio.K("config-file"))
    groupB.add_argument(*anvio.A("dry-run"), **anvio.K("dry-run"))
    groupB.add_argument(*anvio.A("save-workflow-graph"), **anvio.K("save-workflow-graph"))
    groupB.add_argument(*anvio.A("additional-params"), **anvio.K("additional-params"))

    args = anvio.get_args(parser)

    try:
        main(args)
    except ConfigError as e:
        print(e)
        sys.exit(-1)
    except FilesNPathsError as e:
        print(e)
        sys.exit(-2)
