/* ============================================================
   VLF 2026 · ZALO OA — KHUNG iPhone #2 (màn hội thoại OA)
   Chrome chat = Zalo (xanh, platform). Nhận tin từ App state.
   props: thread (mảng message), lang
   ============================================================ */

function OAStatusBar({ lang }) {
  return (
    <div className="oa-status">
      <div className="l">{lang === 'en' ? '9:41' : '06:18'}</div>
      <div className="r">
        <svg width="18" height="12" viewBox="0 0 18 12" fill="currentColor"><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>
        <svg width="17" height="12" viewBox="0 0 17 12" fill="currentColor"><path d="M8.5 2.2c2.3 0 4.4.9 6 2.4l-1.5 1.6A6.4 6.4 0 0 0 8.5 4.4c-1.7 0-3.3.6-4.5 1.8L2.5 4.6A8.6 8.6 0 0 1 8.5 2.2zM8.5 6c1.2 0 2.3.5 3.1 1.3l-1.5 1.5c-.4-.4-1-.7-1.6-.7s-1.2.3-1.6.7L5.4 7.3C6.2 6.5 7.3 6 8.5 6zM8.5 9.4l1.5 1.5-1.5 1.1L7 10.9 8.5 9.4z" /></svg>
        <span className="oa-battery"><i /></span>
      </div>
    </div>
  );
}

function OAHeader({ lang }) {
  const T = (vi, en) => (lang === 'en' ? en : vi);
  return (
    <div className="oa-head">
      <div className="oa-back"><svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.3" strokeLinecap="round" strokeLinejoin="round"><path d="M15 18l-6-6 6-6" /></svg></div>
      <div className="oa-ava">
        <VLFLogo />
        <span className="tick"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3.4" strokeLinecap="round" strokeLinejoin="round"><path d="M5 12l5 5L20 6" /></svg></span>
      </div>
      <div className="oa-id">
        <div className="nm">VLF 2026
          <svg className="vrf" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2l2.6 1.5 3 .2 1.2 2.7 2.2 2-.6 3 .6 3-2.2 2-1.2 2.7-3 .2L12 22l-2.6-1.5-3-.2-1.2-2.7-2.2-2 .6-3-.6-3 2.2-2L6.4 3.7l3-.2L12 2z" /><path d="M8.5 12.2l2.3 2.3 4.5-4.7" fill="none" stroke="#0068FF" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" /></svg>
        </div>
        <div className="sub">{T('Official Account', 'Official Account')} <b>· {T('Đang hoạt động', 'Active now')}</b></div>
      </div>
      <div className="oa-actions">
        <div className="ic"><svg width="21" height="21" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round"><path d="M22 16.9v3a2 2 0 0 1-2.2 2 19.8 19.8 0 0 1-8.6-3 19.5 19.5 0 0 1-6-6 19.8 19.8 0 0 1-3-8.6A2 2 0 0 1 4.1 2h3a2 2 0 0 1 2 1.7c.1 1 .4 1.9.7 2.8a2 2 0 0 1-.5 2.1L8.1 9.9a16 16 0 0 0 6 6l1.3-1.3a2 2 0 0 1 2.1-.4c.9.3 1.8.6 2.8.7a2 2 0 0 1 1.7 2Z" /></svg></div>
        <div className="ic"><svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round"><rect x="2" y="6" width="13" height="12" rx="2.4" /><path d="M22 8.5l-7 3.5 7 3.5z" /></svg></div>
        <div className="ic"><svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><path d="M4 7h16M4 12h16M4 17h16" /></svg></div>
      </div>
    </div>
  );
}

function OAMsgAva() { return <div className="oa-msgava"><VLFLogo /></div>; }

function OABubble({ m, lang }) {
  const tx = (o) => (o ? (lang === 'en' ? o.en : o.vi) : '');
  if (m.view === 'day') return <div className="oa-day"><span>{tx(m.text)}</span></div>;

  if (m.view === 'chips') {
    return (
      <div className="oa-row">
        <OAMsgAva />
        <div className="oa-stack"><div className="oa-chips">{m.items.map((c, i) => <span className="oa-chip" key={i}>{tx(c)}</span>)}</div></div>
      </div>
    );
  }

  const me = m.from === 'me';
  return (
    <div className={'oa-row' + (me ? ' me' : '')}>
      {!me && <OAMsgAva />}
      <div className="oa-stack">
        {m.view === 'zns'
          ? <ZNSRender zt={m.zt} d={m.d} lang={lang} />
          : <div className="oa-bubble">{tx(m.text)}</div>}
        {m.time && (
          <div className="oa-time">{tx(m.time)}
            {me && m.seen && <span className="seen"><svg viewBox="0 0 22 11" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"><path d="M1 6l3.5 3.5L11 2" /><path d="M9 8l1 1L20 1" /></svg></span>}
          </div>
        )}
      </div>
    </div>
  );
}

function OADock({ lang }) {
  const T = (vi, en) => (lang === 'en' ? en : vi);
  return (
    <div className="oa-dock">
      <div className="oa-richmenu">
        <div className="item"><Icon name="ticket" size={19} sw={1.9} /><span>{T('Mua vé', 'Tickets')}</span></div>
        <div className="item"><Icon name="calendar" size={19} sw={1.9} /><span>{T('Lịch đấu', 'Schedule')}</span></div>
        <div className="item"><svg width="19" height="19" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><path d="M4 7h16M4 12h16M4 17h16" /></svg><span>{T('Xem thêm', 'More')}</span></div>
      </div>
      <div className="oa-input">
        <div className="ic"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><circle cx="12" cy="12" r="9.5" /><path d="M12 8v8M8 12h8" /></svg></div>
        <div className="box">{T('Tin nhắn…', 'Message…')}</div>
        <div className="ic"><svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="9.5" /><path d="M8.5 14.5a4 4 0 0 0 7 0" /><path d="M9 9.5h.01M15 9.5h.01" /></svg></div>
        <div className="ic mic"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round"><rect x="9" y="2.5" width="6" height="12" rx="3" /><path d="M5 11a7 7 0 0 0 14 0M12 18.5V22" /></svg></div>
      </div>
      <div className="oa-home" />
    </div>
  );
}

function OAPhone({ thread, lang }) {
  const convRef = React.useRef(null);
  React.useLayoutEffect(() => {
    const el = convRef.current;
    if (el) el.scrollTop = el.scrollHeight;
  }, [thread]);

  return (
    <div className="za-frame za-frame--oa">
      <div className="za-phone">
        <div className="za-island" />
        <div className="za-screen oa-screen" data-screen-label="Zalo OA · hội thoại chính thức">
          <OAStatusBar lang={lang} />
          <OAHeader lang={lang} />
          <div className="oa-conv" ref={convRef}>
            {thread.map((m) => <OABubble key={m.id} m={m} lang={lang} />)}
          </div>
          <OADock lang={lang} />
        </div>
      </div>
    </div>
  );
}

Object.assign(window, { OAPhone });
