"""
for each cluster in the layer
    for each other cluster from all the other layers
        find the number of connexions
        save them into a dictionary (ClusterID(from other layer) -> Nr of connections)
    save all the dictionaries in a map? ( ClusterID1 -> dictionary1, ClusterID2 -> dicitonary2 )
have a map per layer? (Nr of maps = nr of layers)
Each cluster has a dictionary of connCluster-> nrConections
Each layer has a dictionary of clusters -> dictionaries of nodes/connections
"""
import os
import sys
import math
import datetime
from typing import Dict

##################AUX

modules_path = '../../../modules/'
if os.path.exists(modules_path):
    sys.path.insert(1, modules_path)

#### TO BE DELETED #### ^

from db.entities.connected_node import NodeC
from db.entities.connected_cluster import ClusterC
from db.entities.connected_layer import LayerC
from processing.similarityFiles.dataInput import *
from processing.similarityFiles.calculateWeights import *
from processing.similarityFiles.calculateSimilarity import *
from processing.similarityFiles.miscFunctions import *
from processing.similarityFiles.dataOutput import *


def main():   
    print("\nEntered Main")

    outputToFileFLAG = False

    timelist = []
    timelist.append(currentTime())#starting time
    
    """
    Current Layers
        Price_Layer
        FinishedTime_Layer
        Destination_Layer
        StartingPoint_Layer
        Reputation_Layer
        StartingTime_Layer
        User_Layer
    """
    layerNameList = ["Price_Layer","FinishedTime_Layer","Destination_Layer"] #Get it from somewhere else?
    limitNrCluster = -1 #per Layer # 0< equals noLimit
    limitNrNodes = -1 #per Layer


    layerDict = getClusterDataFromMongo(layerNameList,limitNrCluster,limitNrNodes)

    #URLlist = None
    #layerDict = getClusterDataFromSwagger(limitNrCluster,limitNrNodes, URLlist) #for Swagger, Change URLs inside the function for different input Data or provide a list with URLS 
    totalNodes = totalNumberOfNodes(layerDict)
    print("Nr. of nodes: " +str(totalNodes))
    totalClusters = totalNumberOfClusters(layerDict)
    print("Nr. of clusters: " + str(totalClusters))
    timelist.append(currentTime())

    
    #calculates the weights between the clusters (weight == number of connections) #return is displayed in outputLayerFunction    
    layerDict = calculateWeights(layerDict)
    timelist.append(currentTime())

    
    #calculates the similarity between the clusters #returns dictionary[ tuple(cluster_label1,cluster_label2), 
    #                                                                      listOfSimilarity(layer1,layer2,layer3) ]    
    similarityDict = calculateSimilarity(layerDict)
    timelist.append(currentTime()) #Finishing time    

    
    #Write to files
    runId = add_connected_run()

    
    if (outputToFileFLAG == True):
        print("Outputing data")
        outputFileLayerFunction(layerDict,totalNodes,totalClusters,runId)
        outputFileSimilFunction(similarityDict,totalNodes,totalClusters,runId)
        outputFileTimeFunction(timelist,totalNodes,totalClusters,runId)

    #Output to DB
    outputMongoConnClustDict(layerDict,runId)
    outputMongoSimilarity(similarityDict,runId)


    #Currently not used in the calculation of connections/similarity, developed for possible future uses
    #connClustersFromMongo = getConnClusterDataFromMongo()    
    #similarityDictFromMongo = calculateSimilarity(connClustersFromMongo)

    #similarityArrFromMongo = getSimilarityDataFromMongo("Price_Layer") # only 220 similarities, but there are about 20 clusters total
    #similarityArrFromMongo = getSimilarityDataFromMongo("Destination_Layer") # ~2.500k similarities    
    #similarityArrFromMongo = getSimilarityDataFromMongo("FinishedTime_Layer")# should have the rest of similarities => 15.000k
    #connectedRunFromMongo = getConnectedRunDataFromMongo()
    

    print("FINISHED")

    return

##########START##########
main()
#########FINISH##########
