/* global React */
// NUON hero — animated isometric district diagram
// Reuses the visual vocabulary from slides/10-isometric.html, adapted for the
// home page with subtle live animations (pulsing core, flowing energy lines).

const { useState, useEffect } = React;

function IsoHero({ copy }) {
  return (
    <div className="hero__stage">
      <span className="hero__tag tl">{copy.stage_tl}</span>
      <Clock className="hero__tag tr" prefix="↻ CYCLE" />
      <span className="hero__tag bl">{copy.stage_bl}</span>

      <svg viewBox="0 0 1200 660" preserveAspectRatio="xMidYMid meet" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
        <defs>
          <linearGradient id="ih-top" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#FFFFFF" />
            <stop offset="100%" stopColor="#F1F5FA" />
          </linearGradient>
          <linearGradient id="ih-left" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#E4ECF5" />
            <stop offset="100%" stopColor="#C9D6E5" />
          </linearGradient>
          <linearGradient id="ih-right" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#D4DEEB" />
            <stop offset="100%" stopColor="#B0C0D5" />
          </linearGradient>
          <linearGradient id="ih-cap-cian" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#A6E5FF" />
            <stop offset="100%" stopColor="#76D6FF" />
          </linearGradient>
          <linearGradient id="ih-cap-azul" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#5BB6FF" />
            <stop offset="100%" stopColor="#0096FF" />
          </linearGradient>
          <linearGradient id="ih-cap-indigo" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#A4A9FF" />
            <stop offset="100%" stopColor="#7A81FF" />
          </linearGradient>
          <linearGradient id="ih-cap-magenta" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#E8B0FF" />
            <stop offset="100%" stopColor="#D883FF" />
          </linearGradient>
          <linearGradient id="ih-cap-rosa" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#FFD9EC" />
            <stop offset="100%" stopColor="#FDBFE8" />
          </linearGradient>
          <marker id="ih-arr" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
            <path d="M0,0 L10,5 L0,10 z" fill="currentColor" />
          </marker>
          <radialGradient id="ih-core-halo" cx="50%" cy="50%" r="50%">
            <stop offset="0%" stopColor="#0096FF" stopOpacity="0.35" />
            <stop offset="100%" stopColor="#0096FF" stopOpacity="0" />
          </radialGradient>
        </defs>

        {/* Subtle ground diamond */}
        <g opacity="0.5">
          <path d="M 600 100 L 1100 350 L 600 600 L 100 350 Z" fill="none" stroke="#0096FF" strokeWidth="1" strokeDasharray="4 4" opacity="0.3" />
        </g>

        {/* BLOCK 1 — INDUSTRIAL (center) */}
        <g>
          <path d="M 480 380 L 600 440 L 600 320 L 480 260 Z" fill="url(#ih-right)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 480 380 L 360 440 L 360 320 L 480 260 Z" fill="url(#ih-left)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 480 260 L 600 320 L 480 380 L 360 320 Z" fill="url(#ih-top)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 480 260 L 600 320 L 580 330 L 470 274 Z" fill="url(#ih-cap-azul)" opacity="0.85" />
          {/* roof detail */}
          <line x1="420" y1="350" x2="540" y2="350" stroke="#9DAEC4" strokeWidth="0.6" strokeDasharray="2 2" opacity="0.6" />
        </g>

        {/* BLOCK 2 — GENERATION (top-right) */}
        <g>
          <path d="M 760 360 L 850 405 L 850 285 L 760 240 Z" fill="url(#ih-right)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 760 360 L 670 405 L 670 285 L 760 240 Z" fill="url(#ih-left)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 760 240 L 850 285 L 760 330 L 670 285 Z" fill="url(#ih-top)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 760 200 L 830 235 L 760 270 L 690 235 Z" fill="url(#ih-cap-indigo)" stroke="#7A81FF" strokeWidth="0.8" />
          <path d="M 690 235 L 690 270 L 760 305 L 760 270 Z" fill="#A4A9FF" opacity="0.85" stroke="#7A81FF" strokeWidth="0.8" />
          <path d="M 760 270 L 760 305 L 830 270 L 830 235 Z" fill="#7A81FF" opacity="0.9" stroke="#7A81FF" strokeWidth="0.8" />
        </g>

        {/* BLOCK 3 — BATTERY (front-center) */}
        <g>
          <path d="M 580 500 L 720 570 L 720 510 L 580 440 Z" fill="url(#ih-right)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 580 500 L 440 570 L 440 510 L 580 440 Z" fill="url(#ih-left)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 580 440 L 720 510 L 580 500 L 440 510 Z" fill="url(#ih-top)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 580 440 L 720 510 L 700 520 L 575 458 Z" fill="url(#ih-cap-cian)" opacity="0.9" />
          {/* cells */}
          <line x1="500" y1="475" x2="500" y2="540" stroke="#9DAEC4" strokeWidth="0.7" strokeDasharray="2 2" opacity="0.5" />
          <line x1="540" y1="495" x2="540" y2="555" stroke="#9DAEC4" strokeWidth="0.7" strokeDasharray="2 2" opacity="0.5" />
          <line x1="640" y1="495" x2="640" y2="555" stroke="#9DAEC4" strokeWidth="0.7" strokeDasharray="2 2" opacity="0.5" />
          <line x1="680" y1="475" x2="680" y2="540" stroke="#9DAEC4" strokeWidth="0.7" strokeDasharray="2 2" opacity="0.5" />
          {/* pulsing SOC indicator */}
          <circle cx="580" cy="470" r="3" fill="#76D6FF">
            <animate attributeName="opacity" values="1;0.3;1" dur="2.2s" repeatCount="indefinite" />
          </circle>
        </g>

        {/* BLOCK 4 — DEMAND (back-left) */}
        <g>
          <path d="M 280 340 L 360 380 L 360 305 L 280 265 Z" fill="url(#ih-right)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 280 340 L 200 380 L 200 305 L 280 265 Z" fill="url(#ih-left)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 280 265 L 360 305 L 280 340 L 200 305 Z" fill="url(#ih-top)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 280 265 L 360 305 L 350 310 L 275 273 Z" fill="url(#ih-cap-magenta)" opacity="0.85" />
        </g>

        {/* BLOCK 5 — EV FLEET (front-right) */}
        <g>
          <path d="M 870 510 L 990 575 L 990 535 L 870 470 Z" fill="url(#ih-right)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 870 510 L 750 575 L 750 535 L 870 470 Z" fill="url(#ih-left)" stroke="#9DAEC4" strokeWidth="1" />
          <path d="M 870 470 L 990 535 L 870 510 L 750 535 Z" fill="url(#ih-top)" stroke="#9DAEC4" strokeWidth="1" />
          <circle cx="800" cy="500" r="3" fill="#FDBFE8">
            <animate attributeName="opacity" values="1;0.3;1" dur="2s" begin="0.0s" repeatCount="indefinite" />
          </circle>
          <circle cx="840" cy="510" r="3" fill="#FDBFE8">
            <animate attributeName="opacity" values="1;0.3;1" dur="2s" begin="0.4s" repeatCount="indefinite" />
          </circle>
          <circle cx="900" cy="510" r="3" fill="#FDBFE8">
            <animate attributeName="opacity" values="1;0.3;1" dur="2s" begin="0.8s" repeatCount="indefinite" />
          </circle>
          <circle cx="940" cy="520" r="3" fill="#FDBFE8">
            <animate attributeName="opacity" values="1;0.3;1" dur="2s" begin="1.2s" repeatCount="indefinite" />
          </circle>
        </g>

        {/* CORE NODE */}
        <g>
          <circle cx="600" cy="120" r="50" fill="url(#ih-core-halo)">
            <animate attributeName="r" values="40;60;40" dur="3s" repeatCount="indefinite" />
          </circle>
          <circle cx="600" cy="120" r="22" fill="none" stroke="#0096FF" strokeWidth="1">
            <animate attributeName="r" values="22;48;22" dur="2.4s" repeatCount="indefinite" />
            <animate attributeName="opacity" values="0.9;0;0.9" dur="2.4s" repeatCount="indefinite" />
          </circle>
          <circle cx="600" cy="120" r="22" fill="#FFFFFF" stroke="#0096FF" strokeWidth="1.5" />
          <circle cx="600" cy="120" r="22" fill="none" stroke="#7A81FF" strokeWidth="1" strokeDasharray="3 3" opacity="0.7">
            <animateTransform attributeName="transform" attributeType="XML" type="rotate" from="0 600 120" to="360 600 120" dur="20s" repeatCount="indefinite" />
          </circle>
          <circle cx="600" cy="120" r="6" fill="#0096FF" />
          <text x="600" y="92" textAnchor="middle" fontFamily="IBM Plex Mono, monospace" fontSize="11" letterSpacing="1.5" fill="#0096FF">[ NUON · CORE ]</text>
          <text x="600" y="156" textAnchor="middle" fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1" fill="#6B7A96">DIGITAL TWIN · OPTIMIZER</text>
        </g>

        {/* FLOWS */}
        <g fill="none" strokeWidth="1.8" strokeLinecap="round">
          {/* industrial (azul) */}
          <path d="M 600 142 Q 540 200 480 240" stroke="#0096FF" markerEnd="url(#ih-arr)" style={{ color: "#0096FF" }} className="flow-anim" />
          {/* generation (indigo) */}
          <path d="M 612 138 Q 700 170 760 200" stroke="#7A81FF" markerEnd="url(#ih-arr)" style={{ color: "#7A81FF" }} className="flow-anim" />
          {/* demand (magenta) */}
          <path d="M 588 138 Q 430 200 290 260" stroke="#D883FF" markerEnd="url(#ih-arr)" style={{ color: "#D883FF" }} className="flow-anim" />
          {/* battery (cian) */}
          <path d="M 600 142 Q 600 280 580 432" stroke="#76D6FF" markerEnd="url(#ih-arr)" style={{ color: "#76D6FF" }} className="flow-anim" />
          {/* EV (rosa) */}
          <path d="M 614 140 Q 760 280 870 462" stroke="#FDBFE8" strokeWidth="2.2" markerEnd="url(#ih-arr)" style={{ color: "#FDBFE8" }} className="flow-anim" />
        </g>

        {/* feedback ring */}
        <g fill="none" stroke="#9DAEC4" strokeWidth="0.8" strokeDasharray="3 3" opacity="0.6">
          <path d="M 1000 540 Q 1130 500 1130 280 Q 1130 100 1020 90 Q 800 80 622 100" markerEnd="url(#ih-arr)" style={{ color: "#9DAEC4" }} />
        </g>
        <text x="1140" y="285" fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1.2" fill="#6B7A96">[ FEEDBACK ]</text>

        {/* axes */}
        <g opacity="0.55">
          <line x1="40" y1="540" x2="80" y2="560" stroke="#0096FF" strokeWidth="0.8" />
          <line x1="40" y1="540" x2="40" y2="500" stroke="#0096FF" strokeWidth="0.8" />
          <line x1="40" y1="540" x2="80" y2="520" stroke="#0096FF" strokeWidth="0.8" />
          <text x="86" y="563" fontFamily="IBM Plex Mono, monospace" fontSize="9" fill="#0096FF">x</text>
          <text x="40" y="494" fontFamily="IBM Plex Mono, monospace" fontSize="9" fill="#0096FF">z</text>
          <text x="86" y="523" fontFamily="IBM Plex Mono, monospace" fontSize="9" fill="#0096FF">y</text>
        </g>

        {/* labels */}
        <g fontFamily="IBM Plex Mono, monospace" fontSize="10" letterSpacing="1" fill="#6B7A96">
          <line x1="220" y1="270" x2="160" y2="240" stroke="#9DAEC4" strokeWidth="0.7" />
          <text x="156" y="232" textAnchor="end">04 · {copy.legend[3][0].toUpperCase()}</text>
          <line x1="450" y1="280" x2="450" y2="220" stroke="#9DAEC4" strokeWidth="0.7" />
          <text x="450" y="212" textAnchor="middle">01 · {copy.legend[0][0].toUpperCase()}</text>
          <line x1="760" y1="200" x2="760" y2="170" stroke="#9DAEC4" strokeWidth="0.7" />
          <text x="760" y="162" textAnchor="middle">02 · {copy.legend[1][0].toUpperCase()}</text>
          <line x1="580" y1="450" x2="350" y2="585" stroke="#9DAEC4" strokeWidth="0.7" />
          <text x="346" y="588" textAnchor="end">03 · {copy.legend[2][0].toUpperCase()}</text>
          <line x1="930" y1="490" x2="1000" y2="450" stroke="#9DAEC4" strokeWidth="0.7" />
          <text x="1004" y="450">05 · {copy.legend[4][0].toUpperCase()}</text>
        </g>
      </svg>

      <div className="hero__legend">
        <div className="head">{copy.legend_head}</div>
        {copy.legend.map((row, i) => {
          const swatches = ["#0096FF", "#7A81FF", "#76D6FF", "#D883FF", "#FDBFE8"];
          return (
            <div className="row" key={i}>
              <span className="swatch" style={{ background: swatches[i] }}></span>
              <span>{row[0]}</span>
              <span className="num">{row[1]}</span>
            </div>
          );
        })}
      </div>
    </div>
  );
}

// Reloj de ciclo — la marca es Bogotá; zona y etiqueta viven juntas para que no
// se desincronicen, y el formatter se construye una sola vez (no por tick/instancia).
const CLOCK_ZONE = { tz: "America/Bogota", label: "UTC-5" };
const CLOCK_FMT = new Intl.DateTimeFormat("en-US", {
  timeZone: CLOCK_ZONE.tz, hour12: false,
  hour: "2-digit", minute: "2-digit", second: "2-digit",
});

// Live cycle clock — ticking timestamp
function Clock({ className, prefix }) {
  const [t, setT] = useState(new Date());
  useEffect(() => {
    const id = setInterval(() => setT(new Date()), 1000);
    return () => clearInterval(id);
  }, []);
  const time = CLOCK_FMT.format(t).replace(/^24/, "00"); // Intl puede emitir "24:mm:ss" a medianoche
  return <span className={className}>{prefix} {time} {CLOCK_ZONE.label}</span>;
}

function TypoHero({ copy }) {
  return (
    <div className="hero__stage">
      <span className="hero__tag tl" style={{ color: "rgba(255,255,255,.6)" }}>{copy.stage_tl}</span>
      <Clock className="hero__tag tr" prefix="↻" />
      <span className="hero__tag bl" style={{ color: "rgba(255,255,255,.6)" }}>v.4.0 · BLUEPRINT</span>

      <div className="typo-hero">
        <div>{copy.typo_a}</div>
        <div>{copy.typo_b}</div>
        <div><span className="grad">{copy.typo_c}</span><span style={{ color: "#FDBFE8" }}>{copy.typo_dot}</span></div>
      </div>

      {/* Floating spectrum chips */}
      <div style={{ position: "absolute", bottom: 24, left: 24, display: "flex", gap: 6, zIndex: 2 }}>
        {["#76D6FF", "#0096FF", "#7A81FF", "#D883FF", "#FDBFE8"].map((c, i) => (
          <div key={i} style={{ width: 36, height: 6, borderRadius: 999, background: c, opacity: 0.9 }}></div>
        ))}
      </div>
      <div style={{ position: "absolute", bottom: 24, right: 24, fontFamily: "var(--font-mono)", fontSize: 10, letterSpacing: ".14em", color: "rgba(255,255,255,.5)", textTransform: "uppercase", zIndex: 2 }}>
        [ TIPOGRAFÍA · BEBAS NEUE 400 ]
      </div>
    </div>
  );
}

window.IsoHero = IsoHero;
window.TypoHero = TypoHero;
window.Clock = Clock;
