#Misc util functions
import json
import requests
import datetime




def currentTime():
    ts = datetime.datetime.now()
    print(ts)
    return ts

def totalNumberOfNodes(inputLayerDict):
    ''' Computes total number of nodes in the inputLayerDict.

    :param Dict{string: Layer} inputLayerDict: Layer in which the Clusters in which the Nodes are stored

    :returns: Returns nr of Nodes
    
    :rtype: int
    '''  
    nodeCount = 0
    for curLayer in inputLayerDict.values():
        for curCluster in curLayer.cluster_Dict.values():
                nodeCount+=len(curCluster.cluster_containedNodesDict.values())
                
    return nodeCount

def totalNumberOfClusters(inputLayerDict):
    ''' Computes total number of clusters in the inputLayerDict.

    :param Dict{string: Layer} inputLayerDict: Layer in which the Clusters are stored

    :returns: Returns nr of Clusters

    :rtype: int
    '''  
    clustCount = 0
    for curLayer in inputLayerDict.values():
            clustCount+= len(curLayer.cluster_Dict.values())
                
    return clustCount

def convertLayerDictToJSON(layerDict, runId):
    ''' Converts a Layer object to JSON format.

    :param Dict{string: Layer} layerDict: Object which contains Data about the Layers, Clusters and Nodes

    :rtype: Dict{string: [Cluster1, Cluster2, ...]}
    '''

    outputJSON = []

    for curLayer in layerDict.values():        
        for curCluster in curLayer.cluster_Dict.values():
            outputJSON.append({
                "cluster_label" : curCluster.cluster_label,
                "cluster_layer" : curCluster.cluster_layer, 
                "cluster_runId" : runId,
                "cluster_connClustDict" : changeTupleDictToDictList(curCluster.cluster_connClustDict),
                "cluster_connNodesDict" : getFrozensetFromConnNodesDict(curCluster.cluster_connNodesDict), #Don
                "cluster_containedNodesDict" : getNodeIdListFromContainedNodesDict(curCluster.cluster_containedNodesDict)
            })

    #outputJSON = json.dumps(outputJSON, default=lambda o: o.__dict__, indent=4)

    return outputJSON

def changeTupleDictToDictList(inputDict):
    '''
    Helper function used to convert the code into JSON format

    '''

    outputList = []
    for tupleKey in inputDict:
        auxDict = dict()
        auxDict["cluster_label"]= tupleKey[0]
        auxDict["cluster_layer"]= tupleKey[1]
        auxDict["connectionWeight"] = inputDict[tupleKey]
        outputList.append(auxDict)            

    return outputList

def getNodeIdListFromContainedNodesDict(inputDict):
    '''
    Helper function used to convert the code into JSON format

    '''
    output = []
    for curNode in inputDict.values():
        output.append(curNode.uniqueID)
    return output

def getFrozensetFromConnNodesDict(inputDict):
    '''
    Helper function used to convert the code into JSON format

    '''
    output = []
    
    for curNode in inputDict.values(): 
        auxDict = {}
        auxDict["node_id"]= curNode.uniqueID
        auxDict["node_cluster"] = curNode.node_cluster
        auxDict["node_layer"] = curNode.node_layer
        output.append(auxDict)
    return output


def convertSimilarityDictToJSON(inputDict,runId):
    ''' Converts a Similarity Dictionary to JSON format. For outputting to DB

    :param Dict{} similarityDict: Object which contains Data about the Computed similarities between Clusters

    :rtype: List[Dicts]
    '''

    similList = []
    for tupleKey in inputDict:
        auxDict = dict()
        auxDict["cluster1_label"]= tupleKey[0]
        auxDict["cluster2_label"]= tupleKey[1]
        auxDict["cluster_layer"] = tupleKey[2]  
        auxDict["similarityValues"] = inputDict[tupleKey]    
        auxDict["runId"] = runId
        similList.append(auxDict)
    similToJSON = similList
    #outputJSON = json.dumps(similToJSON, default=lambda o: o.__dict__, indent=4)

    return similToJSON

