// Products section — flagship Dr. Alice on top, then a 3-up grid of Global Hospital / Connect / Vovoco.

// ------- Visual treatments per product (no real photography, so we draw the mood) -------

function DrAliceVisual() {
  // Animated dictation-style waveform — quiet, monoline, single accent
  const bars = 28;
  return (
    <div style={{
      height: 220, display:'flex', alignItems:'center', justifyContent:'center',
      gap: 6, padding: '0 24px',
    }}>
      {Array.from({length: bars}).map((_, i) => {
        const phase = (i / bars) * Math.PI * 2;
        const h = 14 + Math.abs(Math.sin(phase * 1.6 + 0.4)) * 80 + Math.abs(Math.cos(phase * 0.8)) * 40;
        return (
          <span key={i} style={{
            width: 4, height: `${h}px`, borderRadius: 4,
            background: i === Math.floor(bars/2) ? 'var(--apple-bright-blue)' : 'rgba(255,255,255,0.62)',
            animation: `aseptic-pulse 2.4s ease-in-out ${i*60}ms infinite`,
          }}/>
        );
      })}
      <style>{`
        @keyframes aseptic-pulse {
          0%, 100% { transform: scaleY(0.6); opacity: 0.7; }
          50%      { transform: scaleY(1.0); opacity: 1.0; }
        }
      `}</style>
    </div>
  );
}

function GlobalHospitalVisual({ lang }) {
  // Floating multilingual greetings
  const phrases = [
    { t: '안녕하세요',  x: 50,  y: 18,  s: 28, dim: false },
    { t: 'Hello',     x: 70,  y: 44,  s: 22, dim: true  },
    { t: '你好',       x: 16,  y: 38,  s: 26, dim: true  },
    { t: 'مرحبا',     x: 28,  y: 70,  s: 22, dim: true  },
    { t: 'こんにちは',  x: 62,  y: 76,  s: 22, dim: true  },
    { t: 'Xin chào',  x: 84,  y: 64,  s: 16, dim: true  },
    { t: 'Bonjour',   x: 8,   y: 14,  s: 18, dim: true  },
    { t: 'Здравствуйте', x: 38, y: 50, s: 16, dim: true },
  ];
  return (
    <div style={{position:'relative', height: 220, overflow:'hidden'}}>
      {phrases.map((p, i) => (
        <span key={i} style={{
          position:'absolute', left:`${p.x}%`, top:`${p.y}%`,
          transform: 'translate(-50%, -50%)',
          fontFamily: 'var(--apple-font-display)',
          fontWeight: p.dim ? 400 : 600,
          fontSize: p.s,
          letterSpacing: '-0.4px',
          color: p.dim ? 'rgba(29,29,31,0.32)' : 'var(--apple-near-black)',
          whiteSpace: 'nowrap',
        }}>{p.t}</span>
      ))}
    </div>
  );
}

function ConnectVisual() {
  // Minimal node/edge network — "relationship-first"
  const nodes = [
    { x: 50, y: 50, r: 16, hi: true  },
    { x: 22, y: 30, r: 7 },
    { x: 78, y: 28, r: 9 },
    { x: 18, y: 72, r: 6 },
    { x: 82, y: 74, r: 8 },
    { x: 50, y: 14, r: 5 },
    { x: 50, y: 86, r: 6 },
    { x: 36, y: 62, r: 4 },
    { x: 64, y: 40, r: 5 },
  ];
  return (
    <svg viewBox="0 0 200 100" preserveAspectRatio="xMidYMid meet"
         style={{width:'100%', height: 220, display:'block'}}>
      {nodes.slice(1).map((n, i) => (
        <line key={`l${i}`}
          x1="100" y1="50"
          x2={n.x * 2} y2={n.y}
          stroke="rgba(255,255,255,0.20)" strokeWidth="0.6" />
      ))}
      {nodes.map((n, i) => (
        <circle key={`c${i}`}
          cx={n.x * 2} cy={n.y} r={n.r * 0.5}
          fill={n.hi ? 'var(--apple-bright-blue)' : 'rgba(255,255,255,0.85)'} />
      ))}
    </svg>
  );
}

function VovocoVisual() {
  // The actual Vovoco app icon, presented as a sculpture on a soft field
  return (
    <div style={{height: 220, display:'flex', alignItems:'center', justifyContent:'center'}}>
      <img src="assets/vovoco-icon.png" alt="Vovoco app icon"
           style={{width: 168, height: 168, borderRadius: 38,
                   boxShadow: 'rgba(0,0,0,0.10) 0px 14px 50px 0px',
                   display:'block'}}/>
    </div>
  );
}

// ------- Tile shells -------

function FlagshipTile({ lang, p }) {
  return (
    <article style={{
      background: 'var(--apple-black)', color: 'var(--apple-white)',
      borderRadius: 'var(--apple-radius-large)',
      padding: '64px 22px 56px', textAlign: 'center',
      overflow:'hidden', position:'relative',
    }}>
      <div className="apple-t-subheading"
           style={{fontSize: 12, letterSpacing: '1.2px', fontWeight: 600,
                   color: 'var(--apple-bright-blue)', textTransform:'uppercase',
                   marginBottom: 12}}>
        {pick(p.tag, lang)}
      </div>
      <h3 style={{
        fontFamily:'var(--apple-font-display)',
        fontSize: 'clamp(36px, 5vw, 56px)', fontWeight: 600,
        lineHeight: 1.07, letterSpacing:'-0.28px',
        margin: '0 0 8px',
      }}>
        {pick(p.name, lang)}
      </h3>
      <p className="apple-t-subheading"
         style={{margin: '0 0 6px', fontSize: 'clamp(20px, 2.3vw, 26px)',
                 color: 'var(--apple-text-on-dark)'}}>
        {pick(p.tagline, lang)}
      </p>
      <p className="apple-t-body"
         style={{margin: '0 auto 28px', maxWidth: 620,
                 color: 'rgba(255,255,255,0.7)', textWrap:'pretty'}}>
        {pick(p.desc, lang)}
      </p>
      <div style={{display:'flex', justifyContent:'center', gap: 14, flexWrap:'wrap', marginBottom: 12}}>
        <a href={p.url} target="_blank" rel="noopener" className="apple-pill-link apple-pill-link--dark">
          {pick(p.cta, lang)} ›
        </a>
      </div>
      <DrAliceVisual />
    </article>
  );
}

function ProductTile({ lang, p, theme = 'light', visual }) {
  const dark = theme === 'dark';
  return (
    <article style={{
      background: dark ? 'var(--apple-surface-dark-1)' : 'var(--apple-white)',
      color: dark ? 'var(--apple-white)' : 'var(--apple-near-black)',
      borderRadius: 'var(--apple-radius-large)',
      padding: '40px 28px 8px',
      overflow:'hidden',
      display:'flex', flexDirection:'column',
      minHeight: 460,
    }}>
      <div style={{
        fontSize: 11, fontWeight: 600, letterSpacing: '1.2px', textTransform: 'uppercase',
        color: dark ? 'var(--apple-bright-blue)' : 'var(--apple-link-blue)',
        marginBottom: 10,
      }}>
        {pick(p.tag, lang)}
      </div>
      <h3 style={{
        fontFamily: 'var(--apple-font-display)',
        fontSize: 28, fontWeight: 600, lineHeight: 1.14,
        letterSpacing: '-0.2px',
        margin: '0 0 6px',
      }}>{pick(p.name, lang)}</h3>
      <p className="apple-t-subheading" style={{margin: '0 0 10px', fontSize: 19,
            color: dark ? 'var(--apple-text-on-dark)' : 'var(--apple-near-black)'}}>
        {pick(p.tagline, lang)}
      </p>
      <p className="apple-t-body" style={{margin: 0,
            color: dark ? 'rgba(255,255,255,0.65)' : 'var(--apple-text-secondary-light)',
            textWrap:'pretty'}}>
        {pick(p.desc, lang)}
      </p>
      <div style={{flex: 1, minHeight: 24}}/>
      <div style={{margin: '0 -28px'}}>
        {visual}
      </div>
    </article>
  );
}

// ------- Section -------

function Products({ lang, showVovoco }) {
  return (
    <section id="solutions" className="section apple-section-light">
      <div className="section-inner section-inner--wide">
        <div style={{textAlign:'center', marginBottom: 44}}>
          <div style={{
            fontSize: 13, fontWeight: 600, letterSpacing: '0.6px', textTransform: 'uppercase',
            color: 'var(--apple-text-tertiary-light)', marginBottom: 14,
          }}>{pick(COPY.productsSection.eyebrow, lang)}</div>
          <h2 className="apple-t-section"
              style={{margin: 0, fontSize: 'clamp(32px, 4.4vw, 48px)', textWrap:'balance'}}>
            {pick(COPY.productsSection.title, lang)}
          </h2>
        </div>

        <FlagshipTile lang={lang} p={COPY.products.dralice} />

        <div style={{
          marginTop: 20,
          display: 'grid',
          gridTemplateColumns: `repeat(${showVovoco ? 3 : 2}, minmax(0,1fr))`,
          gap: 20,
        }} className="aseptic-products-grid">
          <ProductTile lang={lang} p={COPY.products.globalHospital} theme="light" visual={<GlobalHospitalVisual lang={lang} />} />
          <ProductTile lang={lang} p={COPY.products.connect}        theme="dark"  visual={<ConnectVisual />} />
          {showVovoco && <ProductTile lang={lang} p={COPY.products.vovoco} theme="light" visual={<VovocoVisual />} />}
        </div>

        <style>{`
          @media (max-width: 900px) {
            .aseptic-products-grid { grid-template-columns: 1fr !important; }
          }
        `}</style>
      </div>
    </section>
  );
}

Object.assign(window, { Products });
