# -*- coding: utf-8 -*-
import click
import json
from ..utils.spinner import (
    init_spinner,
    start_spinner,
    stop_spinner,
)
from ..utils.print import (
    tbprint,
    eprint,
    oprint,
    opprint,
)


@click.group()
@click.pass_obj
@click.pass_context
def clients(ctx, obj):
    """DNA Center Clients API (version: 1.3.0).

    Wraps the DNA Center Clients API and exposes the API as native Python commands.

    """
    ctx.obj = obj.clients


@clients.command()
@click.option('--timestamp', type=str,
              help='''Epoch time(in milliseconds) when the Client health data is required.''',
              show_default=True)
@click.option('--headers', type=str, help='''Dictionary of HTTP Headers to send with the Request.''',
              default=None,
              show_default=True)
@click.option('-pp', '--pretty_print', type=int, help='''Pretty print indent''',
              default=None,
              show_default=True)
@click.option('--beep', is_flag=True, help='''Spinner beep (on)''')
@click.pass_obj
def get_overall_client_health(obj, pretty_print, beep,
                              timestamp,
                              headers):
    """Returns Overall Client Health information by Client type (Wired and Wireless) for any given point of time.
    """
    spinner = init_spinner(beep=beep)
    start_spinner(spinner)
    try:
        if headers is not None:
            headers = json.loads(headers)
        result = obj.get_overall_client_health(
            timestamp=timestamp,
            headers=headers)
        stop_spinner(spinner)
        opprint(result, indent=pretty_print)
    except Exception as e:
        stop_spinner(spinner)
        tbprint()
        eprint('Error:', e)
        click.Context.exit(-1)


@clients.command()
@click.option('--timestamp', type=str,
              help='''Epoch time(in milliseconds) when the Client health data is required.''',
              show_default=True)
@click.option('--mac_address', type=str,
              help='''MAC Address of the client.''',
              required=True,
              show_default=True)
@click.option('--headers', type=str, help='''Dictionary of HTTP Headers to send with the Request.''',
              default=None,
              show_default=True)
@click.option('-pp', '--pretty_print', type=int, help='''Pretty print indent''',
              default=None,
              show_default=True)
@click.option('--beep', is_flag=True, help='''Spinner beep (on)''')
@click.pass_obj
def get_client_detail(obj, pretty_print, beep,
                      timestamp,
                      mac_address,
                      headers):
    """Returns detailed Client information retrieved by Mac Address for any given point of time. .
    """
    spinner = init_spinner(beep=beep)
    start_spinner(spinner)
    try:
        if headers is not None:
            headers = json.loads(headers)
        result = obj.get_client_detail(
            timestamp=timestamp,
            mac_address=mac_address,
            headers=headers)
        stop_spinner(spinner)
        opprint(result, indent=pretty_print)
    except Exception as e:
        stop_spinner(spinner)
        tbprint()
        eprint('Error:', e)
        click.Context.exit(-1)
