#!/usr/bin/env python

"""
Use cases

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

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

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

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

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

import click
import os.path

from codev.configuration import YAMLConfigurationWriter
from codev.cli import command, main


@main.command(
    help='Create .codev file with basic settings in current directory or in given file path.',
    short_help='Create .codev file with basic settings.'
              )
# TODO ?Merge with configuration option?
@click.option('-p', '--path', default='.codev', help='Path to configuration file.')
def init(path):
    """
    :param path:
    :type path: str
    :return:
    :rtype: bool
    """
    if os.path.isdir(path):
        path = os.path.join(path, '.codev')

    if os.path.exists(path):
        if not click.confirm("A file '%(filepath)s' already exists. Overwrite?" % {'filepath': path}):
            raise click.Abort()

    try:
        YAMLConfigurationWriter().save_to_file(path)
        return True
    except Exception as e:
        raise click.ClickException(e)


@command(
    help='Install project',
    confirmation="Install project '{project}' to '{environment}' environment with infrastructure '{infrastructure}' at installation '{installation}'?",
    perform=True
)
def install(deployment, perform):
    """
    Install project

    :param deployment:
    :type deployment: codev.deployment.Deployment
    :param perform:
    :type perform: bool
    :return:
    :rtype: bool
    """
    if perform:
        return deployment.provision()
    else:
        return deployment.install()


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


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


@command(help='Stop the running command in isolation')
def stop(deployment):
    """
    :param deployment:
    :type deployment: codev.deployment.Deployment
    :return:
    :rtype: bool
    """
    return deployment.stop()


@command(help='Kill the running command in isolation')
def kill(deployment):
    """
    :param deployment:
    :type deployment: codev.deployment.Deployment
    :return:
    :rtype: bool
    """
    return deployment.kill()


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


@command(
    help='Run the script in isolation',
    confirmation="Execute script '{script}' from project '{project}' in '{environment}' environment with infrastructure '{infrastructure}' at installation '{installation}'?"
)
@click.argument('script')
def run(deployment, script):
    """
    :param deployment:
    :type deployment: codev.deployment.Deployment
    :param script:
    :return:
    :rtype: bool
    """
    deployment.run(script)


@command(
    help='Destroy isolation.',
    confirmation="Destroy isolation of project '{project}' in '{environment}' environment with infrastructure '{infrastructure}' at installation '{installation}'?"
)
def destroy(deployment):
    """
    :param deployment:
    :type deployment: codev.deployment.Deployment
    :return:
    :rtype: bool
    """
    deployment.destroy()


if __name__ == "__main__":
    main()

