import click, terminaltables
from terminaltables import AsciiTable
from .api_resources import gravitee_api
from ...exeptions import GraviteeRequestError

@click.group()
@click.pass_context
def api(ctx):
        ctx.obj = gravitee_api()

@click.command()
@click.option('--deploy-state', help='show if API configuration is synchronized', is_flag=True)
@click.pass_obj
def ps(gravitee_api, deploy_state):
        """APIs list"""
        resp =  gravitee_api.get_apis()
        
        if not resp:
                click.echo("No Api(s) found ")
        else:

                data = []

                if deploy_state:
                        data.append(['id', 'Name', 'Synchronized', 'Status'])
                else:
                        data.append(['id', 'Name', 'Status'])

                for api_item in resp.json():
                        if api_item['state'] == 'started':
                                state_color = 'green'
                        else:
                                state_color = 'red'

                        if 'workflow_state' in api_item:
                                state = click.style(api_item['workflow_state'].upper(), fg='blue')  + "-" + click.style(api_item['state'].upper(), fg=state_color)
                        else:
                                state = click.style(api_item['state'].upper(), fg=state_color)
                        
                        if deploy_state:
                                response_state = gravitee_api.state_api(api_item['id'])
                                synchronized = click.style("X", fg='yellow')
                                if response_state.json()["is_synchronized"]:
                                        synchronized = click.style("V", fg='green')
                                
                                data.append([api_item['id'], api_item['name'], synchronized, state])
                        else:

                                if api_item['state'] == 'started':
                                        color = 'green'
                                else:
                                        color = 'red'
                                data.append([api_item['id'], api_item['name'], state])

                table = AsciiTable(data)
                table.inner_footing_row_border = False
                table.inner_row_border = False
                table.inner_column_border  = False
                table.outer_border = False
                if deploy_state:
                        table.justify_columns[2] = 'center'
                click.echo(table.table)

@click.command()
@click.argument('api_id', required = True)
@click.pass_obj
def start(gravitee_api, api_id):
        """start api"""
        resp = gravitee_api.start_api(api_id)
        click.echo("API {} is started".format(api_id))

@click.command()
@click.argument('api_id', required = True)
@click.pass_obj
def stop(gravitee_api, api_id):
        """stop api"""
        resp = gravitee_api.stop_api(api_id)
        click.echo("API {} is stopped".format(api_id))

@click.command()
@click.argument('api_id', required = True)
@click.pass_obj
def deploy(gravitee_api, api_id):
        """deploy api configuration"""
        resp = gravitee_api.deploy_api(api_id)
        click.echo("API {} is deployed".format(api_id))


api.add_command(ps)
api.add_command(start)
api.add_command(stop)
api.add_command(deploy)

