import os
import subprocess
from PIL import Image

ICONS_SVG = {
    # 1. Obradores: Horno tradicional de obrador con cúpula, ladrillos, fuego/pan dorado dentro, pala y rodillo
    "icon_prod_obradores": """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">
  <defs>
    <filter id="shadow" x="-5%" y="-5%" width="110%" height="115%">
      <feDropShadow dx="0" dy="8" stdDeviation="10" flood-color="#14334F" flood-opacity="0.2"/>
    </filter>
  </defs>
  <g filter="url(#shadow)">
    <!-- Base platform -->
    <rect x="80" y="420" width="352" height="36" rx="14" fill="#1C4468" stroke="#14334F" stroke-width="6"/>
    <!-- Oven lower block -->
    <rect x="110" y="270" width="292" height="150" rx="16" fill="#DCE8F2" stroke="#1C4468" stroke-width="12"/>
    <path d="M110 320 L402 320 M110 370 L402 370" stroke="#B8CFE0" stroke-width="6" stroke-dasharray="24 16"/>
    <!-- Oven dome -->
    <path d="M120 270 C120 120, 392 120, 392 270 Z" fill="#1C4468" stroke="#14334F" stroke-width="12"/>
    <!-- Chimney -->
    <rect x="300" y="80" width="56" height="80" rx="10" fill="#D29C38" stroke="#B37F22" stroke-width="10"/>
    <rect x="290" y="70" width="76" height="20" rx="8" fill="#1C4468"/>
    <!-- Oven mouth arch -->
    <path d="M186 380 L186 280 C186 200, 326 200, 326 280 L326 380 Z" fill="#F7F4EC" stroke="#1C4468" stroke-width="12"/>
    <path d="M200 375 L200 286 C200 220, 312 220, 312 286 L312 375 Z" fill="#D29C38"/>
    <!-- Warm inner glow / flame -->
    <ellipse cx="256" cy="350" rx="44" ry="24" fill="#E57E25"/>
    <ellipse cx="256" cy="352" rx="28" ry="14" fill="#FFDE59"/>
    <!-- Fresh Bread Loaf inside -->
    <path d="M228 348 C228 328, 284 328, 284 348 Z" fill="#B37F22" stroke="#F7F4EC" stroke-width="4"/>
    <!-- Baker's paddle on side -->
    <line x1="84" y1="410" x2="160" y2="150" stroke="#B37F22" stroke-width="14" stroke-linecap="round"/>
    <rect x="136" y="110" width="50" height="74" rx="12" fill="#D29C38" stroke="#B37F22" stroke-width="8" transform="rotate(-16 161 147)"/>
    <!-- Rolling pin on right -->
    <rect x="360" y="240" width="24" height="150" rx="10" fill="#D29C38" stroke="#B37F22" stroke-width="8" transform="rotate(18 372 315)"/>
  </g>
</svg>""",

    # 2. Recetas: Libro de recetas/fórmulas de pastelería con cinta marcapáginas y varilla batidora
    "icon_prod_recetas": """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">
  <defs>
    <filter id="shadow" x="-5%" y="-5%" width="110%" height="115%">
      <feDropShadow dx="0" dy="8" stdDeviation="10" flood-color="#14334F" flood-opacity="0.2"/>
    </filter>
  </defs>
  <g filter="url(#shadow)">
    <!-- Book back cover -->
    <rect x="90" y="70" width="310" height="380" rx="24" fill="#14334F"/>
    <!-- Book front cover / body -->
    <rect x="106" y="80" width="294" height="360" rx="20" fill="#1C4468" stroke="#14334F" stroke-width="10"/>
    <!-- Spine binding accents -->
    <rect x="106" y="80" width="46" height="360" rx="6" fill="#14334F"/>
    <line x1="129" y1="130" x2="129" y2="170" stroke="#D29C38" stroke-width="8" stroke-linecap="round"/>
    <line x1="129" y1="240" x2="129" y2="280" stroke="#D29C38" stroke-width="8" stroke-linecap="round"/>
    <line x1="129" y1="350" x2="129" y2="390" stroke="#D29C38" stroke-width="8" stroke-linecap="round"/>
    <!-- Front Plate / Label -->
    <rect x="180" y="140" width="180" height="230" rx="14" fill="#F7F4EC" stroke="#D29C38" stroke-width="8"/>
    <!-- Chef Hat / Whisk emblem on label -->
    <path d="M230 220 C220 200, 240 180, 256 185 C270 178, 292 200, 282 220 Z" fill="#D29C38"/>
    <rect x="232" y="222" width="48" height="14" rx="4" fill="#1C4468"/>
    <!-- Recipe text lines -->
    <line x1="206" y1="265" x2="334" y2="265" stroke="#1C4468" stroke-width="8" stroke-linecap="round"/>
    <line x1="206" y1="295" x2="310" y2="295" stroke="#1C4468" stroke-width="8" stroke-linecap="round"/>
    <line x1="206" y1="325" x2="280" y2="325" stroke="#D29C38" stroke-width="8" stroke-linecap="round"/>
    <!-- Golden bookmark ribbon hanging down -->
    <path d="M300 70 L300 130 L320 110 L340 130 L340 70 Z" fill="#D29C38" stroke="#B37F22" stroke-width="4"/>
    <!-- Whisk crossed in front on bottom-right -->
    <g transform="translate(320, 290) rotate(-35)">
      <line x1="0" y1="90" x2="0" y2="0" stroke="#D29C38" stroke-width="12" stroke-linecap="round"/>
      <ellipse cx="0" cy="-40" rx="28" ry="46" fill="none" stroke="#1C4468" stroke-width="10"/>
      <ellipse cx="0" cy="-40" rx="14" ry="46" fill="none" stroke="#1C4468" stroke-width="8"/>
      <line x1="0" y1="-86" x2="0" y2="6" stroke="#1C4468" stroke-width="8"/>
    </g>
  </g>
</svg>""",

    # 3. Materias Primas: Saco de harina/ingredientes con espiga de trigo y cuchara dosificadora
    "icon_prod_materias_primas": """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">
  <defs>
    <filter id="shadow" x="-5%" y="-5%" width="110%" height="115%">
      <feDropShadow dx="0" dy="8" stdDeviation="10" flood-color="#14334F" flood-opacity="0.2"/>
    </filter>
  </defs>
  <g filter="url(#shadow)">
    <!-- Flour Sack body -->
    <path d="M140 440 C120 300, 150 200, 200 170 C220 160, 292 160, 312 170 C362 200, 392 300, 372 440 C350 460, 160 460, 140 440 Z" fill="#F7F4EC" stroke="#1C4468" stroke-width="14"/>
    <!-- Sack crinkled top / neck -->
    <path d="M190 170 C180 120, 332 120, 322 170 Z" fill="#DCE8F2" stroke="#1C4468" stroke-width="12"/>
    <!-- Tied cord -->
    <rect x="180" y="160" width="152" height="24" rx="8" fill="#1C4468"/>
    <circle cx="256" cy="172" r="14" fill="#D29C38" stroke="#B37F22" stroke-width="4"/>
    <!-- Sack stamp/badge -->
    <rect x="195" y="240" width="122" height="130" rx="14" fill="#DCE8F2" stroke="#1C4468" stroke-width="8"/>
    <!-- Golden Wheat ear inside badge -->
    <line x1="256" y1="260" x2="256" y2="350" stroke="#B37F22" stroke-width="6" stroke-linecap="round"/>
    <ellipse cx="242" cy="276" rx="14" ry="8" fill="#D29C38" stroke="#B37F22" stroke-width="3" transform="rotate(-30 242 276)"/>
    <ellipse cx="270" cy="276" rx="14" ry="8" fill="#D29C38" stroke="#B37F22" stroke-width="3" transform="rotate(30 270 276)"/>
    <ellipse cx="242" cy="298" rx="14" ry="8" fill="#D29C38" stroke="#B37F22" stroke-width="3" transform="rotate(-30 242 298)"/>
    <ellipse cx="270" cy="298" rx="14" ry="8" fill="#D29C38" stroke="#B37F22" stroke-width="3" transform="rotate(30 270 298)"/>
    <ellipse cx="242" cy="320" rx="14" ry="8" fill="#D29C38" stroke="#B37F22" stroke-width="3" transform="rotate(-30 242 320)"/>
    <ellipse cx="270" cy="320" rx="14" ry="8" fill="#D29C38" stroke="#B37F22" stroke-width="3" transform="rotate(30 270 320)"/>
    <!-- Flour scoop on right -->
    <path d="M340 370 L430 330 C450 370, 410 420, 360 410 Z" fill="#D29C38" stroke="#1C4468" stroke-width="10"/>
    <line x1="410" y1="340" x2="460" y2="300" stroke="#1C4468" stroke-width="12" stroke-linecap="round"/>
  </g>
</svg>"""
}

os.makedirs("scratch/rendered", exist_ok=True)
chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

for name, svg in ICONS_SVG.items():
    html = f"<!DOCTYPE html><html><head><style>body{{margin:0;padding:0;background:transparent;overflow:hidden;}}svg{{display:block;width:512px;height:512px;}}</style></head><body>{svg}</body></html>"
    html_file = f"scratch/rendered/{name}.html"
    png_file = f"scratch/rendered/{name}.png"
    with open(html_file, "w", encoding="utf-8") as f:
        f.write(html)
    cmd = [
        chrome_path, "--headless=new", "--disable-gpu",
        "--default-background-color=00000000", "--window-size=512,512",
        f"--screenshot={os.path.abspath(png_file)}",
        f"file:///{os.path.abspath(html_file)}"
    ]
    subprocess.run(cmd, check=True)
    im = Image.open(png_file)
    im42 = im.resize((42, 42), Image.Resampling.LANCZOS)
    im42.save(f"scratch/rendered/{name}_42.png")
    print(f"Generated {name} master and 42x42!")
