import click
import platform

from .api import api
from .config import config
from .utils import client
from ..errors.common import AuthenticationError, InvalidConfig
from ..version import version as client_version

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])


@click.group(context_settings=CONTEXT_SETTINGS)
def main():
    pass


@main.command()
@click.option('--system', default='default', help="The NETLAB+ system to get server version from. Leave blank for "
                                                  "'default'.")
def version(system):
    """ Show the client, server, Python, and OS versions"""
    try:
        results = client(system, catch_exceptions=False).system_status_get()
    except (AuthenticationError, InvalidConfig, OSError, ValueError):
        results = {}
    server_version = results.get('sys_sdn_version', click.style("Unable to connect to server", fg='red'))

    click.echo('NETLAB+ Server : {}'.format(server_version))
    click.echo('Python Client  : {}'.format(client_version))
    click.echo('Python Version : {}'.format(platform.python_version()))
    click.echo('OS Platform    : {}'.format(platform.platform()))


# manually setup top level command groups
main.add_command(api)
main.add_command(config)
