# -*- 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: 2.2.1).

    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('--username', type=str,
              help='''Wireless client username for which proximity information is required.''',
              required=True,
              show_default=True)
@click.option('--number_days', type=int,
              help='''Number of days to track proximity until current date. Defaults and maximum up to 14 days.''',
              show_default=True)
@click.option('--time_resolution', type=int,
              help='''Time interval (in minutes) to measure proximity. Defaults to 15 minutes with a minimum 5 minutes.''',
              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 client_proximity(obj, pretty_print, beep,
                     username,
                     number_days,
                     time_resolution,
                     headers):
    """This intent API will provide client proximity information for a specific wireless user. Proximity is defined as presence on the same floor at the same time as the specified wireless user. The Proximity workflow requires the subscription to the following event (via the Event Notification workflow) prior to making this API call: NETWORK-CLIENTS-3-506 - Client Proximity Report.
    """
    spinner = init_spinner(beep=beep)
    start_spinner(spinner)
    try:
        if headers is not None:
            headers = json.loads(headers)
        result = obj.client_proximity(
            username=username,
            number_days=number_days,
            time_resolution=time_resolution,
            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('--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_enrichment_details(obj, pretty_print, beep,
                                  headers):
    """Enriches a given network End User context (a network user-id or end user’s device Mac Address) with details about the user, the devices that the user is connected to and the assurance issues that the user is impacted by.
    """
    spinner = init_spinner(beep=beep)
    start_spinner(spinner)
    try:
        if headers is not None:
            headers = json.loads(headers)
        result = obj.get_client_enrichment_details(
            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)
