#!/usr/bin/python3 -s
# -*- coding: utf-8 -*-

import argparse
try:
    import simplejson as json
except ImportError:
    import json

from urllib.parse import urljoin
from pybgpranking import BGPRanking
from pyipasnhistory import IPASNHistory
from datetime import date, timedelta


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Run a query against BGP Ranking')
    parser.add_argument('--url', type=str, help='URL of the instance.')
    parser.add_argument('--date', default=date.today().isoformat(), help='Date of the dataset required')

    sub_parsers = parser.add_subparsers(title='Available commands')

    index_query = sub_parsers.add_parser('index')
    index_query.add_argument('--limit', default=100, help='Max number of ASN to get')
    index_query.add_argument('--family', default='v4', help='v4 or v6')
    index_query.set_defaults(which='index')

    simple_query = sub_parsers.add_parser('simple')
    group = simple_query.add_mutually_exclusive_group(required=True)
    group.add_argument('--asn', help='ASN to lookup')
    group.add_argument('--ip', help='IP to lookup')
    simple_query.set_defaults(which='simple')

    args = parser.parse_args()

    if args.url:
        bgpranking = BGPRanking(args.url)
        ipasn = IPASNHistory(urljoin(args.url, 'ipasn_history'))
    else:
        bgpranking = BGPRanking()
        ipasn = IPASNHistory()

    if args.which == 'simple':
        if args.ip:
            response = ipasn.query(args.ip)
            print(json.dumps(response, indent=2))
            if 'response' in response and response['response']:
                asn = response['response'][list(response['response'].keys())[0]]['asn']
        else:
            asn = args.asn

        response = bgpranking.query(asn, date=(date.today() - timedelta(1)).isoformat())
    elif args.which == 'index':
        response = bgpranking.asns_global_ranking(address_family=args.family, limit=args.limit, date=args.date)
    print(json.dumps(response, indent=2))
