// Getfiit landing: the painted phone and the live WhatsApp screen inside it.
//
// phone.webp is a Nano Banana render with its display keyed out to black.
// The screen box (gf-landing.css, .lp-device-screen) was measured off that
// render, and the WhatsApp UI is laid out in iPhone screen points (393 wide)
// and scaled to whatever size the phone is drawn at, so the text stays sharp.

const SCREEN_W = 393;
// The painted display is 798x1738, a touch narrower than an iPhone's 393x852.
const SCREEN_H = Math.round(SCREEN_W / (798 / 1738));

function Device({ label, children, className = '', eager = false }) {
  const screenRef = React.useRef(null);
  const [scale, setScale] = React.useState(0);

  React.useLayoutEffect(() => {
    const el = screenRef.current;
    if (!el) return undefined;
    const measure = () => setScale(el.clientWidth / SCREEN_W);
    measure();
    const ro = new ResizeObserver(measure);
    ro.observe(el);
    return () => ro.disconnect();
  }, []);

  return (
    <div className={`lp-device ${className}`} role="img" aria-label={label}>
      <img
        className="lp-device-frame"
        src="assets/landing/phone.webp"
        alt=""
        width="860"
        height="1754"
        loading={eager ? 'eager' : 'lazy'}
        decoding="async"
      />
      <div className="lp-device-screen" ref={screenRef} aria-hidden="true">
        <div
          className="lp-device-ui"
          style={{
            width: SCREEN_W,
            height: SCREEN_H,
            transform: `scale(${scale})`,
            visibility: scale ? 'visible' : 'hidden',
          }}
        >
          {children}
        </div>
      </div>
    </div>
  );
}

// ─────────── WhatsApp chrome ───────────

function WaScreen({ children, presence = 'online' }) {
  return (
    <div className="wa">
      <div className="wa-status">
        <span>9:41</span>
        <span className="wa-island" />
        <span className="wa-status-icons">
          <SignalIcon /><WifiIcon /><BatteryIcon />
        </span>
      </div>
      <div className="wa-head">
        <span className="wa-back"><ChevronLeft /></span>
        <span className="wa-avatar"><img src="logo-mark-white.svg" alt="" /></span>
        <div className="wa-who">
          <div className="wa-name">Getfiit</div>
          <div className="wa-presence">{presence}</div>
        </div>
        <div className="wa-head-icons"><VideoIcon /><PhoneIcon /></div>
      </div>
      <div className="wa-body">{children}</div>
      <div className="wa-input">
        <PlusIcon />
        <span className="wa-input-field" />
        <CameraIcon />
        <MicIcon />
      </div>
    </div>
  );
}

function WaDate({ children = 'Today' }) {
  return <div className="wa-date">{children}</div>;
}

function WaMeta({ time, sent }) {
  return (
    <span className="wa-meta">
      {time}
      {sent && <ReadTicks />}
    </span>
  );
}

function WaOut({ children, time, style }) {
  return (
    <div className="wa-msg wa-msg--out" style={style}>
      {children}
      <WaMeta time={time} sent />
    </div>
  );
}

function WaIn({ children, time, style }) {
  return (
    <div className="wa-msg wa-msg--in" style={style}>
      {children}
      <WaMeta time={time} />
    </div>
  );
}

function WaPhoto({ src, caption, time, small, style }) {
  return (
    <div className={`wa-msg wa-msg--out wa-msg--photo${small ? ' is-small' : ''}`} style={style}>
      <img src={src} alt="" width="214" height="214" loading="lazy" decoding="async" />
      <div className="wa-caption">
        {caption}
        <WaMeta time={time} sent />
      </div>
    </div>
  );
}

// A voice note as WhatsApp draws a sent one: play, waveform, then the
// sender's face with the blue mic once it has been played.
const WAVE = [6, 10, 16, 22, 14, 9, 18, 26, 20, 12, 8, 14, 24, 28, 18, 10, 7, 12, 20, 15, 9, 6, 11, 17, 13, 8, 5, 9];

function WaVoice({ dur, time, style }) {
  return (
    <div className="wa-msg wa-msg--out" style={style}>
      <div className="wa-voice">
        <span className="wa-voice-play"><PlayIcon /></span>
        <div style={{ flex: 1 }}>
          <div className="wa-wave">
            {WAVE.map((h, i) => <i key={i} className={i < 11 ? 'on' : ''} style={{ height: h }} />)}
          </div>
          <div className="wa-voice-dur">{dur}</div>
        </div>
        <span className="wa-voice-face">
          <PersonIcon />
          <span className="wa-voice-mic"><MicSmall /></span>
        </span>
      </div>
      <WaMeta time={time} sent />
    </div>
  );
}

// One card of a WhatsApp carousel: media, the card's text, its quick replies.
function WaCard({ img, text, buttons = [], style }) {
  return (
    <div className="wa-card" style={style}>
      <img src={img} alt="" width="1125" height="600" loading="lazy" decoding="async" />
      <div className="wa-card-text">{text}</div>
      {buttons.map((b) => <div key={b} className="wa-card-btn">{b}</div>)}
    </div>
  );
}

function WaTyping() {
  return (
    <div className="wa-msg wa-msg--in wa-typing">
      <i /><i /><i />
    </div>
  );
}

// ─────────── Icons (screen points) ───────────

function ReadTicks() {
  return (
    <svg width="17" height="11" viewBox="0 0 16 11" fill="#53bdeb" aria-hidden="true">
      <path d="M11.07.65 4.6 7.11 2.05 4.56 1 5.61l3.6 3.6L12.13 1.7z" />
      <path d="m15 .65-6.46 6.46-.94-.95-1.05 1.05 2 2L16.05 1.7z" />
    </svg>
  );
}
function SignalIcon() {
  return (
    <svg width="18" height="12" viewBox="0 0 18 12" fill="#111b21" aria-hidden="true">
      <rect x="0" y="8" width="3" height="4" rx="1" /><rect x="5" y="5.5" width="3" height="6.5" rx="1" />
      <rect x="10" y="3" width="3" height="9" rx="1" /><rect x="15" y="0" width="3" height="12" rx="1" />
    </svg>
  );
}
function WifiIcon() {
  return (
    <svg width="17" height="12" viewBox="0 0 17 12" fill="#111b21" aria-hidden="true">
      <path d="M8.5 2.3c2.4 0 4.6.9 6.3 2.5l1.2-1.2A10.6 10.6 0 0 0 8.5.6 10.6 10.6 0 0 0 1 3.6l1.2 1.2a9 9 0 0 1 6.3-2.5Zm0 3.5c1.4 0 2.8.5 3.8 1.5l1.2-1.2a7.2 7.2 0 0 0-10 0l1.2 1.2c1-1 2.4-1.5 3.8-1.5Zm0 3.4c.5 0 1 .2 1.3.5L8.5 11 7.2 9.7c.3-.3.8-.5 1.3-.5Z" />
    </svg>
  );
}
function BatteryIcon() {
  return (
    <svg width="27" height="13" viewBox="0 0 27 13" fill="none" aria-hidden="true">
      <rect x="0.5" y="0.5" width="23" height="12" rx="3.5" stroke="#111b21" opacity="0.4" />
      <rect x="2" y="2" width="20" height="9" rx="2" fill="#111b21" />
      <path d="M25 4.5v4c.8-.3 1.3-1.1 1.3-2s-.5-1.7-1.3-2Z" fill="#111b21" opacity="0.4" />
    </svg>
  );
}
function ChevronLeft() {
  return (
    <svg width="13" height="21" viewBox="0 0 13 21" fill="none" stroke="#007aff" strokeWidth="2.6" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
      <path d="M11 2 2.5 10.5 11 19" />
    </svg>
  );
}
function VideoIcon() {
  return (
    <svg width="26" height="18" viewBox="0 0 26 18" fill="none" stroke="#007aff" strokeWidth="1.8" strokeLinejoin="round" aria-hidden="true">
      <rect x="1" y="2" width="16" height="14" rx="3.5" /><path d="m17 7.2 7-4.2v12l-7-4.2" />
    </svg>
  );
}
function PhoneIcon() {
  return (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#007aff" strokeWidth="1.8" strokeLinejoin="round" aria-hidden="true">
      <path d="M5 2.8h3.2l1.6 4.4-2.2 1.6a12 12 0 0 0 7.6 7.6l1.6-2.2 4.4 1.6V19a2.2 2.2 0 0 1-2.2 2.2C10.1 21.2 2.8 13.9 2.8 5A2.2 2.2 0 0 1 5 2.8Z" />
    </svg>
  );
}
function PlusIcon() {
  return (
    <svg width="22" height="34" viewBox="0 0 22 34" fill="none" stroke="#007aff" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
      <path d="M11 9v16M3 17h16" />
    </svg>
  );
}
function CameraIcon() {
  return (
    <svg width="26" height="34" viewBox="0 0 26 34" fill="none" stroke="#007aff" strokeWidth="1.8" strokeLinejoin="round" aria-hidden="true">
      <path d="M3 13.5A2.5 2.5 0 0 1 5.5 11h3l2-2.5h5l2 2.5h3a2.5 2.5 0 0 1 2.5 2.5V24a2.5 2.5 0 0 1-2.5 2.5h-15A2.5 2.5 0 0 1 3 24Z" /><circle cx="13" cy="18.5" r="4.2" />
    </svg>
  );
}
function MicIcon() {
  return (
    <svg width="20" height="34" viewBox="0 0 20 34" fill="none" stroke="#007aff" strokeWidth="1.8" strokeLinecap="round" aria-hidden="true">
      <rect x="6" y="7" width="8" height="14" rx="4" /><path d="M2.5 17a7.5 7.5 0 0 0 15 0M10 24.5V28" />
    </svg>
  );
}
function PlayIcon() {
  return (
    <svg width="20" height="22" viewBox="0 0 20 22" fill="currentColor" aria-hidden="true">
      <path d="M3 2.2v17.6a1 1 0 0 0 1.5.9l14.2-8.8a1 1 0 0 0 0-1.8L4.5 1.3A1 1 0 0 0 3 2.2Z" />
    </svg>
  );
}
function PersonIcon() {
  return (
    <svg width="24" height="24" viewBox="0 0 24 24" fill="#fff" aria-hidden="true">
      <circle cx="12" cy="9" r="4.5" /><path d="M3.5 21a8.5 8.5 0 0 1 17 0Z" />
    </svg>
  );
}
function MicSmall() {
  return (
    <svg width="14" height="16" viewBox="0 0 14 16" fill="currentColor" aria-hidden="true">
      <rect x="4" y="0.5" width="6" height="10" rx="3" /><path d="M1.5 8a5.5 5.5 0 0 0 11 0h-1.4a4.1 4.1 0 0 1-8.2 0Zm4.8 6.4V16h1.4v-1.6Z" />
    </svg>
  );
}

window.GfDevice = { Device, WaScreen, WaDate, WaOut, WaIn, WaPhoto, WaVoice, WaCard, WaTyping };
