# from graph.topo_graph import *
# import networkx as nx
# import matplotlib.pyplot as plt
#
# topo = Topo()
# topo.generate_topo_graph()
# load_G = topo.get_load_G()
# 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)
# # topo.get_unload_target_node_real()
# plt.show()

# import numpy as np
#
# arrival_truck_set = ['309705a0-5ddf-4559-b6c4-ee17a57677ad', '899705a0-5ddf-4559-b6c4-ee17a57677ad']
#
# arrival_truck_reach_time = [8.04, 6.05]
# #
# # arrival_truck_set = ['309705a0-5ddf-4559-b6c4-ee17a57677ad']
# #
# # arrival_truck_reach_time = [8.04]
#
# arrival_truck_list = list(zip(np.array(arrival_truck_set), np.array(arrival_truck_reach_time)))
#
# print(arrival_truck_list)
#
# arrival_truck_list = sorted(arrival_truck_list, key=lambda item: item[1])
#
# # print(arrival_truck_list)
# #
# # print(zip(*arrival_truck_list))
#
# for x1, x2 in arrival_truck_list:
#     print(x1, x2)


# A = [['309705a0-5ddf-4559-b6c4-ee17a57677ad', 9.04], ['899705a0-5ddf-4559-b6c4-ee17a57677ad', 6.05]]
# A = sorted(A, key=lambda item: item[1])
# print(A)

from graph.topo_graph import *
import networkx as nx
import matplotlib.pyplot as plt
import sqlite3


conn = sqlite3.connect('test.db')
c = conn.cursor()

cursor = c.execute("SELECT * FROM GRAPH_NODE")

node_label = {}
for row in cursor:
        node_label[row[1]] = row[2]

print(node_label)

load_G = nx.Graph()

unload_G = nx.Graph()

cursor = c.execute("SELECT * FROM GRAPH_EDGE")

for row in cursor:

        # 处理边包含路段
        lanes = row[5].split(" ")

        lane_info = {}

        for item in lanes:
                try:
                        # print(item)
                        distance = session_postgre.query(Lane).filter_by(Id=item).first().Length
                        lane_info[item] = list([distance, distance])
                except Exception as es:
                        print(es)
                        session_postgre.rollback()

        # 重构卸载路径拓扑图
        if row[4] == 1:
                load_G.add_node(row[1], name=node_label[row[1]])
                load_G.add_node(row[2], name=node_label[row[2]])
                load_G.add_edge(str(row[1]), str(row[2]), real_distance=row[3], lane=lane_info)
        # 重构装载路径拓扑图
        else:
                unload_G.add_node(row[1], name=node_label[row[1]])
                unload_G.add_node(row[2], name=node_label[row[2]])
                unload_G.add_edge(str(row[1]), str(row[2]), real_distance=row[3], lane=lane_info)
print("数据操作成功")

node_label_load = {}

node_label_unload = {}

for node in load_G.nodes:
        if node in node_label:
                if node_label[node] == "None":
                        node_label_load[node] = ''
                else:
                        node_label_load[node] = node_label[node]
        else:
                node_label_load[node] = ''

for node in unload_G.nodes:
        if node in node_label:
                if node_label[node] == "None":
                        node_label_unload[node] = ''
                else:
                        node_label_unload[node] = node_label[node]
        else:
                node_label_unload[node] = ''

pos1 = nx.spring_layout(load_G)
# nx.draw(load_G, pos1, with_labels=False, node_color='red', edge_color='blue', font_size=30, width=3, node_size=200,
#         alpha=0.5)

nx.draw_networkx(load_G, pos=pos1, labels=node_label_load, with_labels=True, node_color='red', \
                 edge_color='blue', font_size=10, width=1.5, node_size=100, alpha=0.5)
plt.show()

pos2 = nx.spring_layout(unload_G)
# pos2 = nx.shell_layout(unload_G)
# nx.draw(load_G, pos1, with_labels=False, node_color='red', edge_color='blue', font_size=30, width=3, node_size=200,
#         alpha=0.5)

nx.draw_networkx(unload_G, pos=pos2, labels=node_label_unload, with_labels=True, node_color='red', \
                 edge_color='blue', font_size=15, width=3, node_size=200, alpha=0.5)
plt.show()

# topo = Topo()
# topo.load_G = load_G
# topo.unload_G = unload_G
#
# print(topo.load_G.nodes.data('None'))
# print(topo.get_load_target_node_real("6b417083-83b0-4eec-a8bd-2f85f96cb029", "1010cbfe-b134-3ef1-91bb-d746241c975a"))