#!python

"""
Use cases

codev init --template=[template]
    -- create new project

codev install [environment] [configuration] [version]
    -- install project

codev start [environment] [configuration] [version]
    -- start installed project

codev stop [environment] [configuration] [version]
    -- stop installed project

codev run [script] [environment] [configuration] [version]
"""

import click

from codev.cli import command, main
from json import loads
import sys
import select


@command(
    help='Install project.',
    confirmation="Install project '{project}' to environment '{environment}' in configuration '{configuration}' at source '{source_transition}'?"
)
def install(installation):
    """
    Install project

    :param installation:
    :type installation: codev.installation.Installation
    :return:
    :rtype: bool
    """
    return installation.install()


@command(
    help='Deploy project.',
    confirmation="Deploy project '{project}' to environment '{environment}' in configuration '{configuration}' at source '{source_transition}'?"
)
def deploy(installation):
    """
    Install project

    :param installation:
    :type installation: codev.installation.Installation
    :return:
    :rtype: bool
    """
    return installation.deploy()

#
# @command(help='Join the running command in isolation.')
# def join(installation):
#     """
#     :param installation:
#     :type installation: codev.installation.Installation
#     :return:
#     :rtype: bool
#     """
#     return installation.join()


@command(help='Run command in project context in isolation.')
@click.argument('command', nargs=-1)
def run(installation, command):
    """
    :param installation:
    :type installation: codev.installation.Installation
    :param command:
    :type command: str
    :return:
    :rtype: bool
    """

    if select.select([sys.stdin,], [], [], 0.0)[0]:
        arguments = loads(sys.stdin.read())
    else:
        arguments = {}
    return installation.run(' '.join(command), arguments)


# @command(help='Execute command in isolation.')
# @click.argument('command', nargs=-1)
# def execute(installation, command):
#     """
#     :param installation:
#     :type installation: codev.installation.Installation
#     :param command:
#     :type command: str
#     :return:
#     :rtype: bool
#     """
#     return installation.execute(' '.join(command))


# @command(help='Stop the running command in isolation.')
# def stop(installation):
#     """
#     :param installation:
#     :type installation: codev.installation.Installation
#     :return:
#     :rtype: bool
#     """
#     return installation.stop()
#
#
# @command(help='Kill the running command in isolation.')
# def kill(installation):
#     """
#     :param installation:
#     :type installation: codev.installation.Installation
#     :return:
#     :rtype: bool
#     """
#     return installation.kill()


# @command(help='Invoke an isolation shell.')
# def shell(installation):
#     """
#     :param installation:
#     :type installation: codev.installation.Installation
#     :return:
#     :rtype: bool
#     """
#     return installation.shell()


@command(
    help='Destroy isolation.',
    confirmation="Destroy isolation of project '{project}' in environment '{environment}' in configuration '{configuration}' at source '{source_transition}'?"
)
def destroy(installation):
    """
    :param installation:
    :type installation: codev.installation.Installation
    :return:
    :rtype: bool
    """
    return installation.destroy()


@command(help='Show info about the installation.')
def info(installation):
    """
    :param installation:
    :type installation: codev.installation.Installation
    :return:
    :rtype: bool
    """
    info = installation.info
    formatter = click.HelpFormatter(indent_increment=4)
    isolation = info.get('isolation', {})
    if isolation:
        formatter.write_text('Isolation:')
        with formatter.indentation():
            formatter.write_text('ident: {ident}'.format(**isolation))
            formatter.write_text('   ip: {ip}'.format(**isolation))

    formatter.write_text('Infrastructure:')
    for machine_group_name, machines in info['infrastructure'].items():
        with formatter.indentation():
            formatter.write_text('{name}'.format(name=machine_group_name))
            for machine in machines:
                with formatter.indentation():
                    formatter.write_text('{ident:<20} {ip}'.format(**machine))
                    if isolation:
                        machine_connectivity = isolation.get('connectivity', {}).get(machine['ident'], {})
                        with formatter.indentation():
                            for source, target in machine_connectivity.items():
                                formatter.write_text('{source} -> {target}'.format(source=source, target=target))
    click.echo(formatter.getvalue())
    return True

# @command(help='Show settings.')
# def settings(installation):
#     pass


if __name__ == "__main__":
    main()

