// level-art.jsx — Dibujos en corte de medidores de nivel (Módulo 3 · Tecnologías de medición)
// Va después de flujo-art.jsx, temp-art.jsx y press-art.jsx (usa FA, TA, PA, FlowDefs, TempDefs, PTX, DPCell…).
// Cada <svg> que use estas piezas debe incluir <FlowDefs /> y <TempDefs /> una vez.

const LA = { oil: '#e9c46a', water: '#9cc8f2', foam: '#f4f1ea', sludge: '#8a7a62', fork: '#9aa6b4', flagR: '#d7263d', flagW: '#ffffff', mag: '#2f4a6b' };

// ── Recipiente en corte ─────────────────────────────────────────────────────
// level: 0 a 1 (altura del líquido). iface: altura de la interfase (líquido pesado abajo). closed: tapa abombada.
function Vessel({ x, y, w = 360, h = 520, level = 0.6, color = LA.water, heavy = '#6fa8dc', iface = null, closed = true, foam = 0, wall = 12, t = 0 }) {
  const yb = y + h, ly = yb - h * level, iy = iface != null ? yb - h * iface : null;
  const top = closed ? `M${x} ${y + 40} Q${x} ${y - 20} ${x + w / 2} ${y - 24} Q${x + w} ${y - 20} ${x + w} ${y + 40}` : '';
  const wave = Array.from({ length: 31 }, (_, i) => `${(x + wall + (w - 2 * wall) * i / 30).toFixed(1)} ${(ly + 3 * Math.sin(i * 0.9 + t * 3)).toFixed(1)}`).join(' L');
  return (
    <g>
      <path d={`M${x + wall} ${ly} L${wave} L${x + w - wall} ${yb} L${x + wall} ${yb} Z`} fill={color} opacity="0.8" />
      {iy != null && <rect x={x + wall} y={iy} width={w - 2 * wall} height={yb - iy} fill={heavy} opacity="0.85" />}
      {foam > 0 && <rect x={x + wall} y={ly - 40 * foam} width={w - 2 * wall} height={40 * foam} fill={LA.foam} stroke="#d8d2c4" strokeDasharray="4 4" />}
      <path d={`M${x} ${closed ? y + 40 : y} L${x} ${yb + wall} L${x + w} ${yb + wall} L${x + w} ${closed ? y + 40 : y}`} fill="none" stroke={FA.ink} strokeWidth="3" />
      <rect x={x} y={closed ? y + 40 : y} width={wall} height={yb - (closed ? y + 40 : y)} fill="url(#faHatch)" stroke={FA.ink} strokeWidth="2" />
      <rect x={x + w - wall} y={closed ? y + 40 : y} width={wall} height={yb - (closed ? y + 40 : y)} fill="url(#faHatch)" stroke={FA.ink} strokeWidth="2" />
      <rect x={x} y={yb} width={w} height={wall} fill="url(#faHatch)" stroke={FA.ink} strokeWidth="2" />
      {closed && <path d={top} fill="none" stroke={FA.ink} strokeWidth={wall} />}
      {closed && <path d={top} fill="none" stroke={FA.steel} strokeWidth={wall - 5} />}
    </g>
  );
}

// ── Mirilla (nivel de vidrio) ───────────────────────────────────────────────
function SightGlass({ x, yTop, yBot, level = 0.5, color = LA.water }) {
  const ly = yBot - (yBot - yTop) * level;
  return (
    <g>
      <rect x={x - 14} y={yTop} width="28" height={yBot - yTop} rx="6" fill={TA.glass} stroke={FA.ink} strokeWidth="2.5" />
      <rect x={x - 9} y={ly} width="18" height={yBot - ly - 4} fill={color} />
      <line x1={x - 60} y1={yTop + 10} x2={x - 14} y2={yTop + 10} stroke={FA.ink} strokeWidth="5" />
      <line x1={x - 60} y1={yBot - 10} x2={x - 14} y2={yBot - 10} stroke={FA.ink} strokeWidth="5" />
    </g>
  );
}

// ── Radar sin contacto (antena de bocina) y su haz ──────────────────────────
function RadarHorn({ x, y, s = 1 }) {
  return (
    <g transform={`translate(${x} ${y}) scale(${s})`}>
      <rect x="-50" y="-110" width="100" height="70" rx="26" fill={FA.paint} stroke={FA.ink} strokeWidth="3" />
      <rect x="-18" y="-40" width="36" height="30" fill="url(#taSheathV)" stroke={FA.ink} strokeWidth="2.5" />
      <rect x="-60" y="-12" width="120" height="14" fill="url(#faHatch)" stroke={FA.ink} strokeWidth="2.5" />
      <path d="M-20 2 L-46 70 L46 70 L20 2 Z" fill="url(#taSheath)" stroke={FA.ink} strokeWidth="2.5" />
    </g>
  );
}
// Pulsos que bajan y ecos que suben entre yTop y ySurf. echo: altura del eco (0 a 1).
function RadarBeam({ x, yTop, ySurf, t = 0, color = TA.hot, spread = 0.25, period = 1.6, echo = 1 }) {
  const out = [];
  const L = ySurf - yTop;
  for (let k = 0; k < 3; k++) {
    const p = ((t / period + k / 3) % 1);
    if (p < 0.5) { const yy = yTop + L * p * 2, hw = 20 + (yy - yTop) * spread; out.push(<path key={k} d={`M${x - hw} ${yy - 8} Q${x} ${yy + 10} ${x + hw} ${yy - 8}`} fill="none" stroke={color} strokeWidth="4" opacity={0.9} />); }
    else { const yy = ySurf - L * (p - 0.5) * 2, hw = 20 + (ySurf - yy) * 0.08; out.push(<path key={k} d={`M${x - hw} ${yy + 8} Q${x} ${yy - 10} ${x + hw} ${yy + 8}`} fill="none" stroke={TA.cold} strokeWidth="4" opacity={0.8 * echo} />); }
  }
  return <g>
    <path d={`M${x - 20} ${yTop} L${x - 20 - L * spread} ${ySurf} L${x + 20 + L * spread} ${ySurf} L${x + 20} ${yTop} Z`} fill={color} opacity="0.07" />
    {out}
  </g>;
}

// ── Radar de onda guiada (sonda) ────────────────────────────────────────────
function GWRProbe({ x, yTop, yBot, ySurf, yIf = null, t = 0, period = 1.8 }) {
  const p = (t / period) % 1;
  const yp = p < 0.5 ? yTop + (ySurf - yTop) * p * 2 : ySurf - (ySurf - yTop) * (p - 0.5) * 2;
  return (
    <g>
      <rect x={x - 5} y={yTop} width="10" height={yBot - yTop} fill={FA.steelD} stroke={FA.ink} strokeWidth="1.5" />
      <circle cx={x} cy={yBot} r="9" fill={FA.steelD} stroke={FA.ink} strokeWidth="1.5" />
      <rect x={x - 14} y={yp - 10} width="28" height="20" rx="6" fill={p < 0.5 ? TA.hot : TA.cold} opacity="0.85" />
      <line x1={x - 30} y1={ySurf} x2={x + 30} y2={ySurf} stroke={TA.hot} strokeWidth="3" />
      {yIf != null && <line x1={x - 30} y1={yIf} x2={x + 30} y2={yIf} stroke={TA.cold} strokeWidth="3" strokeDasharray="6 4" />}
    </g>
  );
}

// ── Ultrasónico: transductor y frentes de sonido ────────────────────────────
function USTransducer({ x, y, ySurf, t = 0, foam = 0 }) {
  const arcs = Array.from({ length: 4 }, (_, k) => { const p = ((t * 0.7 + k / 4) % 1); const yy = y + 20 + (ySurf - y - 20) * p; const r = 30 + (yy - y) * 0.35; return <path key={k} d={`M${x - r} ${yy - 12} Q${x} ${yy + 12} ${x + r} ${yy - 12}`} fill="none" stroke={TL4.blue} strokeWidth="3" opacity={(1 - p) * (1 - 0.7 * foam)} />; });
  return (
    <g>
      {arcs}
      <rect x={x - 46} y={y - 70} width="92" height="70" rx="20" fill={FA.paint} stroke={FA.ink} strokeWidth="3" />
      <rect x={x - 38} y={y} width="76" height="20" rx="4" fill={FA.ink} />
    </g>
  );
}

// ── Desplazador con brazo de torsión ────────────────────────────────────────
function Displacer({ x, yTop, len = 300, w = 50, yLiq, defl = 0 }) {
  const yd = yTop + 80 + defl * 10;
  const sub = Math.max(0, Math.min(len, yd + len - yLiq));
  return (
    <g>
      <rect x={x - 70} y={yTop - 30} width="140" height="60" rx="10" fill={FA.paint} stroke={FA.ink} strokeWidth="3" />
      <line x1={x} y1={yTop + 30} x2={x} y2={yd} stroke={FA.ink} strokeWidth="3" />
      <g transform={`rotate(${-defl * 6} ${x} ${yTop})`}><line x1={x - 60} y1={yTop} x2={x + 60} y2={yTop} stroke={FA.steelD} strokeWidth="6" /></g>
      <rect x={x - w / 2} y={yd} width={w} height={len} rx={w / 2} fill="url(#taSheathV)" stroke={FA.ink} strokeWidth="2.5" />
      {sub > 0 && <rect x={x - w / 2 + 3} y={yd + len - sub} width={w - 6} height={sub - 3} rx={w / 2 - 3} fill={TL4.blue} opacity="0.25" />}
    </g>
  );
}

// ── Indicador magnético de nivel (cámara lateral con banderas) ──────────────
function MagGauge({ x, yTop, yBot, level = 0.5, color = LA.water }) {
  const ly = yBot - (yBot - yTop) * level, n = 22, step = (yBot - yTop) / n;
  return (
    <g>
      <rect x={x - 20} y={yTop} width="40" height={yBot - yTop} rx="10" fill="#eef3f8" stroke={FA.ink} strokeWidth="3" />
      <rect x={x - 15} y={ly} width="30" height={yBot - ly - 4} fill={color} opacity="0.7" />
      <rect x={x - 14} y={ly - 40} width="28" height="60" rx="12" fill={FA.steel} stroke={FA.ink} strokeWidth="2" />
      <rect x={x - 6} y={ly - 16} width="12" height="12" fill={LA.mag} />
      {Array.from({ length: n }, (_, i) => { const yy = yBot - (i + 1) * step; return <rect key={i} x={x + 26} y={yy + 2} width="34" height={step - 4} rx="2" fill={yy + step / 2 > ly - 10 ? LA.flagR : LA.flagW} stroke={FA.ink} strokeWidth="1.2" />; })}
    </g>
  );
}

// ── Magnetostrictivo: guía de onda con flotador ─────────────────────────────
function Magnetostrictive({ x, yTop, yBot, yFloat, yIf = null, t = 0 }) {
  const p = (t * 0.8) % 1;
  const yPulse = yTop + (yFloat - yTop) * Math.min(1, p * 2);
  const back = p > 0.5 ? yFloat - (yFloat - yTop) * (p - 0.5) * 2 : null;
  return (
    <g>
      <rect x={x - 6} y={yTop} width="12" height={yBot - yTop} fill={FA.steelD} stroke={FA.ink} strokeWidth="1.5" />
      <rect x={x - 34} y={yFloat - 22} width="68" height="44" rx="18" fill={FA.steel} stroke={FA.ink} strokeWidth="2.5" />
      <rect x={x - 30} y={yFloat - 5} width="60" height="10" fill={LA.mag} opacity="0.8" />
      {yIf != null && <g><rect x={x - 30} y={yIf - 18} width="60" height="36" rx="14" fill="#c7d3df" stroke={FA.ink} strokeWidth="2.5" /><rect x={x - 26} y={yIf - 4} width="52" height="8" fill={LA.mag} opacity="0.8" /></g>}
      {p <= 0.5 && <line x1={x} y1={yTop} x2={x} y2={yPulse} stroke={TA.hot} strokeWidth="5" />}
      {back != null && <path d={`M${x - 10} ${back} q10 -8 20 0 q-10 8 -20 0`} fill="none" stroke={TA.cold} strokeWidth="4" />}
    </g>
  );
}

// ── Sonda capacitiva ────────────────────────────────────────────────────────
function CapProbe({ x, yTop, yBot, wallX, ySurf }) {
  return (
    <g>
      <rect x={x - 8} y={yTop} width="16" height={yBot - yTop} rx="6" fill="#f2b134" stroke={FA.ink} strokeWidth="2" />
      {Array.from({ length: 9 }, (_, i) => { const yy = yTop + 30 + i * (yBot - yTop - 40) / 8; const wet = yy > ySurf; return <path key={i} d={`M${x + 10} ${yy} Q${(x + wallX) / 2} ${yy - 10} ${wallX} ${yy}`} fill="none" stroke={wet ? TA.hot : FA.mut} strokeWidth={wet ? 2.5 : 1.5} strokeDasharray={wet ? '' : '4 4'} />; })}
    </g>
  );
}

// ── Horquilla vibratoria ────────────────────────────────────────────────────
// flip: montada en la pared derecha (x = pared): carcasa afuera, dientes hacia adentro del tanque.
function VibFork({ x, y, covered = false, t = 0, horiz = true, flip = false }) {
  const a = covered ? 1.5 * Math.sin(t * 20) : 4 * Math.sin(t * 60);
  return (
    <g transform={(horiz ? `translate(${x} ${y})` : `translate(${x} ${y}) rotate(90)`) + (flip ? ' scale(-1 1) translate(-8 0)' : '')}>
      <rect x="-80" y="-24" width="70" height="48" rx="8" fill={FA.paint} stroke={FA.ink} strokeWidth="2.5" />
      <rect x="-10" y="-14" width="30" height="28" fill="url(#taSheathV)" stroke={FA.ink} strokeWidth="2" />
      <rect x="20" y={-14 + a} width="80" height="8" rx="4" fill={LA.fork} stroke={FA.ink} strokeWidth="1.5" />
      <rect x="20" y={6 - a} width="80" height="8" rx="4" fill={LA.fork} stroke={FA.ink} strokeWidth="1.5" />
    </g>
  );
}

// ── Interruptor de flotador basculante ──────────────────────────────────────
function FloatSwitch({ x, y, up = false }) {
  const ang = up ? -30 : 35;
  return (
    <g>
      <rect x={x - 30} y={y - 30} width="30" height="60" fill={FA.paint} stroke={FA.ink} strokeWidth="2.5" />
      <g transform={`rotate(${ang} ${x} ${y})`}><line x1={x} y1={y} x2={x + 110} y2={y} stroke={FA.ink} strokeWidth="4" /><ellipse cx={x + 140} cy={y} rx="36" ry="22" fill="#f3e9cf" stroke={FA.ink} strokeWidth="2.5" /></g>
    </g>
  );
}

// ── Radiométrico ────────────────────────────────────────────────────────────
function Radiometric({ xs, xd, yTop, yBot, ySurf, t = 0 }) {
  const rays = Array.from({ length: 7 }, (_, i) => { const yy = yTop + (yBot - yTop) * i / 6; const absorbed = yy > ySurf; return <line key={i} x1={xs + 40} y1={(yTop + yBot) / 2} x2={xd - 10} y2={yy} stroke={TA.red} strokeWidth="2" strokeDasharray="6 8" strokeDashoffset={-t * 40} opacity={absorbed ? 0.2 : 0.8} />; });
  return (
    <g>
      {rays}
      <rect x={xs - 30} y={(yTop + yBot) / 2 - 40} width="70" height="80" rx="8" fill="#f2b134" stroke={FA.ink} strokeWidth="3" />
      <text x={xs + 5} y={(yTop + yBot) / 2 + 8} textAnchor="middle" fontFamily={MONO4} fontSize="22" fontWeight="700" fill={FA.ink}>☢</text>
      <rect x={xd - 10} y={yTop} width="30" height={yBot - yTop} rx="8" fill={FA.paint} stroke={FA.ink} strokeWidth="3" />
    </g>
  );
}

// ── Celda de carga bajo una pata ────────────────────────────────────────────
function LoadCell({ x, y, w = 70 }) {
  return (
    <g>
      <rect x={x - w / 2} y={y} width={w} height="30" rx="4" fill="url(#taSheath)" stroke={FA.ink} strokeWidth="2.5" />
      <rect x={x - w / 2 - 10} y={y + 30} width={w + 20} height="12" fill="url(#faHatch)" stroke={FA.ink} strokeWidth="2" />
      <line x1={x + w / 2} y1={y + 15} x2={x + w / 2 + 40} y2={y + 15} stroke={TA.cu} strokeWidth="3" />
    </g>
  );
}

Object.assign(window, { LA, Vessel, SightGlass, RadarHorn, RadarBeam, GWRProbe, USTransducer, Displacer, MagGauge, Magnetostrictive, CapProbe, VibFork, FloatSwitch, Radiometric, LoadCell });
