import sys
import pandas as pd
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
D = sys.argv[1] if len(sys.argv) > 1 else ".."
SORTIE = sys.argv[2] if len(sys.argv) > 2 else "."
c = pd.read_csv(f"{D}/centrales_artesia_5.csv").sort_values("twh_par_an")
N = {"O Sky": "O Sky", "Pacha et Gold existantes": "Existing Pacha and Gold", "Panneaux solaires hybrides industriels": "Industrial hybrid solar panels", "Sky (existantes et nouvelles)": "Sky (existing and new)", "Titan Claudia 5 côtière et 4 Paris Style": "Coastal Titan Claudia 5 and 4 Paris Style", "V14 Soleil des plateaux": "V14 Soleil on plateaus", "V14 Soleil mixte": "Mixed V14 Soleil", "V14 Soleil près des postes fossiles": "V14 Soleil near fossil substations", "V14 de crête": "Ridge V14", "V15 Canyon": "V15 Canyon", "V21 Barrage": "V21 Barrage", "Îles hybrides flottantes": "Floating hybrid islands"}
fig, (g1, g2) = plt.subplots(1, 2, figsize=(15, 7), sharey=True)
col = ["#2e8b57" if m == "oui" else "#e67e22" if m == "à étudier" else "#7f8c8d" for m in c.max_possible]
g1.barh(c.centrale.map(N), c.twh_par_an, color=col)
for y, v in enumerate(c.twh_par_an):
    g1.text(v + 50, y, f"{v:,.0f}", va="center", fontsize=9)
g1.set_title("TWh per year delivered to the plan", fontsize=11)
g1.set_xlim(0, 8200)
g2.barh(c.centrale.map(N), c.md_euros, color=col)
for y, (v, m) in enumerate(zip(c.md_euros, c.meur_par_gwh)):
    t = f"€{v:,.0f}bn" + (f", {m:.2f} M€/GWh" if pd.notna(m) else "")
    g2.text(v + 40, y, t, va="center", fontsize=9)
g2.set_title("Billion euros over 25 years, and M€ per annual GWh", fontsize=11)
g2.set_xlim(0, 4600)
g1.plot([], [], color="#2e8b57", lw=6, label="MAX version planned")
g1.plot([], [], color="#e67e22", lw=6, label="MAX to be studied")
g1.plot([], [], color="#7f8c8d", lw=6, label="No MAX (size set by the site)")
g1.legend(loc="lower right", fontsize=9)
fig.suptitle("The plants of the Artesia 5 plan, version 1, world economy", fontsize=13)
fig.tight_layout(rect=[0, 0, 1, 0.95])
fig.savefig(f"{SORTIE}/schema_centrales_artesia_5.png", dpi=110)
