import warnings

import networkx as nx
import pandas as pd
import json
class fetch:
    def __init__(self):

        self.USER= []
        self.REPUTATION= []
        self.STARTINGPOINT= []
        self.StartTIME= []
        self.finishedTIME = []
        self.TRAVELPRICE= []
        self.DESTINATION= []

    count = 0
    G = nx.classes.graph.Graph()
    Users = []
    def userfetch(self):
        with open("users.json", "r") as json_file:
            df_nodes = json.load(json_file)
        for row in df_nodes:
           userreputation = "L1-Reputation_" + str(row['reputation'])
           print(row['reputation'])
           self.REPUTATION.append([row['id'],row['reputation']])
           self.G.add_node(userreputation)
           userId= "L2-Users_" + row['id']
           ids = userId.split('_')[1]
           self.Users.append(ids)
           self.USER= self.Users
           print(row['id'])
           self.G.add_node(userId)
           self.G.add_edge(userId, userreputation, key="reputation")


    def travelfetch(self):
        with open("csvjson.json", "r") as json_file:
            df_nodes = json.load(json_file)
        username2=''
        startedby= []
        TravelID=0
        for row in df_nodes:
               TravelID = TravelID+1
               startedby= row['startedBy']
               finishedby = row['finishedBy']
               print("     ")
               TotalPrice = row['totalPrice']
               TravelPrice = "L6-TravelPrices_" + str(TotalPrice)
               self.G.add_node(TravelPrice)
               for row1 in startedby:
                   #NewNode = "L1-StartingPoints_" + row1['user']
                   #print(NewNode)
                   Latitude= row1['coordinate']['latitude']
                   Longitude= row1['coordinate']['longitude']

                   NewNode = "L3-StartingPoints_" + str(Latitude)+ ",  " + str(Longitude)
                   self.G.add_node(NewNode)

                   print(row1['coordinate']['latitude'])
                   print(row1['coordinate']['longitude'])
                   count = 0

                   for row3 in self.Users:
                       username = row1['user']
                       username1= row3
                       if(username1 == username):
                           userId = "L2-Users_" + username1
                           username2= username1
                           self.G.add_edge(userId, NewNode, key="userstartingpoint")

                       count= count+1

                   NewNode1 = "L4-time_" + row1['user']
                   Moment = row1['moment']
                   self.G.add_node(NewNode1)
                   print(row1['moment'])
                   self.G.add_edge(NewNode, NewNode1, key= "startingtime")
                   self.STARTINGPOINT.append([TravelID,row1['user'], Latitude, Longitude, Moment])
                   self.StartTIME.append([TravelID,row1['user'], Moment])
                   print("   ")

               for row2 in finishedby:
                   NewNode = "L5-Destinations_" + row2['user']
                   print(NewNode)
                   Latitude= row2['coordinate']['latitude']
                   Longitude= row2['coordinate']['longitude']
                   self.G.add_node(NewNode)
                   print(row2['coordinate']['latitude'])
                   print(row2['coordinate']['longitude'])
                   print(row2['moment'])
                   NewNode1 = "L4-time_" + row2['user']
                   finishedtime = row2['moment']
                   self.G.add_node(NewNode1)
                   self.G.add_edge(NewNode, NewNode1, key= "endtime")
                   self.G.add_edge(TravelPrice, NewNode, key="priceofTravel", edge_type= '1')
                   print("   ")
                   self.DESTINATION.append([TravelID, row2['user'], Latitude, Longitude, finishedtime, TotalPrice])
                   self.finishedTIME.append([TravelID, row2['user'], finishedtime])
                   self.TRAVELPRICE.append([TravelID, row2['user'], TotalPrice])

    print(Users)
    hello = "hello"
    for i in range(len(Users)):
        print(Users[i])
        if (Users[i] == 'Ellwood'):
            print("hey")







    def matchinggraph(self):
        self.userfetch()
        self.travelfetch()

        print(nx.info(self.G))
        nx.write_gpickle(self.G, "/home/zarah/Downloads/multigraph2.gpickle")
    def gettingLayerInf(self):
        print("\n\n the content inside User_Layer:   ")
        print(self.Users)
        print("\n\nthe content inside Reputation_Layer:   ")
        print(self.REPUTATION)
        print("\n\nthe content inside StartingPoint_Layer (TravelID,UserID, Latitude, Longitude, Startingtime):   ")
        print(self.STARTINGPOINT)
        print("\n\nthe content inside Destination_Layer (TravelID,UserID, Latitude, Longitude, Finished time, Price of Travel):   ")
        print(self.DESTINATION)
        print("\n\nthe content inside Destination_Layer (TravelID,UserID, Starting time):   ")
        print(self.StartTIME)
        print("\n\nthe content inside Travel Price_Layer (TravelID,UserID, finished time):   ")
        print(self.finishedTIME)
        print("\n\nthe content inside Travel Price_Layer (TravelID,UserID, TotalPrice):   ")
        print(self.TRAVELPRICE)

a = fetch()
a.matchinggraph()

a.gettingLayerInf()

