from django.shortcuts import render
from backend.mongodb import db
from exfo.lib import Exfo, Mikrotik
from pprint import pprint

from ttp import ttp
from django.http import JsonResponse, HttpResponse
from datetime import datetime
from celery import shared_task

exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
exfo.login()

mkt = Mikrotik()

def index(request):
    collection  = db['mascot']
    mascot_details = collection.find({})

    rapi = exfo.list_api()
    sla = exfo.call_api("sla")
   
    data_to_parse = """
    interface Loopback0
     description Router-id-loopback
     ip address 192.168.0.113/24
    !
    interface Vlan778
     description CPE_Acces_Vlan
     ip address 2002::fd37/124
     ip vrf CPE1
    !
    """

    ttp_template = """
    interface {{ interface }}
     ip address {{ ip }}/{{ mask }}
     description {{ description }}
     ip vrf {{ vrf }}
    """

    template = """
<input load="text">
interface Loopback0
 description Router-id-loopback
 ip address 192.168.0.113/24
!
interface Loopback1
 description Router-id-loopback
 ip address 192.168.0.1/24
!
interface Vlan778
 ip address 2002::fd37/124
 ip vrf CPE1
!
interface Vlan779
 ip address 2002::bbcd/124
 ip vrf CPE2
!
</input>

<group name="loopbacks_new**.{{ interface }}">
interface {{ interface | contains("Loop") }}
 ip address {{ ip }}/{{ mask }}
 description {{ description }}
 ip vrf {{ vrf }}
</group>

<group name="vlans*">
interface {{ interface | contains("Vlan") }}
 ip address {{ ip }}/{{ mask }}
 description {{ description }}
 ip vrf {{ vrf }}
</group>
    """

    parser = ttp(template=template)
    parser.parse()
    # form excel table and save in file
    parser.result(
        format="excel",
        filename="excel_out_test_excel_formatter_update.xlsx",
        returner="file",
        update=True,
        url="./Output/",
        table=[
            {
                "headers": ["interface", "ip", "mask", "vrf", "description"],
                "path": "loopbacks_new",
                "key": "interface",
                "tab_name": "loopbacks_new",
            },
            {"path": "vlans"},
        ],
    )
    # create parser object and parse data using template:
    # parser = ttp(data=data_to_parse, template=ttp_template)
    # parser.parse()

    # # print result in JSON format
    # results = parser.result(format='xlsx')[0]
    # pprint(results)
    try:
        mk_ips = mkt.call_remote("ip/route") 
        mk_address = mkt.call_remote("ip/address") 
    except:
        mk_ips = []
        mk_address = []

    return render(request, 'backend/index.html', {'objs': mascot_details, 'output': rapi.json(),\
            'sla': sla.json(), 'mk_ips': mk_ips, 'mk_address': mk_address})


#Define Collection
def remote(request):
    cmd = request.GET.get('cmd', None)
    # exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
    # exfo.login()
    r = exfo.call_remote_api(cmd)
    pprint(r.json())
    # return JsonResponse(r.json())
    return render(request, 'backend/remote_render.html', {'res': r})

def service_status(request):
    cmd = request.GET.get('cmd', None)
    import urllib.parse
    section = request.GET.get('section', 'all')
    # exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
    # exfo.login()
    cmd = urllib.parse.unquote(cmd)
    pprint(f" cmd = {cmd}")
    r = exfo.call_remote_api(cmd)
    pprint(r.json())
    # return JsonResponse(r.json())
    return render(request, 'backend/service_status.html', {'res': r, 'section': section})

def reports(request):
    days = range(1,31)
    sla = exfo.call_api("sla")
    sla_json = sla.json()
    sla_results = {}
    test_status = {}
    service_results = {}
    for r in sla_json['result']:
        sla_uri = r['ids']['sla_uri']
        t = exfo.call_remote_api(sla_uri).json()
        sla_results[r['ids']['sla_name']] = t
        t = exfo.call_remote_api(sla_uri).json()
        service_results[r['ids']['sla_name']] = []
        for si in t['result']['service_instances']:
            service_results[r['ids']['sla_name']].append(exfo.call_remote_api(si['service_uri']).json()['result'])
        try:
            t2 = exfo.call_remote_api(t['result']['tests'][-1]['test_status_uri']).json()
            test_status[r['ids']['sla_name']] = t2
        except:
            test_status[r['ids']['sla_name']] = {}
    return render(request, 'backend/reports2.html', {'days': days, 'sla': sla.json(), 'sla_results': sla_results, 'test_status': test_status, 'service_results': service_results })

def dump_api(request):
    from bson.json_util import dumps
    from bson.json_util import loads
    col  = db['exfo_api']
    # col.delete_many({})
    sla = exfo.call_api("sla")
    sla_json = sla.json()
    temp = sla_json['result']
    for t in temp:
        sla_uri = t['ids']['sla_uri']
        sla_name = t['ids']['sla_name']
        r = exfo.call_remote_api(sla_uri).json()
        t['sla_uri_result'] = r['result']
        service_result = []
        for si in r['result']['service_instances']:
            c = exfo.call_remote_api(si['service_uri']).json()['result']
            service_result.append(c)

        test_instance_class_result = []
        test_status_result = []

        for si in r['result']['tests']:
            c = exfo.call_remote_api(si['test_instance_class_uri']).json()['result']
            service = c['service']
            target = None
            if 'target' in c:
                target = c['target']
            test_instance_class_result.append(c)
            c = exfo.call_remote_api(si['test_status_uri']).json()['result']
            test_status_result.append({'sla_name':sla_name, 'test_instance_id': si['test_instance_id'],\
                                    'service': service, 'target': target, \
                                    'type_type_name':  si['test_type_name'],\
                                    'test_instance_class_id': si['test_instance_class_id']  , 'status': c, })

        t['test_instance_class_result'] = test_instance_class_result
        t['test_status_result'] = test_status_result
        t['created'] = datetime.utcnow()
        col.insert_one(t)

    # pprint(temp)
    #col.insert_many(temp)
    results = col.find({})
    data = dumps(list(results), indent=4)
    return HttpResponse(data, content_type='application/json')


@shared_task
def dump_api_task():
    from bson.json_util import dumps
    from bson.json_util import loads
    col  = db['exfo_api']
    # col.delete_many({})
    sla = exfo.call_api("sla")
    sla_json = sla.json()
    temp = sla_json['result']
    for t in temp:
        sla_uri = t['ids']['sla_uri']
        sla_name = t['ids']['sla_name']
        r = exfo.call_remote_api(sla_uri).json()
        t['sla_uri_result'] = r['result']
        service_result = []
        for si in r['result']['service_instances']:
            c = exfo.call_remote_api(si['service_uri']).json()['result']
            service_result.append(c)

        test_instance_class_result = []
        test_status_result = []

        for si in r['result']['tests']:
            c = exfo.call_remote_api(si['test_instance_class_uri']).json()['result']
            service = c['service']
            target = None
            if 'target' in c:
                target = c['target']
            test_instance_class_result.append(c)
            c = exfo.call_remote_api(si['test_status_uri']).json()['result']
            test_status_result.append({'sla_name':sla_name, 'test_instance_id': si['test_instance_id'],\
                                    'service': service, 'target': target, \
                                    'type_type_name':  si['test_type_name'],\
                                    'test_instance_class_id': si['test_instance_class_id']  , 'status': c, })

        t['test_instance_class_result'] = test_instance_class_result
        t['test_status_result'] = test_status_result
        t['created'] = datetime.utcnow()
        col.insert_one(t)

    pprint("Dump API ... Finished")

def print_table(request):
    return render(request, 'backend/print_table.html')


    
