import sys
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['Inter', 'Noto Sans CJK SC']
from matplotlib.patches import FancyBboxPatch
D = sys.argv[1] if len(sys.argv) > 1 else "."
L = [('原型实测之后，\n技术逐个部件站得住', '90%', 90), ('成本控制在\n正负 25% 以内', '76%', 76), ('实现部分部署：\n25 年内替代 30% 至 50% 的化石电力', '73%', 73), ('实现完整计划（全面部署）：\n25 年内替代 95% 的化石电力', '35% 至 39%', 37), ('如果计划实现，\n变暖明显减缓', '80%', 80), ('气候保持在 |1.5 °C| 以下，\n如果计划实现且其他行业跟进', '不到 10%', 8), ('气候保持在 |2 °C| 以下，\n如果计划实现且其他行业跟进', '49%', 49), ('气候保持在 |2.5 °C| 以下，\n如果计划实现且其他行业跟进', '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, "Artesia 7 世界计划：人工智能估计的成功概率", ha="center", va="center", fontsize=19, weight="bold", color="#1f4e8c")
ax.text(8, 11.3, "这是估计，不是计算，每个计划之后都会重新审视", 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, "估计的内容", ha="left", va="center", fontsize=15, weight="bold", color="white")
ax.text(9.3, 10.65, "概率", ha="center", va="center", fontsize=15, weight="bold", color="white")
ax.text(12.6, 10.65, "一目了然", 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, "绿色：70% 及以上。橙色：40% 至 69%。红色：低于 40%。   www.mulo.world，2026 年 11 月，CC BY 4.0", ha="center", va="bottom", fontsize=12, color="#555555")
fig.savefig(f"{D}/tableau_chances_plan_mondial_artesia_7.png", dpi=150)
