#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Fonctions pour dessiner des graphes simples en Python.

Utilise la bilbiothèque NetworkX
https://networkx.org/
"""

import networkx as nx
import matplotlib.pyplot as plt


def make_graph(lst):
    """ lst est une liste d'adjacence.
        Renvoie graphe au format NetworkX.
        Le format NetworkX est documenté ici :
        https://networkx.org/documentation/stable/reference/readwrite/edgelist.html
    """
    G = nx.Graph()
    nodes_labels = {}
    n = len(lst)

    for v in range(n):
        G.add_node(v)
        nodes_labels[v] = str(v)

    for v in range(n):
        for u in lst[v]:
            G.add_edge(v, u)

    return G, nodes_labels


def draw_graph(lst):
    """ Renvoie dessin du graphe (simple) représenté
        par la liste d'adjacence lst.
    """
    G, nodes_labels = make_graph(lst)

    node_options = {
        "node_color": "white",
        "edgecolors": "black",
        "linewidths": 1,
    }

    fig, ax = plt.subplots()
    ax.axis('off')

    # Disposition des sommets
    pos = nx.spring_layout(G)
    # Pour les dispositions des sommets possibles, voir
    # https://networkx.org/documentation/stable/reference/drawing.html#module-networkx.drawing.layout

    nx.draw_networkx_nodes(G, pos=pos, ax=ax, **node_options)
    nx.draw_networkx_edges(G, pos=pos, ax=ax)
    nx.draw_networkx_labels(G, pos, nodes_labels)

    fig.tight_layout()
    return fig
