#!/opt/anaconda1anaconda2anaconda3/bin/python
# -*- coding: utf-8
"""Entry point to the interactive interface for pan genomes.

The massage of the data is being taken care of in the interactive module,
and this file implements the bottle callbacks."""

import os
import sys
import argparse

import anvio
import anvio.utils as utils
import anvio.terminal as terminal
import anvio.interactive as interactive
from anvio.bottleroutes import BottleApplication

from anvio.errors import ConfigError, FilesNPathsError, DictIOError, SamplesError, HDF5Error


__author__ = "A. Murat Eren"
__copyright__ = "Copyright 2016, The anvio Project"
__credits__ = []
__license__ = "GPL 3.0"
__version__ = anvio.__version__
__maintainer__ = "A. Murat Eren"
__email__ = "a.murat.eren@gmail.com"


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

# setup the command line user interface
parser = argparse.ArgumentParser(description="Start an anvi'o server to display a pan-genome")

groupA = parser.add_argument_group('INPUT FILES', "Input files from the pangenome analysis.")
groupB = parser.add_argument_group('OPTIONAL INPUTS', "Where the yay factor becomes a reality.")
groupC = parser.add_argument_group('ADDITIONAL STUFF', "Parameters to provide additional layers, views, or layer data.")
groupD = parser.add_argument_group('VISUALS RELATED', "Parameters that give access to various adjustements regarding\
                                                       the interface.")
groupE = parser.add_argument_group('SWEET PARAMS OF CONVENIENCE', "Parameters and flags that are not quite essential (but\
                                                                   nice to have).")
groupF = parser.add_argument_group('SERVER CONFIGURATION', "For power users.")

groupA.add_argument(*anvio.A('pan-db'), **anvio.K('pan-db'))
groupA.add_argument(*anvio.A('genomes-storage'), **anvio.K('genomes-storage', {'required': False}))
groupA.add_argument(*anvio.A('samples-information-db'), **anvio.K('samples-information-db'))
groupB.add_argument(*anvio.A('view-data'), **anvio.K('view-data'))
groupB.add_argument(*anvio.A('tree'), **anvio.K('tree'))

groupC.add_argument(*anvio.A('additional-view'), **anvio.K('additional-view'))
groupC.add_argument(*anvio.A('additional-layers'), **anvio.K('additional-layers'))
groupD.add_argument(*anvio.A('view'), **anvio.K('view'))
groupD.add_argument(*anvio.A('title'), **anvio.K('title'))
groupD.add_argument(*anvio.A('state-autoload'), **anvio.K('state-autoload'))
groupD.add_argument(*anvio.A('collection-autoload'), **anvio.K('collection-autoload'))
groupD.add_argument(*anvio.A('export-svg'), **anvio.K('export-svg'))

groupE.add_argument(*anvio.A('skip-init-functions'), **anvio.K('skip-init-functions'))
groupE.add_argument(*anvio.A('show-views'), **anvio.K('show-views'))
groupE.add_argument(*anvio.A('dry-run'), **anvio.K('dry-run'))
groupE.add_argument(*anvio.A('show-states'), **anvio.K('show-states'))
groupE.add_argument(*anvio.A('list-collections'), **anvio.K('list-collections'))
groupE.add_argument(*anvio.A('skip-auto-ordering'), **anvio.K('skip-auto-ordering'))

groupF.add_argument(*anvio.A('ip-address'), **anvio.K('ip-address'))
groupF.add_argument(*anvio.A('port-number'), **anvio.K('port-number'))
groupF.add_argument(*anvio.A('read-only'), **anvio.K('read-only'))
groupF.add_argument(*anvio.A('server-only'), **anvio.K('server-only'))

args = parser.parse_args()

try:
    args.mode = 'pan'
    d = interactive.InputHandler(args)

    args.port_number = utils.get_port_num(args.port_number, args.ip_address, run=run)
except ConfigError as e:
    print(e)
    sys.exit(-1)
except FilesNPathsError as e:
    print(e)
    sys.exit(-2)
except DictIOError as e:
    print(e)
    sys.exit(-3)
except SamplesError as e:
    print(e)
    sys.exit(-4)
except HDF5Error as e:
    print(e)
    sys.exit(-5)

app = BottleApplication(d, args)
app.run_application(args.ip_address, args.port_number)
