#!python
""" Command line interface for enginecore """
# pylint: disable=C0103

import argparse
import sys

import neo4j.exceptions as db_error

from enginecore.cli.status import status_command
from enginecore.cli.power import power_command
from enginecore.cli.thermal import thermal_command
from enginecore.cli.configure_state import configure_command
from enginecore.cli.model import model_command
from enginecore.cli.storage import storage_command


################ Define Command line options & arguments

## set-up top level params
argparser = argparse.ArgumentParser(
    description='Simengine CLI provides a set of management tools for the engine core'
)
subparsers = argparser.add_subparsers()
argparser.add_argument('--version', action='version', version='%(prog)s 2.0')

## set-up the commands (status/power etc..)
status_command(
    subparsers.add_parser('status', help="Retrieve status of registered asset(s)")
)
power_command(
    subparsers.add_parser('power', help="Control power component of registered asset(s)")
)
thermal_command(
    subparsers.add_parser('thermal', help="Manage temperature/thermal settings of the system")
)
storage_command(
    subparsers.add_parser('storage', help="Manage storage state of the system")
)
configure_command(
    subparsers.add_parser('configure-state', help="Update runtime state of the assets/sensors")
)
model_command(
    subparsers.add_parser('model', help="Manage system model: create new/update existing asset etc.")
)

try:
    options = argparser.parse_args()
    
    # if validation is present
    if hasattr(options, 'validate'):
        options.validate(vars(options))
    # action associated with the CLI command
    if hasattr(options, 'func'):
        options.func(vars(options))
    else:
        argparser.print_help()
except db_error.ConstraintError as e:
    print('Database constraint was violated: ')
    print(e, file=sys.stderr)
except argparse.ArgumentTypeError as e:
    print(e, file=sys.stderr)
