#!/usr/local/bin/python3

import argparse
import traceback
from multiprocessing import cpu_count

from wellfare.molecule import Molecule
from wellfare.qmparser import extract_molecular_data
from wellfare.messages import *

########################################################
#                                                      #
# This is where the command line arguments are defined #
#                                                      #
########################################################

parser = argparse.ArgumentParser(
    description="WellFARe: Method for the Incremental Construction and Exploration of the Potential Energy Surface",
    epilog="recognised filetypes: gaussian, orca, turbomole, xyz")
parser.add_argument("-P", "--numproc", type=int,
                    help="number of processes for parallel execution",
                    default="0")
parser.add_argument("-v", "--verbosity", help="increase output verbosity",
                    type=int, choices=[0, 1, 2, 3], default=3)
parser.add_argument("inputfile", metavar='file',
                    help="input file(s) with molecular structure")

args = parser.parse_args()

# Default number of cores for processing is requested with "-p 0" and uses
# all available cores.
if args.numproc == 0:
    args.numproc = cpu_count()


############################################
#                                          #
# The main part of the program starts here #
#                                          #
############################################

def main():
    prg_start_time = time.time()

    # Print GPL v3 statement and program header
    if args.verbosity >= 1:
        msg_program_header("WellFARe-FF", 0.9)

    molecule = Molecule("Input Structure", 0)
    extract_molecular_data(args.inputfile, molecule, verbosity=args.verbosity)

    # Print program footer
    if args.verbosity >= 1:
        msg_program_footer(prg_start_time)


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        msg_program_error("Keyboard interrupt by user")
    except:
        error_txt = "This didn't go according to plan: Tell Matthias!"
        error_txt += "\n\n(Please email the command line arguments you"
        error_txt += " used\ntogether with the files you were analyzing"
        error_txt += " and the\ninformation below (within the"
        error_txt += " exclamation marks)\nto: matthias.lein@gmail.com.\n\n"
        error_txt += "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"

        # Get info about the exception
        exceptiondata = traceback.format_exc().splitlines()
        # Extract from this data: Exception type first, then traceback
        exception_type = exceptiondata[-1]
        exception_line = exceptiondata[-3].split()[3]
        exception_module = exceptiondata[-3].split()[5]
        # Print exception type
        error_txt += "! {}\n".format(exception_type)
        # Print line number and module
        error_txt += "! Line {} in module {}\n".format(exception_line,
                                                     exception_module)
        error_txt += "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
        msg_program_error(error_txt)
