__author__ = 'stubbfel'
import sys
sys.path.insert(0, '../../json')
sys.path.insert(0, '../../csv')
import jsonutility as ju
import csvutilies as csv
import glob
from collections import OrderedDict

def exportRV2CSV(files, output):
    resulttable = [["time"]]
    maxRow = 0
    for fileName in files:
        jsonObject = ju.readJSON(fileName)
        jsonObject = OrderedDict(sorted(jsonObject.items(), key=lambda t: float(t[0])))
        column = [fileName]
        for value in jsonObject.values():
            if float(value) >= 0:
                column.append(value)
        resulttable.append(column)

        tmpMaxRow = len(column)
        if tmpMaxRow > maxRow:
            maxRow = tmpMaxRow

    colCount = len(resulttable)
    for i in range(1, maxRow):
        resulttable[0].append(i-1)
        for j in range(1, colCount):
            if i > len(resulttable[j]):
                resulttable[j].append(0)
    print(resulttable)
    csv.writeCSV(output, zip(*resulttable))

def exportRTT2CSV(files, output):
    resulttable = [["msgIndex"]]
    maxRow = 0
    for fileName in files:
        jsonObject = ju.readJSON(fileName)
        jsonObject = OrderedDict(sorted(jsonObject.items(), key=lambda t: float(t[0])))
        column = [fileName]
        for value in jsonObject.values():
            if float(value) >= 0:
                column.append(value)
        resulttable.append(column)

        tmpMaxRow = len(column)
        if tmpMaxRow > maxRow:
            maxRow = tmpMaxRow

    colCount = len(resulttable)
    for i in range(1, maxRow):
        resulttable[0].append(i-1)
        for j in range(1, colCount):
            if i > len(resulttable[j]):
                resulttable[j].append(0)
    print(resulttable)
    csv.writeCSV(output, zip(*resulttable))

def exportLocktime2CSV(files, output, output2):
    resulttable = []
    resulttable2 = []
    for fileName in files:
        jsonObject = ju.readJSON(fileName)
        runTime = float(jsonObject["runtime"])
        locktTime = float(jsonObject["locktime"])
        ratioTime = locktTime/runTime
        resulttable.append([fileName, ratioTime])
        resulttable2.append([fileName, locktTime])

    print(resulttable)
    print(resulttable2)
    csv.writeCSV(output, zip(*resulttable))
    csv.writeCSV(output2, zip(*resulttable2))

def exportRuntime2CSV(files, output):
    resulttable = []

    for fileName in files:
        jsonObject = ju.readJSON(fileName)
        runTime = float(jsonObject["runtime"])
        resulttable.append([fileName, runTime])

    print(resulttable)
    csv.writeCSV(output, zip(*resulttable))

def exportMsgCount2CSV(files, output):
    resulttable = []

    for fileName in files:
        jsonObject = ju.readJSON(fileName)
        msgcount = float(jsonObject["msgcount"])
        resulttable.append([fileName, msgcount])

    print(resulttable)
    csv.writeCSV(output, zip(*resulttable))

def exportRTT2CSV(files, output):
    resulttable = []
    for fileName in files:
        jsonObject = ju.readJSON(fileName)
        rtt = [fileName]
        rtt.extend(jsonObject.values())
        resulttable.append(rtt)

    print(resulttable)
    csv.writeCSV(output, zip(*resulttable))

def exportCNPN2CSV(files, output):
    resulttable = []
    for fileName in files:
        jsonObject = ju.readJSON(fileName)
        lastViewID = "0"

        for key in jsonObject.keys():
            if (csv.is_number(key)) and (int(key) > int(lastViewID)):
                lastViewID = key

        view = jsonObject[lastViewID]
        clusterCount = float(view["cn"])
        peerCount = float(view["pn"])
        cnpn = peerCount / clusterCount
        resulttable.append([fileName, cnpn])
    print(resulttable)
    csv.writeCSV(output, zip(*resulttable))


exportRV2CSV(glob.glob("[0-9][0-9][0-9].json_rv1"),"all.csv")
exportRV2CSV(glob.glob("[0][0-9][0-9].json_rv1"),"0xx.csv")
exportRV2CSV(glob.glob("[1][0-9][0-9].json_rv1"),"1xx.csv")
exportRV2CSV(glob.glob("[2][0-9][0-9].json_rv1"),"2xx.csv")
exportRV2CSV(glob.glob("[2][0][0-9].json_rv1"),"x0x.csv")
exportRV2CSV(glob.glob("[2][1][0-9].json_rv1"),"x1x.csv")
exportRV2CSV(glob.glob("[2][2][0-9].json_rv1"),"x2x.csv")
exportRV2CSV(glob.glob("[0-9][0-9][0].json_rv1"),"xx0.csv")
exportRV2CSV(glob.glob("[0-9][0-9][1].json_rv1"),"xx1.csv")
exportRV2CSV(glob.glob("[0-9][0-9][2].json_rv1"),"xx2.csv")
exportRV2CSV(glob.glob("[0-9][0-9][3].json_rv1"),"xx3.csv")
exportRV2CSV(glob.glob("[0-9][0-9][4].json_rv1"),"xx4.csv")
exportRV2CSV(glob.glob("[0-9][0-9][5].json_rv1"),"xx5.csv")
exportRV2CSV(glob.glob("[0-9][0-9][7].json_rv1"),"xx7.csv")
exportRV2CSV(glob.glob("[2][0][0-9].json_rv1"),"20x.csv")


exportLocktime2CSV(glob.glob("[0-9][0-9][0-9].json"),"all-locktime.csv","all-locktimenn.csv")
exportLocktime2CSV(glob.glob("[0][0-9][0-9].json"),"0xx-locktime.csv","0xx-locktimenn.csv")
exportLocktime2CSV(glob.glob("[1][0-9][0-9].json"),"1xx-locktime.csv","1xx-locktimenn.csv")
exportLocktime2CSV(glob.glob("[2][0-9][0-9].json"),"2xx-locktime.csv","2xx-locktimenn.csv")
exportLocktime2CSV(glob.glob("[0-9][0][0-9].json"),"x0x-locktime.csv","x0x-locktimenn.csv")
exportLocktime2CSV(glob.glob("[0-9][1][0-9].json"),"x1x-locktime.csv","x1x-locktimenn.csv")
exportLocktime2CSV(glob.glob("[0-9][2][0-9].json"),"x2x-locktime.csv","x2x-locktimenn.csv")
exportLocktime2CSV(glob.glob("[0-9][0-9][0].json"),"xx0-locktime.csv","xx0-locktimenn.csv")
exportLocktime2CSV(glob.glob("[0-9][0-9][1].json"),"xx1-locktime.csv","xx1-locktimenn.csv")
exportLocktime2CSV(glob.glob("[0-9][0-9][2].json"),"xx2-locktime.csv","xx2-locktimenn.csv")
exportLocktime2CSV(glob.glob("[0-9][0-9][3].json"),"xx3-locktime.csv","xx3-locktimenn.csv")
exportLocktime2CSV(glob.glob("[0-9][0-9][4].json"),"xx4-locktime.csv","xx4-locktimenn.csv")
exportLocktime2CSV(glob.glob("[0-9][0-9][5].json"),"xx5-locktime.csv","xx5-locktimenn.csv")
exportLocktime2CSV(glob.glob("[0-9][0-9][7].json"),"xx7-locktime.csv","xx7-locktimenn.csv")
exportLocktime2CSV(glob.glob("[2][0][0-9].json"),"20x-locktime.csv","20x-locktimenn.csv")


exportRTT2CSV(glob.glob("[0-9][0-9][0-9].json_rtt"),"all-rtt.csv")
exportRTT2CSV(glob.glob("[0][0-9][0-9].json_rtt"),"0xx-rtt.csv")
exportRTT2CSV(glob.glob("[1][0-9][0-9].json_rtt"),"1xx-rtt.csv")
exportRTT2CSV(glob.glob("[2][0-9][0-9].json_rtt"),"2xx-rtt.csv")
exportRTT2CSV(glob.glob("[0-9][0][0-9].json_rtt"),"x0x-rtt.csv")
exportRTT2CSV(glob.glob("[0-9][1][0-9].json_rtt"),"x1x-rtt.csv")
exportRTT2CSV(glob.glob("[0-9][2][0-9].json_rtt"),"x2x-rtt.csv")
exportRTT2CSV(glob.glob("[0-9][0-9][0].json_rtt"),"xx0-rtt.csv")
exportRTT2CSV(glob.glob("[0-9][0-9][1].json_rtt"),"xx1-rtt.csv")
exportRTT2CSV(glob.glob("[0-9][0-9][2].json_rtt"),"xx2-rtt.csv")
exportRTT2CSV(glob.glob("[0-9][0-9][3].json_rtt"),"xx3-rtt.csv")
exportRTT2CSV(glob.glob("[0-9][0-9][4].json_rtt"),"xx4-rtt.csv")
exportRTT2CSV(glob.glob("[0-9][0-9][5].json_rtt"),"xx5-rtt.csv")
exportRTT2CSV(glob.glob("[0-9][0-9][7].json_rtt"),"xx7-rtt.csv")
exportRTT2CSV(glob.glob("[2][0][0-9].json_rtt"),"20x-rtt.csv")


exportCNPN2CSV(glob.glob("[0-9][0-9][0-9].json"),"all-cnpn.csv")
exportCNPN2CSV(glob.glob("[0][0-9][0-9].json"),"0xx-cnpn.csv")
exportCNPN2CSV(glob.glob("[1][0-9][0-9].json"),"1xx-cnpn.csv")
exportCNPN2CSV(glob.glob("[2][0-9][0-9].json"),"2xx-cnpn.csv")
exportCNPN2CSV(glob.glob("[0-9][0][0-9].json"),"x0x-cnpn.csv")
exportCNPN2CSV(glob.glob("[0-9][1][0-9].json"),"x1x-cnpn.csv")
exportCNPN2CSV(glob.glob("[0-9][2][0-9].json"),"x2x-cnpn.csv")
exportCNPN2CSV(glob.glob("[0-9][0-9][0].json"),"xx0-cnpn.csv")
exportCNPN2CSV(glob.glob("[0-9][0-9][1].json"),"xx1-cnpn.csv")
exportCNPN2CSV(glob.glob("[0-9][0-9][2].json"),"xx2-cnpn.csv")
exportCNPN2CSV(glob.glob("[0-9][0-9][3].json"),"xx3-cnpn.csv")
exportCNPN2CSV(glob.glob("[0-9][0-9][4].json"),"xx4-cnpn.csv")
exportCNPN2CSV(glob.glob("[0-9][0-9][5].json"),"xx5-cnpn.csv")
exportCNPN2CSV(glob.glob("[0-9][0-9][7].json"),"xx7-cnpn.csv")
exportCNPN2CSV(glob.glob("[2][0][0-9].json"),"20x-cnpn.csv")

exportMsgCount2CSV(glob.glob("[0-9][0-9][0-9].json"),"all-mgs.csv")
exportMsgCount2CSV(glob.glob("[0][0-9][0-9].json"),"0xx-mgs.csv")
exportMsgCount2CSV(glob.glob("[1][0-9][0-9].json"),"1xx-mgs.csv")
exportMsgCount2CSV(glob.glob("[2][0-9][0-9].json"),"2xx-mgs.csv")
exportMsgCount2CSV(glob.glob("[0-9][0][0-9].json"),"x0x-mgs.csv")
exportMsgCount2CSV(glob.glob("[0-9][1][0-9].json"),"x1x-mgs.csv")
exportMsgCount2CSV(glob.glob("[0-9][2][0-9].json"),"x2x-mgs.csv")
exportMsgCount2CSV(glob.glob("[0-9][0-9][0].json"),"xx0-mgs.csv")
exportMsgCount2CSV(glob.glob("[0-9][0-9][1].json"),"xx1-mgs.csv")
exportMsgCount2CSV(glob.glob("[0-9][0-9][2].json"),"xx2-mgs.csv")
exportMsgCount2CSV(glob.glob("[0-9][0-9][3].json"),"xx3-mgs.csv")
exportMsgCount2CSV(glob.glob("[0-9][0-9][4].json"),"xx4-mgs.csv")
exportMsgCount2CSV(glob.glob("[0-9][0-9][5].json"),"xx5-mgs.csv")
exportMsgCount2CSV(glob.glob("[0-9][0-9][7].json"),"xx7-mgs.csv")
exportMsgCount2CSV(glob.glob("[2][0][0-9].json"),"20x-mgs.csv")

exportRuntime2CSV(glob.glob("[0-9][0-9][0-9].json"),"all-runtime.csv")
exportRuntime2CSV(glob.glob("[0][0-9][0-9].json"),"0xx-runtime.csv")
exportRuntime2CSV(glob.glob("[1][0-9][0-9].json"),"1xx-runtime.csv")
exportRuntime2CSV(glob.glob("[2][0-9][0-9].json"),"2xx-runtime.csv")
exportRuntime2CSV(glob.glob("[0-9][0][0-9].json"),"x0x-runtime.csv")
exportRuntime2CSV(glob.glob("[0-9][1][0-9].json"),"x1x-runtime.csv")
exportRuntime2CSV(glob.glob("[0-9][2][0-9].json"),"x2x-runtime.csv")
exportRuntime2CSV(glob.glob("[0-9][0-9][0].json"),"xx0-runtime.csv")
exportRuntime2CSV(glob.glob("[0-9][0-9][1].json"),"xx1-runtime.csv")
exportRuntime2CSV(glob.glob("[0-9][0-9][2].json"),"xx2-runtime.csv")
exportRuntime2CSV(glob.glob("[0-9][0-9][3].json"),"xx3-runtime.csv")
exportRuntime2CSV(glob.glob("[0-9][0-9][4].json"),"xx4-runtime.csv")
exportRuntime2CSV(glob.glob("[0-9][0-9][5].json"),"xx5-runtime.csv")
exportRuntime2CSV(glob.glob("[0-9][0-9][7].json"),"xx7-runtime.csv")
exportRuntime2CSV(glob.glob("[2][0][0-9].json"),"20x-runtime.csv")