import sys
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch
D = sys.argv[1] if len(sys.argv) > 1 else "."
L = [("La technique tient, brique par brique,\nune fois les prototypes mesurés", "90 %", 90), ("Les coûts tiennent\nà plus ou moins 25 %", "78 %", 78), ("Un déploiement partiel se fait :\n30 à 50 % de l'électricité fossile en 25 ans", "76 %", 76), ("Le plan complet (déploiement total) se fait :\n95 % de l'électricité fossile en 25 ans", "40 à 45 %", 42), ("Le réchauffement ralentit nettement\nsi le plan est réalisé", "81 %", 81), ("Le climat tient sous |1,5 °C| si le plan est réalisé\net que les autres secteurs suivent", "moins de 10 %", 8), ("Le climat tient sous |2 °C| si le plan est réalisé\net que les autres secteurs suivent", "49 %", 49), ("Le climat tient sous |2,5 °C| si le plan est réalisé\net que les autres secteurs suivent", "90 %", 90)]
def couleur(p):
    if p >= 70:
        return "#27ae60"
    if p >= 40:
        return "#f39c12"
    return "#c0392b"
fig = plt.figure(figsize=(16, 12.4), dpi=150)
ax = fig.add_axes([0, 0, 1, 1])
ax.set_xlim(0, 16)
ax.set_ylim(0, 12.4)
ax.axis("off")
ax.text(8, 11.85, "Plan mondial Artesia 8 : les chances de réussite estimées par l'intelligence artificielle", ha="center", va="center", fontsize=19, weight="bold", color="#1f4e8c")
ax.text(8, 11.3, "Une estimation, pas un calcul, relue après chaque plan", ha="center", va="center", fontsize=14, color="#555555")
ax.add_patch(FancyBboxPatch((0.3, 10.35), 15.4, 0.6, boxstyle="round,pad=0.02,rounding_size=0.12", fc="#1f4e8c", ec="none"))
ax.text(0.6, 10.65, "Ce qui est estimé", ha="left", va="center", fontsize=15, weight="bold", color="white")
ax.text(9.3, 10.65, "Chance", ha="center", va="center", fontsize=15, weight="bold", color="white")
ax.text(12.6, 10.65, "En image", ha="center", va="center", fontsize=15, weight="bold", color="white")
h = 1.22
for i, (q, t, p) in enumerate(L):
    y = 10.2 - (i + 1) * h
    ax.add_patch(FancyBboxPatch((0.3, y + 0.06), 15.4, h - 0.12, boxstyle="round,pad=0.02,rounding_size=0.12", fc="#f4f6f8" if i % 2 == 0 else "white", ec="#d5dbe1", lw=1))
    if "|" in q:
        l1, l2 = q.split("\n")
        a, b, c = l1.split("|")
        r = fig.canvas.get_renderer()
        x = 0.6
        for t2, wt in ((f"{i + 1}. {a}", "normal"), (b, "bold"), (c, "normal")):
            tx = ax.text(x, y + h / 2 + 0.165, t2, ha="left", va="center", fontsize=14.5, color="#222222", weight=wt)
            bb = tx.get_window_extent(renderer=r).transformed(ax.transData.inverted())
            x = bb.x1
        ax.text(0.6, y + h / 2 - 0.165, l2, ha="left", va="center", fontsize=14.5, color="#222222")
    else:
        ax.text(0.6, y + h / 2, f"{i + 1}. {q}", ha="left", va="center", fontsize=14.5, color="#222222", linespacing=1.35)
    ax.text(9.3, y + h / 2, t, ha="center", va="center", fontsize=19, weight="bold", color=couleur(p))
    x0, w = 10.6, 4.8
    ax.add_patch(FancyBboxPatch((x0, y + h / 2 - 0.2), w, 0.4, boxstyle="round,pad=0,rounding_size=0.15", fc="#e3e7eb", ec="none"))
    ax.add_patch(FancyBboxPatch((x0, y + h / 2 - 0.2), max(w * p / 100, 0.3), 0.4, boxstyle="round,pad=0,rounding_size=0.15", fc=couleur(p), ec="none"))
ax.text(8, 0.12, "Vert : 70 % et plus. Orange : 40 à 69 %. Rouge : moins de 40 %.   www.mulo.world, novembre 2026, CC BY 4.0", ha="center", va="bottom", fontsize=12, color="#555555")
fig.savefig(f"{D}/tableau_chances_plan_mondial_artesia_8.png", dpi=150)
