#!/usr/bin/env python
import os

import click
from InquirerPy.utils import color_print
from pyfiglet import figlet_format
from termcolor import cprint

from cloudboot.config import reset_root
from cloudboot.enum.ColorCode import ColorCode
from cloudboot.inquiry.auth import ensure_gcloud_credentials
from cloudboot.inquiry.cloud_functions import init_cloud_function, select_and_deploy_function, \
    display_available_functions_templates
from cloudboot.inquiry.project import init_cloud_project
from cloudboot.service.core.config import initialize_cloudboot_project, reload_cache, ensure_gcloud
from cloudboot.service.gcloud.cloud_functions import deploy_functions, list_local_functions


def print_logo():
    cprint(figlet_format('CloudBoot'), color='light_blue', force_color=True, attrs=['bold'])
    color_print([(ColorCode.SUCCESS, 'v0.1.0-beta')])
    color_print([(ColorCode.SUCCESS,
                  'A groovy collection of easy-peasy scripts and templates for Google Cloud serverless computing!')])


@click.group(invoke_without_command=True, context_settings=dict(help_option_names=['-h', '--help']))
def cloudboot():
    context = click.get_current_context()
    if context.invoked_subcommand is None:
        print_logo()
        click.echo(context.command.get_help(context))


@cloudboot.command(help='Build Cloud Bootstrapper directories and cache.')
@click.option('-h', '--help', is_flag=True, help='Display available options.')
def init(help):
    print_logo()
    if help:
        context = click.get_current_context()
        click.echo(context.command.get_help(context))
        exit(0)
    ensure_gcloud()
    ensure_gcloud_credentials()
    initialize_cloudboot_project()
    init_cloud_project()


@cloudboot.command(help='Rebuild/sync Cloud Bootstrapper cache.')
@click.option('-h', '--help', is_flag=True, help='Display available options.')
def refresh(help):
    if help:
        print_logo()
        context = click.get_current_context()
        click.echo(context.command.get_help(context))
        exit(0)
    reload_cache()


@cloudboot.command(help='cloudboot functions --help')
@click.option('-d', '--deploy', is_flag=True, help='Deploy Cloud functions.')
@click.option('-l', '--registry', is_flag=True, help='List Cloud functions.')
@click.option('-n', '--create', is_flag=True, help='Create new Cloud function.')
@click.option('-s', '--seldeploy', is_flag=True, help='Select and deploy a Cloud function.')
@click.option('-t', '--templates', is_flag=True, help='List available templates for the selected runtime.')
@click.option('-h', '--help', is_flag=True, help='Display available options.')
def functions(deploy, registry, create, seldeploy, templates, help):
    if help:
        print_logo()
        context = click.get_current_context()
        click.echo(context.command.get_help(context))
        exit(0)
    if create:
        init_cloud_function()
    elif deploy:
        deploy_functions()
    elif registry:
        list_local_functions()
    elif seldeploy:
        select_and_deploy_function()
    elif templates:
        display_available_functions_templates()
    else:
        color_print([('', 'Try '), (ColorCode.INFO, 'cloudboot functions --help'), ('', ' for more guidance.')])


if __name__ == '__main__':
    current_dir = os.getcwd()
    reset_root(current_dir)
    cloudboot()
