import asyncio
import argparse
import json
import sys

from pyebox import EboxClient, REQUESTS_TIMEOUT, PyEboxError, RabbitMQProducer


def _format_output(account, data):
    """Format data to get a readable output"""
    data['account'] = account
    output = ("""Ebox data for account: {d[account]}
Balance
=======
Balance:      {d[balance]:.2f} $
Usage
=====
Usage:      {d[usage]:.2f} %
Before offpeak
==============
Download: {d[before_offpeak_download]:.2f} Gb
Upload:   {d[before_offpeak_upload]:.2f} Gb
Total:    {d[before_offpeak_total]:.2f} Gb
Offpeak
=======
Download: {d[offpeak_download]:.2f} Gb
Upload:   {d[offpeak_upload]:.2f} Gb
Total:    {d[offpeak_total]:.2f} Gb
Total
=====
Download: {d[download]:.2f} Gb
Upload:   {d[upload]:.2f} Gb
Total:    {d[total]:.2f} Gb
Limit:    {d[limit]:.2f} Gb
""")
    print(output.format(d=data))


def main():
    """Main function"""
    parser = argparse.ArgumentParser()
    parser.add_argument('-u', '--username',
                        required=True, help='EBox account')
    parser.add_argument('-p', '--password',
                        required=True, help='Password')
    parser.add_argument('-j', '--json', action='store_true',
                        default=False, help='Json output')
    parser.add_argument('-t', '--timeout',
                        default=REQUESTS_TIMEOUT, help='Request timeout')
    parser.add_argument('-r', '--rabbit-mq', action='store_true',
                        default=False, help='RabbitMq output')
    parser.add_argument('--rabbitmq-user', default="guest", help="username e.g. 'guest'")
    parser.add_argument('--rabbitmq-password', default="guest", help="password e.g. 'pass'")
    parser.add_argument('--rabbitmq-host', default="localhost", help="rabbitMQ host, defaults to localhost")
    parser.add_argument('--rabbitmq-port', type=int, default=5672, help="rabbitMQ port, defaults to 5672")
    args = parser.parse_args()
    client = EboxClient(args.username, args.password, args.timeout)

    loop = asyncio.get_event_loop()
    task = loop.create_task(client.fetch_data())
    try:
        loop.run_until_complete(task)
    except PyEboxError as exp:
        print(exp)
        client.close_session()
        return
    if not client.get_data():
        client.close_session()
        return
    if args.json:
        print(json.dumps(client.get_data()))
    if args.rabbit_mq:
        RabbitMQProducer.publish(args.rabbitmq_user, args.rabbitmq_password, args.rabbitmq_host, args.rabbitmq_port, '', 'ebox-usage-metric', 'ebox-usage-metric', json.dumps(client.get_data()))
    else:
        _format_output(args.username, client.get_data())
    #client.close_session()


if __name__ == '__main__':
    sys.exit(main())