import networkx as nx
import matplotlib.pyplot as plt

# ---------------------------------------------------------
# Positions géographiques (longitude, latitude)
# ---------------------------------------------------------
POSITIONS = {
    "Li": (3.06, 50.63),
    "Pa": (2.35, 48.85),
    "St": (7.75, 48.58),
    "Ly": (4.84, 45.76),
    "Ma": (5.37, 43.30),
    "Ni": (7.26, 43.70),
    "Na": (-1.55, 47.22),
    "Re": (-1.68, 48.11),
    "Bo": (-0.58, 44.84),
    "To": (1.44, 43.60),
}


# ---------------------------------------------------------
# 1. Construire le graphe pondéré
# ---------------------------------------------------------
def construire_graphe():
    G = nx.Graph()

    aretes = [
        ("Pa", "Li", 220),
        ("Pa", "St", 490),
        ("Pa", "Ly", 465),
        ("Pa", "Na", 380),
        ("Na", "Re", 110),
        ("Na", "Bo", 350),
        ("Bo", "To", 245),
        ("To", "Ma", 405),
        ("Ly", "Ma", 315),
        ("Ly", "St", 490),
        ("Ly", "Ni", 300),
        ("Ma", "Ni", 200),
        ("Bo", "Pa", 590),
    ]

    #A compléter (construire les aretes)

    return G


# ---------------------------------------------------------
# 2. Afficher le graphe avec Matplotlib
# ---------------------------------------------------------
def afficher_graphe(G):
    pos = {n: POSITIONS[n] for n in G.nodes()}  

    # Contruire le graphe pour affichage

   

    plt.title("Carte routière simplifiée (positions géographiques)")
    plt.show()


# ---------------------------------------------------------
# 3. Calculer trois itinéraires alternatifs avec Dijkstra
# ---------------------------------------------------------
def trois_itineraires(G, source="Pa", cible="Ma"):
    chemins = []
    # A compléter

    return chemins


# ---------------------------------------------------------
# 4. Afficher les trois itinéraires sur le graphe
# ---------------------------------------------------------
def afficher_itineraires(G, chemins):
    pos = {n: POSITIONS[n] for n in G.nodes()}

    nx.draw(G, pos, with_labels=True, node_color="lightgray", node_size=800)

    labels = nx.get_edge_attributes(G, "weight")
    nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)

    couleurs = ["red", "blue", "green"]

    for i, chemin in enumerate(chemins):
        edges = list(zip(chemin[:-1], chemin[1:]))
        nx.draw_networkx_edges(
            G,
            pos,
            edgelist=edges,
            width=3,
            edge_color=couleurs[i],
            label=f"Itinéraire {i+1}",
        )

    plt.title("Trois itinéraires alternatifs entre Paris et Marseille")
    plt.legend()
    plt.show()


# ---------------------------------------------------------
# Programme principal
# ---------------------------------------------------------

#Contruire le graphe G

#afficher le graphe


# Calculer les 3 itinéraires dans une variables chemins



# Affficher les itinéraires
#afficher_itineraires(G, chemins)
