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 ile(x,y,w,n,col="#2d3e66"):
    out=[]
    pas=w/n
    for i in range(n):
        cx=x+pas*i+pas/2
        out.append(f'<rect x="{cx-pas*0.42:.0f}" y="{y-3}" width="{pas*0.84:.0f}" height="6" fill="{col}" stroke="#9fb3d1" stroke-width="1"/>')
        out.append(f'<circle cx="{cx:.0f}" cy="{y-9}" r="6" fill="none" stroke="#222" stroke-width="1.5"/>')
        out.append(f'<line x1="{cx-5:.0f}" y1="{y-12}" x2="{cx+5:.0f}" y2="{y-6}" stroke="#222" stroke-width="1.5"/>')
    return "".join(out)
def flot(x,y,w,n,col="#2d3e66"):
    pas=w/n
    return "".join(f'<rect x="{x+pas*i+2:.0f}" y="{y-4}" width="{pas-4:.0f}" height="8" fill="{col}" stroke="#9fb3d1" stroke-width="1"/>' for i in range(n))
def dessin(sky):
    taille=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 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,"The upper lake: rain on top, Sky reserve below, no floating panels",14,"middle","bold"))
    s.append(txt(1240,318,{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³",11,"start","bold"))
        s.append(txt(845,394,"no floating PV: salt loop,",11,"start"))
        s.append(txt(845,408,"level that changes every day",11,"start"))
    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(ile(606,352,218,7))
        s.append(txt(845,378,"Upper basin 800,000 m³",12,"start","bold"))
        s.append(txt(845,394,"hybrid island: 168 modules",11,"start","bold","#17415f"))
        s.append(txt(845,408,"+ 9.6 MWp of floating PV",11,"start","bold","#17415f"))
    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"}[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(ile(40,528,190,6))
        s.append(txt(130,570,"Fresh-water lower lake, 12.8 Mm³, 100 ha",11,"middle","bold"))
        s.append(txt(135,585,"hybrid island, 1,144 modules + 58.5 MWp",10,"middle","bold","#17415f"))
        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(130,660,"City lower basin: floating 6 MWp",11,"middle","bold"))
        s.append(txt(130,675,"it produces, and the water stays sheltered",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="90" height="55" fill="#1b4f7a" stroke="#0a3d62" stroke-width="2"/>')
        s.append(vagues(255,772,80,2))
        s.append(flot(252,747,86,3))
        s.append(txt(360,814,"Lower basin reduced to 200,000 m³, level lock",11,"middle","bold"))
        s.append(txt(360,827,"with the lake, classic floating 1.5 MWp",10,"middle","bold","#17415f"))
        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(ile(30,768,190,6))
        s.append('<path d="M 237 772 L 250 772" stroke="#5b8fb5" stroke-width="6"/>')
        s.append(txt(125,814,"Lower lake 6.4 Mm³, 64 ha",11,"middle","bold"))
        s.append(txt(125,827,"hybrid island, 714 modules + 37.8 MWp",10,"middle","bold","#17415f"))
        s.append('<path d="M 13 772 L 0 772" stroke="#5b8fb5" stroke-width="6"/>')
        s.append(txt(135,650,"The lower lake stores water to pump back up;",12,"middle","bold"))
        s.append(txt(135,668,"the O Sky covers it with a hybrid island:",12,"middle"))
        s.append(txt(135,686,"less evaporation, wind sold in the evening",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))
    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:"V20 O Sky 2: the Sky 2 whose waters also produce",3:"V20 O Sky 3: the Sky 3 whose fresh-water lake also produces"}[sky],15,"middle","bold","#17415f"))
    lignes={2:["Hybrid islands on the lower lake (64 ha) and the upper basin (16 ha),","classic floating on the reduced lower basin: 882 rotors + 49 MWp","+96 GWh sold a year, including 18 of wind sold at night and in winter","59.2 M€ of floating minus 9 M€ of basin: +6.8 M€ a year, 2.2 M€ net, 28.3 €/MWh","1,352 GWh sold instead of 1,261: it produces more, it does not store more","82 ha covered: 0.4 million m³ of evaporation avoided a year"],3:["Hybrid island on the fresh-water lower lake (100 ha), classic floating","on the city's lower basin: 1,144 rotors + 64 MWp","+78 GWh sold a year, including 26 of wind","77.6 M€ added to the Sky 3, +7.2 M€ a year, 1.3 M€ net, payback 10.7 years","1,880 GWh sold instead of 1,802: more production, same storage","Never floating panels on the salt loop or on the rain lake"]}[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={2:"Hydroloop Muhleisen 2026 V20 O Sky 2: the Sky 2 plus islands of hybrid rotors and floating PV on its waters",3:"Hydroloop Muhleisen 2026 V20 O Sky 3: the Sky 3 plus a hybrid island on its fresh-water lake"}
    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):
    cairosvg.svg2png(bytestring=dessin(k).encode("utf-8"),write_to=f"schema_hydroloop_muhleisen_2026_v20_o_sky_{k}.png",output_width=W,output_height=H)
print("ok")
