import networkx as nx
from shapely.geometry import Point
import matplotlib.pyplot as plt
import unittest
from linesweep_circle import NodeIDs



def graphbuild(d_racks_segments, d_rack_conn_points):
    # 2. Graph erstellen
    G = nx.Graph()

    # 3. Knoten und Kanten aus d_racts_segments hinzufügen
    for rack, points in d_racks_segments.items():
        start = points[0]
        end = points[1]
        G.add_node(start)  # Knoten für Startpunkt
        G.add_node(end)    # Knoten für Endpunkt
        G.add_edge(start, end)  # Kante zwischen Start und Endpunkt

    # # 4. Verbindungen aus d_rack_conn_points hinzufügen
    # for connection, points in d_rack_conn_points.items():
        
    #     # Wir fügen die Kante zwischen den Punkten der Verbindung hinzu
    #     # Hinweis: Wir nehmen hier an, dass der Punkt von beiden Racks als Verbindungspunkt betrachtet wird
    #     G.add_edge(point, point)  # Verbindungspunkt als Kante

    
    
    return G

class TestShapely(unittest.TestCase):
    def test_graph(self):
        d_racks_segments = {
            'Rack_1-0': [Point(4946.5, 15865.5), Point(4946.5, 3777.6)],
            'Rack_2-0': [Point(0.1, 57.6), Point(0.1, 3777.6)],
            'Rack_2-1': [Point(0.1, 3777.6), Point(14755.1, 3777.6)],
            'Rack_3-0': [Point(185.1, 15865.5), Point(12450.7, 15865.5)],
            'Rack_4-0': [Point(2866.6, 15865.5), Point(2866.6, 3777.6)],
            'Rack_5-0': [Point(8866.1, 15865.5), Point(8866.1, 3777.6)]
        }

        d_rack_conn_points = {
            'Rack_2-0 + Rack_2-1': Point(0.1, 3777.6),
            'Rack_2-1 + Rack_1-0': Point(4946.5, 3777.6),
            'Rack_2-1 + Rack_4-0': Point(2866.6, 3777.6),
            'Rack_2-1 + Rack_5-0': Point(8866.1, 3777.6),
            'Rack_3-0 + Rack_1-0': Point(4946.5, 15865.5),
            'Rack_3-0 + Rack_4-0': Point(2866.6, 15865.5),
            'Rack_3-0 + Rack_5-0': Point(8866.1, 15865.5)
        }

        G = graphbuild(d_racks_segments, d_rack_conn_points)
        
        #print(nx.has_path(G, Point(4946.5, 15865.5), Point(0.1, 57.6)))


        # 5. Graph anzeigen
        nodes_str = "[<POINT (4946.5 15865.5)>, <POINT (4946.5 3777.6)>, <POINT (0.1 57.6)>, <POINT (0.1 3777.6)>," \
        " <POINT (14755.1 3777.6)>, <POINT (185.1 15865.5)>, <POINT (12450.7 15865.5)>, <POINT (2866.6 15865.5)>," \
        " <POINT (2866.6 3777.6)>, <POINT (8866.1 15865.5)>, <POINT (8866.1 3777.6)>]"

        edges_str = "[(<POINT (4946.5 15865.5)>, <POINT (4946.5 3777.6)>), (<POINT (4946.5 15865.5)>," \
        " <POINT (4946.5 15865.5)>), (<POINT (4946.5 3777.6)>, <POINT (4946.5 3777.6)>)," \
        " (<POINT (0.1 57.6)>, <POINT (0.1 3777.6)>), (<POINT (0.1 3777.6)>, <POINT (14755.1 3777.6)>)," \
        " (<POINT (0.1 3777.6)>, <POINT (0.1 3777.6)>), (<POINT (185.1 15865.5)>, <POINT (12450.7 15865.5)>)," \
        " (<POINT (2866.6 15865.5)>, <POINT (2866.6 3777.6)>), (<POINT (2866.6 15865.5)>, <POINT (2866.6 15865.5)>)," \
        " (<POINT (2866.6 3777.6)>, <POINT (2866.6 3777.6)>), (<POINT (8866.1 15865.5)>, <POINT (8866.1 3777.6)>)," \
        " (<POINT (8866.1 15865.5)>, <POINT (8866.1 15865.5)>), (<POINT (8866.1 3777.6)>, <POINT (8866.1 3777.6)>)]"
        
        nodes = G.nodes

        allids = NodeIDs(nodes)
        for k,v in d_racks_segments.items():
            allids.add_points(v)

        for k,v in d_rack_conn_points.items():
            allids.add_point(v)


        #ids = allids.get_ids(nodes)

        #cords = allids.get_points(ids)
        

        # self.assertEqual(len(ids), len(nodes))
        
        
        # self.assertEqual(cords,nodes_str)

        # self.assertEqual(allids.get_point(1), Point(4946.5, 15865.5))


        # 6. Optional: Visualisierung des Graphen
        # Dies ist ein sehr grundlegendes Plotten. Bei Bedarf könnte man es weiter anpassen.
        pos = {node: (node.x, node.y) for node in G.nodes()}  # Positionen der Knoten aus den Punkten

        # Plotten des Graphen
        nx.draw(G, pos, with_labels=False, node_size=10, font_size=8)
        plt.show()

    def test_easy(self):
        pass



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