import cairosvg,math
W,H=1600,900
def txt(x,y,s,size=16,anchor="middle",weight="normal",fill="#111"):
    return f'<text x="{x}" y="{y}" font-family="Arial,Helvetica,sans-serif" font-size="{size}" text-anchor="{anchor}" font-weight="{weight}" fill="{fill}">{s}</text>'
def leader(x1,y1,x2,y2):
    return f'<line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y2}" stroke="#333" stroke-width="1.5"/><circle cx="{x2}" cy="{y2}" r="3" fill="#333"/>'
def vagues(x,y,w,n,col="#a8c4dc"):
    return "".join(f'<path d="M {x+i*w/n} {y} q {w/n/4} -6 {w/n/2} 0 q {w/n/4} 6 {w/n/2} 0" stroke="{col}" stroke-width="2" fill="none"/>' for i in range(n))
def eolienne(x,y,h,r):
    s=[f'<rect x="{x-2}" y="{y}" width="4" height="{h}" fill="#444"/>',f'<circle cx="{x}" cy="{y}" r="3" fill="#333"/>']
    for a in (0,120,240):
        ax=x+r*math.sin(math.radians(a));ay=y-r*math.cos(math.radians(a))
        s.append(f'<line x1="{x}" y1="{y}" x2="{ax:.0f}" y2="{ay:.0f}" stroke="#333" stroke-width="2.5"/>')
    return "".join(s)
def fleche(x1,y1,x2,y2,col="#e07b00"):
    a=math.atan2(y2-y1,x2-x1);l=12
    p1=(x2-l*math.cos(a-0.4),y2-l*math.sin(a-0.4));p2=(x2-l*math.cos(a+0.4),y2-l*math.sin(a+0.4))
    return f'<line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y2}" stroke="{col}" stroke-width="4" stroke-dasharray="10,6"/><polygon points="{x2},{y2} {p1[0]:.0f},{p1[1]:.0f} {p2[0]:.0f},{p2[1]:.0f}" fill="{col}"/>'
def regle(x,ybas,yhaut,hmax,pas):
    s=[f'<line x1="{x}" y1="{ybas}" x2="{x}" y2="{yhaut}" stroke="#17415f" stroke-width="3"/>']
    n=int(hmax/pas)
    for i in range(n+1):
        y=ybas-(ybas-yhaut)*i/n
        s.append(f'<line x1="{x-8}" y1="{y:.0f}" x2="{x+8}" y2="{y:.0f}" stroke="#17415f" stroke-width="2"/>')
        s.append(txt(x+12,y+4,f"{int(i*pas):,} m",11,"start","bold","#17415f"))
    s.append(txt(x,ybas+22,"Height ruler between the two basins",11,"middle","bold","#17415f"))
    return "".join(s)
def dessin(sky):
    taille=3 if sky=="A" else sky
    s=[f'<svg xmlns="http://www.w3.org/2000/svg" width="{W}" height="{H}" viewBox="0 0 {W} {H}">',f'<rect width="{W}" height="{H}" fill="#eef3f7"/>']
    mer=(taille==3)
    s.append('<polygon points="0,700 480,700 700,260 900,180 1150,120 1600,100 1600,830 0,830" fill="#a89a7c"/>')
    s.append('<polygon points="900,180 1150,120 1600,100 1600,140 1160,160 910,215" fill="#7f9a6a"/>')
    s.append('<polygon points="480,700 700,260 735,262 520,700" fill="#8c7b5c"/>')
    if mer:
        s.append('<rect x="0" y="700" width="480" height="130" fill="#3b7fb5"/>')
        s.append(vagues(10,720,460,10,"#9fc3e0"))
        s.append(txt(240,790,"The sea: lower basin of the salt loop, never pumped to the lake",13,"middle","bold","#fff"))
    else:
        s.append('<polygon points="0,700 480,700 520,700 520,730 0,730" fill="#b5c49f"/>')
    s.append('<rect x="0" y="830" width="1600" height="70" fill="#17415f"/>')
    for i in range(12):
        x=960+i*50
        s.append(f'<line x1="{x}" y1="{145-i*2}" x2="{x+8}" y2="{125-i*2}" stroke="#2f6fb5" stroke-width="2" stroke-dasharray="3,3"/>')
    s.append('<path d="M 930 178 L 1590 108" stroke="#2c7fb8" stroke-width="5" fill="none"/>')
    s.append(txt(1260,80,"The Pacha catchment and the artesian spring fill the lake for free" if sky=="A" else "The Pacha catchment and the Gold stream fill the lake for free",14,"middle","bold"))
    s.append(txt(1260,98,"The Sky adds the water pumped up from the lower lake when electricity is worth almost nothing",12,"middle"))
    s.append('<ellipse cx="1080" cy="235" rx="165" ry="44" fill="#1b4f7a" stroke="#0a3d62" stroke-width="2"/>')
    s.append('<path d="M 922 250 Q 1080 300 1238 250" stroke="#f2c14e" stroke-width="3" fill="none" stroke-dasharray="8,5"/>')
    s.append(vagues(945,228,270,8))
    s.append('<rect x="908" y="218" width="12" height="46" fill="#8a6a3e"/>')
    s.append(txt(1080,262,"Sky reserve",11,"middle","bold","#f2c14e"))
    s.append(txt(1240,300 if sky!="A" else 305,"The upper lake: rain on top, the Sky reserve below",14,"middle","bold"))
    s.append(txt(1240,318 if sky!="A" else 323,{2:"8 million m³ of rain + 6.4 million of reserve, raised dam: 4.7 GWh kept for winter",3:"4 million m³ of rain + 12.8 million of fresh-water reserve: 47 GWh kept for winter"}[taille],12,"middle"))
    s.append('<path d="M 935 268 L 935 330 L 860 330" stroke="#5b8fb5" stroke-width="5" fill="none"/>')
    s.append('<rect x="730" y="330" width="130" height="18" rx="4" fill="#8a6a3e"/>')
    s.append('<rect x="842" y="268" width="56" height="30" rx="3" fill="#e07b00"/>')
    s.append(txt(870,288,"lift",10,"middle","bold","#fff"))
    s.append(fleche(860,330,880,300))
    s.append(fleche(880,268,912,240))
    s.append(txt(690,288,"30 m lifting station:",11,"end","bold","#b35f00"))
    s.append(txt(690,303,"from the upper basin to the lake",11,"end"))
    s.append(txt(690,318,"the lake flows back down by gravity",11,"end"))
    if taille==3:
        s.append('<rect x="600" y="350" width="230" height="45" fill="#1b4f7a" stroke="#0a3d62" stroke-width="2"/>')
        s.append('<rect x="713" y="350" width="4" height="45" fill="#d8cdb4"/>')
        s.append(vagues(605,372,105,3));s.append(vagues(720,372,105,3,"#c8e0b0"))
        s.append(txt(845,378,"Upper basin 2 × 400,000 m³",12,"start","bold"))
    else:
        s.append('<rect x="600" y="350" width="230" height="45" fill="#1b4f7a" stroke="#0a3d62" stroke-width="2"/>')
        s.append(vagues(605,372,220,6))
        s.append(txt(845,378,"Upper basin 800,000 m³",12,"start","bold"))
    for i in range(8):
        x=1200+i*45
        s.append(eolienne(x,132-i*3,50,18))
    s.append(txt(1300,348,{2:"40 wind turbines of 6 MW and 16 MWp: when the grid overflows, they pump first",3:"10 wind turbines, 100 MWp and 1,000 hybrid panels pump first, the grid makes up the rest"}[taille],13,"middle","bold"))
    s.append(txt(1300,366,{2:"70 of the 82 GWh pumped up each year come from our own wind and sun",3:"85 GWh from our production, 334 GWh of grid surplus at almost zero price","A":"80 GWh from our production, 339 GWh of grid surplus at almost zero price"}[sky if sky=="A" else taille],12,"middle"))
    s.append('<path d="M 640 395 L 520 700 L 440 700" stroke="#7fb3d9" stroke-width="9" fill="none"/>')
    s.append('<path d="M 640 395 L 520 700 L 440 700" stroke="#2c3e50" stroke-width="3" fill="none"/>')
    s.append(fleche(540,640,610,465))
    l1,l2={2:("Surplus: the 64 MW pumps, almost","idle on the Gold, pump up the lower lake"),3:("Surplus: the 300 MW pumps draw","from the fresh-water lake, never from the sea")}[taille]
    s.append(txt(640,580,l1,13,"start","bold","#b35f00"))
    s.append(txt(640,597,l2,13,"start","bold","#b35f00"))
    l1,l2={2:("Two DN2000 and a third penstock, 300 m:","Pelton 48 MW + Sky Pelton 25 MW"),3:("Steel-lined shaft of 1,500 m:","Duplex Pelton 300 MW + Sky Pelton 100 MW")}[taille]
    s.append(txt(640,657,l1,14,"start","bold"))
    s.append(txt(640,675,l2,14,"start","bold"))
    s.append(leader(636,657,572,600))
    s.append('<rect x="270" y="645" width="200" height="90" fill="#5a4024"/>')
    s.append('<rect x="315" y="630" width="110" height="20" fill="#8a6a3e"/>')
    s.append(txt(370,668,"Plant",13,"middle","bold","#fff"))
    s.append(txt(370,688,"surplus: pumps on, Pelton stopped",10,"middle","normal","#fff"))
    s.append(txt(370,704,"high demand: everything runs 24/7",10,"middle","normal","#fff"))
    s.append(txt(370,720,"the rest of the time: the Gold",10,"middle","normal","#fff"))
    if taille==3:
        s.append('<ellipse cx="135" cy="530" rx="115" ry="24" fill="#1b4f7a" stroke="#0a3d62" stroke-width="2"/>')
        s.append(vagues(45,530,180,5,"#c8e0b0"))
        s.append(txt(135,570,"Fresh-water lower lake, 12.8 Mm³",12,"middle","bold"))
        s.append(txt(135,585,"the turbined rain water, kept to be pumped back up",10,"middle"))
        s.append('<rect x="20" y="605" width="235" height="38" fill="#1b4f7a" stroke="#0a3d62" stroke-width="2"/>')
        s.append(vagues(25,628,225,6,"#c8e0b0"))
        for i in range(8):
            s.append(f'<rect x="{24+i*29}" y="600" width="25" height="8" fill="#2d3e66" stroke="#9fb3d1" stroke-width="1"/>')
        s.append(txt(137,660,"Covered fresh-water lower basin, kept:",12,"middle","bold"))
        s.append(txt(137,675,"the city's water, sheltered from heat waves",10,"middle"))
        s.append('<line x1="248" y1="537" x2="248" y2="600" stroke="#5b8fb5" stroke-width="5"/>')
        s.append('<line x1="270" y1="690" x2="255" y2="630" stroke="#5b8fb5" stroke-width="5"/>')
        s.append('<rect x="40" y="440" width="40" height="55" fill="#c3ccd6" stroke="#9aa6b2"/>')
        s.append('<rect x="100" y="450" width="40" height="45" fill="#c3ccd6" stroke="#9aa6b2"/>')
        s.append(txt(155,412,"The coastal city keeps its fresh water:",11,"middle","bold"))
        s.append(txt(155,427,"the Sky borrows the water and gives it back",11,"middle","bold"))
    else:
        s.append('<rect x="250" y="745" width="220" height="55" fill="#1b4f7a" stroke="#0a3d62" stroke-width="2"/>')
        s.append(vagues(255,772,210,5))
        s.append(txt(360,818,"Lower basin 800,000 m³",12,"middle","bold"))
        s.append('<line x1="300" y1="735" x2="300" y2="745" stroke="#5b8fb5" stroke-width="6"/>')
        s.append('<ellipse cx="125" cy="772" rx="112" ry="32" fill="#1b4f7a" stroke="#0a3d62" stroke-width="2"/>')
        s.append(vagues(35,772,180,5))
        s.append('<path d="M 237 772 L 250 772" stroke="#5b8fb5" stroke-width="6"/>')
        s.append(txt(125,820,"Lower lake 6.4 million m³",12,"middle","bold"))
        s.append('<path d="M 13 772 L 0 772" stroke="#5b8fb5" stroke-width="6"/>')
        s.append(txt(135,650,"Lower lake next to the lower basin:",12,"middle","bold"))
        s.append(txt(135,668,"it keeps the turbined water to pump it back up,",12,"middle"))
        s.append(txt(135,686,"the surplus continues to the valley",12,"middle"))
        s.append(leader(120,695,110,745))
    s.append(regle(1010,770 if taille==2 else 700,372,300 if taille==2 else 1500,50 if taille==2 else 500))
    if sky=="A":
        s.append('<polygon points="1250,175 1600,150 1600,175 1250,200" fill="#6b6b6b"/>')
        s.append('<polygon points="1250,200 1600,175 1600,215 1250,240" fill="#4f86b8"/>')
        s.append('<polygon points="1250,240 1600,215 1600,240 1250,265" fill="#6b6b6b"/>')
        s.append(txt(1430,232,"confined aquifer under pressure",11,"middle","bold","#fff"))
        s.append(txt(1590,168,"impermeable layer",10,"end","normal","#fff"))
        s.append('<line x1="900" y1="196" x2="1600" y2="150" stroke="#c0392b" stroke-width="2" stroke-dasharray="6,4"/>')
        s.append('<path d="M 1260 222 L 1240 225 L 1240 210" stroke="#2c7fb8" stroke-width="7" fill="none"/>')
        s.append('<path d="M 1240 210 q -6 -14 -12 0 M 1240 210 q 6 -14 12 0" stroke="#2c7fb8" stroke-width="3" fill="none"/>')
        s.append(txt(1425,276,"Artesian spring of 2 m³/s: the aquifer pressure",11,"middle","bold","#17415f"))
        s.append(txt(1425,289,"(red line) carries it up to the lake, without a pump",11,"middle","bold","#17415f"))
    s.append('<g transform="translate(1080,465)">')
    s.append('<rect x="0" y="0" width="505" height="170" fill="#ffffff" stroke="#17415f" stroke-width="2"/>')
    s.append(txt(252,24,{2:"V18 Sky 2: the Gold 2 that keeps the wind for windless days",3:"V18 Sky 3: the Gold 3 turned into a winter reserve at 1,500 m","A":"V18 Sky Artesia: the Sky 3 and an artesian spring"}[sky],15,"middle","bold","#17415f"))
    lignes={2:["Surplus: Pelton stopped, our wind turbines pump the lower lake up","Reserve of 6.4 million m³ kept for winter, on top of the 8 of rain","High demand: 48 + 25 MW + rain Pelton, 24 hours a day, as long as there is water","87 M€ added, +11.3 M€ a year, 5.9 M€ net","The MWh sold is worth 95.6 € instead of 87.6: we sell better, not dearer","Round trip 71%: 300 m returned for 330 m pumped"],3:["Surplus: the 300 MW pumps lift the fresh-water lower lake","Reserve of 12.8 million m³ at 1,500 m: 47 GWh, 5 days at full power","High demand: 400 MW of Pelton, 1,802 GWh sold instead of 1,565","225 M€ added, +42.4 M€ a year, 28.2 M€ net","The MWh sold is worth 108.1 € instead of 98.0","Never sea water in the lake: switched intake and fresh-water flushing"],"A":["Artesian spring of 2 m³/s: 26 MW of free water at 1,500 m, day and night","The aquifer pressure carries the water up to the lake, without a pump","Reserve of 12.8 million m³: 47 GWh, partly filled by the spring","1,721 GWh sold instead of 1,429 for the Pacha 3, lake empty 7 days instead of 136","229 M€ added to the Pacha 3, +45.4 M€ a year, 31.0 M€ net","We take from the aquifer only what it renews: never fossil water"]}[sky]
    for i,l in enumerate(lignes):
        s.append(txt(15,52+i*20,l,12,"start","bold" if i==3 else "normal"))
    s.append('</g>')
    titres={"A":"Hydroloop Muhleisen 2026 V18 Sky Artesia: the Sky 3 plus an artesian spring, the water that rises by itself to the lake",2:"Hydroloop Muhleisen 2026 V18 Sky 2: the Gold 2 plus a lower lake, water pumped back up when the grid overflows",3:"Hydroloop Muhleisen 2026 V18 Sky 3: the Gold 3 plus a fresh-water lower lake, 47 GWh kept for winter"}
    s.append(txt(800,860,titres[sky],21,"middle","bold","#fff"))
    s.append(txt(800,885,"Schematic cross-section: heights to scale between the two basins (ruler), the rest not. www.mulo.world, October 2026, CC BY 4.0",13,"middle","normal","#dfe7ee"))
    s.append('</svg>')
    return "\n".join(s)
for k in (2,3,"A"):
    cairosvg.svg2png(bytestring=dessin(k).encode("utf-8"),write_to=f"schema_hydroloop_muhleisen_2026_v18_sky_{'artesia' if k=='A' else k}.png",output_width=W,output_height=H)
print("ok")
