#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, ...]}
    '''

    '''
    {
        layer1 : {
            [
                {
                    cluster_label1 : 0123400,
                    cluster_layer: layer1,
                    "cluster_connClustDict": {
                        "0123456": 98
                        "1234567": 12
                    },

                    cluster_containedNodesDict : {
                        [
                            abcd,
                            sgre,
                            dgre,
                            ddhr,
                            yyrh
                        ]
                    }
                },
                {

                },
                {
                    
                }            
            ]
        },

        layer2 : {

        }
    }
    '''
    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" : changeDictKeysToString(curCluster.cluster_connClustDict),
                "cluster_connNodesDict" : getFrozensetFromConnNodesDict(curCluster.cluster_connNodesDict),
                "cluster_containedNodesDict" : getNodeIdListFromContainedNodesDict(curCluster.cluster_containedNodesDict),
            })

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

    return outputJSON

def changeDictKeysToString(inputDict):

    keys_values = inputDict.items()
    outputDict = { str(key): value for key,value in keys_values}
    return outputDict

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

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


def convertSimilarityDictToJSON(inputDict,runId):

    similList = []
    for compositeKey in inputDict:
        frozensetString =list()
        #key is a tuple of cluster_labels
        for key in compositeKey:
            frozensetString.append(key)

        similList.append({
            "clusterTuple" : frozensetString,
            "similarityValues" : inputDict[compositeKey],
            "runId": runId
        })
    similToJSON = similList
    #outputJSON = json.dumps(similToJSON, default=lambda o: o.__dict__, indent=4)

    return similToJSON

