from path_plan.topo_graph import *
import networkx as nx
import matplotlib.pyplot as plt


# from traffic_flow.traffic_flow_planner import *

# from path_plan.path_planner_2 import *
# from dispatcher import Dispatcher, PreSchedule
topo = Topo()
topo.generate_topo_graph()
# path_planner = PathPlanner(topo)
# a, b = path_planner.path_cost_generate("ef89f721-5134-3ef1-91e2-95b4e5004675", "e5d0ac4e-c134-3ef1-901b-867980c8453e", False)
# print(a,b)
load_G = topo.get_load_G()
unload_G = topo.get_unload_G()
print(unload_G.edges(data=True))
print(load_G.edges(data=True))
# alpha = 1
# beta = 1
# source_area = "e44052d4-1134-6602-ea64-f14d05ecfb3f"
# target_area = "659ca4d5-2134-6602-ed05-1deddfca63f9"
# unload_source_node = str(session_postgre.query(DiggingWorkArea).filter_by(Id=source_area).first().ExitNodeId)
# unload_end_node = str(session_postgre.query(DumpArea).filter_by(Id=target_area).first().EntranceNodeId)
# # _, park_path = nx.single_source_dijkstra(unload_G, source=source_node, target=source_node, weight="real_distance")
# # path = park_path
# # for i in range(len(path)-1):
# #     pair_now = [path[i], path[i+1]]
# #     # edge_lane = nx.get_edge_attributes(load_G,'lane')
# #     data = dict(unload_G[path[i]][path[i+1]]['lane'])
# #     for u,v in data.items():
# #         data[u] = [v[0],beta*3]
# #     val = list(data.values())
# #     unload_G[path[i]][path[i+1]]['locked_distance'] = sum([beta * sum(i[0] for i in val) + alpha * sum(i[-1] for i in val)])
#
# unload_cost = nx.dijkstra_path_length(unload_G, source=unload_source_node, target=unload_end_node,
#                                        weight="locked_distance")
# print(unload_cost)
# # print([path[i], path[i+1]], unload_G[path[i]][path[i+1]]['locked_distance'])
# # for i in range(len(park_path) - 1):
# #     data = dict(unload_G[park_path[i]][park_path[i + 1]]['lane'])
# #     for u, v in data.items():
# #         # u_lane_cost = lane_cost_generate(u)
# #         data[u] = [v[0], beta *3]
# #     val = list(data.values())
# #     # unload_G[park_path[i]][park_path[i + 1]]['locked_distance'] = sum(beta * sum(i[0] for i in val) + alpha * sum(i[-1] for i in val))
# #     unload_G[park_path[i]][park_path[i + 1]]['locked_distance'] = sum( beta * sum(i[0] for i in val))
# #     # unload_G[park_path[i]][park_path[i + 1]]['locked_distance'] = sum(beta * sum(i[0] for i in val) + alpha * sum(i[-1] for i in val))
# #     unload_G[park_path[i]][park_path[i + 1]]['locked_distance'] = sum( beta * sum(i[0] for i in val))
#
# # print(load_G.edges(data=True))
# print(unload_G.edges(data=True))
# # distance = nx.dijkstra_path_length(load_G, source="81be6a9e-8134-3ef2-141e-4bb045b8d097", target= "1c85bc2c-9134-6281-1265-fd19d0dcbd52", weight="locked_distance")
# # print(distance)
# # print(distance,path)
# # aaa = topo.get_load_source_node("7b9c8e89-7134-63ac-9c44-f183674b090c")
# # print(aaa)
# # path,_, _= topo.get_load_target_node_real("7b9c8e89-7134-63ac-9c44-f183674b090c",None)
# # print(path)
# # updata_edge = {}
# # for i in range(len(path)-1):
# #     pair_now = [path[i], path[i+1]]
# #     # edge_lane = nx.get_edge_attributes(load_G,'lane')
# #     data = dict(load_G[path[i]][path[i+1]]['lane'])
# #     for u,v in data.items():
# #         data[u] = [v[0],v[0]]
# #     load_G[path[i]][path[i+1]]['locked_distance'] = sum(i[-1] for i in list(data.values()))
# #     print([path[i], path[i+1]], load_G[path[i]][path[i+1]]['locked_distance'])
#
#



# unload_G = topo.get_un  data=True))
pos = nx.shell_layout(load_G)
nx.draw(load_G, pos, with_labels=True, node_color='red', edge_color='blue', font_size=18, width=5, node_size=600,
        alpha=0.5)
plt.show()

pos = nx.shell_layout(unload_G)
nx.draw(unload_G, pos, with_labels=True, node_color='red', edge_color='blue', font_size=18, width=5, node_size=600,
        alpha=0.5)
plt.show()
# unload_G = topo.get_unload_G()
# print(unload_G.edges(data=True))
#
# edge_arrtibutes = dict(nx.get_edge_attributes(unload_G,'lane'))
# print(edge_arrtibutes)
# print(type(edge_arrtibutes))
# print(edge_arrtibutes.values())
# print(type(edge_arrtibutes.values()))
# pos = nx.shell_layout(load_G)
# nx.draw(load_G, pos, with_labels=True, node_color='red', edge_color='blue', font_size=18, width=5, node_size=600,
#         alpha=0.5)
# plt.show()

# a = "-rw-r--r-- 1  cs9315  22439  Feb 18 2021"
# print(len(a))

# end= time.process_time()
# print('Running time: %s Seconds'%(end-start))
