import unittest

import sys
for path in ['../', './', '../../../modules/', '../../../../modules']:
    sys.path.insert(1, path)

from db.entities.connected_node import NodeC
from db.entities.connected_cluster import ClusterC
from db.entities.connected_layer import LayerC

import math
import datetime
from typing import Dict
from typing import Dict
from processing.similarityFiles.dataInput import *
from processing.similarityFiles.calculateWeights import *
from processing.similarityFiles.calculateSimilarity import *
from processing.similarityFiles.miscFunctions import *

import json


class TestSimilarity(unittest.TestCase):
    '''Tests the similarity calculation which works without object orientation.'''

    def test_integration_similarityCalculation(self):
        '''
        Only for testing, can be deleted at any time.\n
        Served as a testing example to make sure the computations are correct
        '''

        limitNrNodes = 100000

        layerDict = dict()
        ("Creating Connected_cluster dict and similarity dict")
        inputLayerLocation=getTestLocationLayerData()
        inputLayerTime=getTestTimeLayerData()
        inputLayerPrice=getTestPriceLayerData()
        
        layerDict = populateWithNewNodesSingleLayer(inputLayerLocation,layerDict,limitNrNodes)
        layerDict = populateWithNewNodesSingleLayer(inputLayerTime,layerDict,limitNrNodes)
        layerDict = populateWithNewNodesSingleLayer(inputLayerPrice,layerDict,limitNrNodes)

        layerDict = calculateWeights(layerDict)
        similarityDict = calculateSimilarity(layerDict)
        ("Asserting if the calculated values are true")

        #assert x == "hello"

        # SYNTAX:
        #similarityDict[(clusterLabel1,clusterLabel2,layerOfTheClusters)][layerToWhichTheClustersAreCompared] == ExpectedSimilarityValue
        #checking if the cluster "1" and cluster "2" from the "Location" layer have the sqrt(2) similarity values for when compared with 'Price'and 'Time'layers
        self.assertEqual(similarityDict[(1,2,'Location')]['Price'], math.sqrt(2))
        self.assertEqual(similarityDict[(1,2,'Location')]['Time'], math.sqrt(2))
        self.assertEqual(similarityDict[(1,3,'Location')]['Price'], math.sqrt(10))
        self.assertEqual(similarityDict[(1,3,'Location')]['Time'], math.sqrt(16))
        self.assertEqual(similarityDict[(2,3,'Location')]['Price'], math.sqrt(4))
        self.assertEqual(similarityDict[(2,3,'Location')]['Time'], math.sqrt(10))
        self.assertEqual(similarityDict[(4,5,'Time')]['Location'], math.sqrt(19))
        self.assertEqual(similarityDict[(4,5,'Time')]['Price'], math.sqrt(26))
        self.assertEqual(similarityDict[(6,7,'Price')]['Location'], math.sqrt(3))
        self.assertEqual(similarityDict[(6,7,'Price')]['Time'], math.sqrt(8))
        
        # assert similarityDict[(1,2,'Location')]
        ("Test Passed Succesfully")
        return layerDict



def getTestLocationLayerData():
    inputLayerLocation = [
                            {
                                "cluster_label": 1,
                                "layer_name": "Location",
                                "nodes": [
                                {
                                    "Finished_time": 1576631193265951,
                                    "Latitude_Destination": -5.973257,
                                    "Longitude_Destination": 37.416316,
                                    "TravelID": "asd",
                                    "TravelPrice": 15,
                                    "UniqueID": "a",
                                    "UserID": "asdf"
                                },
                                {
                                    "Finished_time": 1576631193265951,
                                    "Latitude_Destination": -5.973257,
                                    "Longitude_Destination": 37.416316,
                                    "TravelID": "5e57ec9159bc0668543f156d",
                                    "TravelPrice": 15,
                                    "UniqueID": "aa",
                                    "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                                },
                                {
                                    "Finished_time": 1576631193265951,
                                    "Latitude_Destination": -5.973257,
                                    "Longitude_Destination": 37.416316,
                                    "TravelID": "asd",
                                    "TravelPrice": 15,
                                    "UniqueID": "aa",
                                    "UserID": "asdf"
                                },
                                {
                                    "Finished_time": 1576631193265951,
                                    "Latitude_Destination": -5.973257,
                                    "Longitude_Destination": 37.416316,
                                    "TravelID": "asd",
                                    "TravelPrice": 15,
                                    "UniqueID": "aaa",
                                    "UserID": "asdf"
                                },
                                {
                                    "Finished_time": 1576631193265951,
                                    "Latitude_Destination": -5.973257,
                                    "Longitude_Destination": 37.416316,
                                    "TravelID": "5e57ec9159bc0668543f156d",
                                    "TravelPrice": 15,
                                    "UniqueID": "aaaa",
                                    "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                                }
                                ]
                            },
                            {
                                "cluster_label": 2,
                                "layer_name": "Location",
                                "nodes": [
                                {
                                    "Finished_time": 1576631193265951,
                                    "Latitude_Destination": -5.973257,
                                    "Longitude_Destination": 37.416316,
                                    "TravelID": "5e57ec9159bc0668543f156b",
                                    "TravelPrice": 15,
                                    "UniqueID": "b",
                                    "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                                },
                                {
                                    "Finished_time": 1576631193265951,
                                    "Latitude_Destination": -5.973257,
                                    "Longitude_Destination": 37.416316,
                                    "TravelID": "asd",
                                    "TravelPrice": 15,
                                    "UniqueID": "bb",
                                    "UserID": "asdf"
                                },
                                {
                                    "Finished_time": 1576631193265951,
                                    "Latitude_Destination": -5.973257,
                                    "Longitude_Destination": 37.416316,
                                    "TravelID": "asd",
                                    "TravelPrice": 15,
                                    "UniqueID": "bbb",
                                    "UserID": "asdf"
                                },
                                {
                                    "Finished_time": 1576631193265951,
                                    "Latitude_Destination": -5.973257,
                                    "Longitude_Destination": 37.416316,
                                    "TravelID": "5e57ec9159bc0668543f156d",
                                    "TravelPrice": 15,
                                    "UniqueID": "bb",
                                    "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                                }
                                ]
                            },
                            {
                                "cluster_label": 3,
                                "layer_name": "Location",
                                "nodes": [
                                {
                                    "Finished_time": 1576631193265951,
                                    "Latitude_Destination": -5.973257,
                                    "Longitude_Destination": 37.416316,
                                    "TravelID": "5e57ec9159bc0668543f156c",
                                    "TravelPrice": 15,
                                    "UniqueID": "c",
                                    "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                                },
                                {
                                    "Finished_time": 1576631193265951,
                                    "Latitude_Destination": -5.973257,
                                    "Longitude_Destination": 37.416316,
                                    "TravelID": "asd",
                                    "TravelPrice": 15,
                                    "UniqueID": "c",
                                    "UserID": "asdf"
                                },
                                {
                                    "Finished_time": 1576631193265951,
                                    "Latitude_Destination": -5.973257,
                                    "Longitude_Destination": 37.416316,
                                    "TravelID": "asd",
                                    "TravelPrice": 15,
                                    "UniqueID": "cc",
                                    "UserID": "asdf"
                                }
                                ]
                            }
                        ]
    return inputLayerLocation

def getTestTimeLayerData():
    inputLayerTime = [
                        {
                            "cluster_label": 4,
                            "layer_name": "Time",
                            "nodes": [
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "a",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "aa",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "aaa",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "aaaa",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "b",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "bb",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "bbb",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            }
                            
                            ]
                        },
                        {
                            "cluster_label": 5,
                            "layer_name": "Time",
                            "nodes": [
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156f",
                                "TravelPrice": 15,
                                "UniqueID": "a",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "c",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            }
                            ]
                        }
                    ]
    return inputLayerTime

def getTestPriceLayerData():
    inputLayerPrice = [
                        {
                            "cluster_label": 6,
                            "layer_name": "Price",
                            "nodes": [
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156h",
                                "TravelPrice": 15,
                                "UniqueID": "b",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "bb",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },{
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "aa",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "aaa",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "aaaa",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            }
                            ]
                            
                        },
                        {
                            "cluster_label": 7,
                            "layer_name": "Price",
                            "nodes": [
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156g",
                                "TravelPrice": 15,
                                "UniqueID": "a",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "aa",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "b",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            },
                            {
                                "Finished_time": 1576631193265951,
                                "Latitude_Destination": -5.973257,
                                "Longitude_Destination": 37.416316,
                                "TravelID": "5e57ec9159bc0668543f156d",
                                "TravelPrice": 15,
                                "UniqueID": "c",
                                "UserID": "a95075f5042b1b27060080156d87fe34ec7e712c"
                            }
                            ]
                        }
                    ]
    return inputLayerPrice



if __name__ == '__main__':
    unittest.main()