#!/home/beer/projects/baseclue/codev/env/bin/python3.4

"""
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
    click.echo(
        str(info)
    )
    # click.echo(
    #     'Isolation:\n'
    #     '    ident: {isolation.ident}\n'
    #     '    ip: {isolation.ip}\n'.format(
    #         isolation=info['isolation']
    #     )
    # )
    # click.echo('Configuration:')
    # for machine_group_name, machines in info['infrastructure'].items():
    #     click.echo('    Machines group: {name}'.format(name=machine_group_name))
    #     for machine in machines:
    #         machine_connectivity = info['connectivity'].get(machine.ident, {})
    #         click.echo('        {machine.ip} {machine.ident}'.format(machine=machine))
    #         for source, target in machine_connectivity.items():
    #             click.echo('                {source} -> {target}'.format(source=source, target=target))
    return True
    # click.echo(
    #     '{project} {environment} {configuration} {source_transition}'.format(**installation.installation_info())
    # )


if __name__ == "__main__":
    main()

