"""
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 *
from routes.connRun import connected_run


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

    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 = 20 #per Layer
    limitNrNodes = 1000 #per Layer

    layerDict = getClusterDataFromMongo(layerNameList,limitNrCluster,limitNrNodes)
    #layerDict = getClusterDataFromSwagger(limitNrCluster,limitNrNodes) #for Swagger, Change URLs inside the function for different input Data

    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()
    
    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, developed for possible future uses
    connClustersFromMongo = getConnClusterDataFromMongo()    
    similarityArrFromMongo = getSimilarityDataFromMOngo()
    print("FINISHED")

    return


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