/* global React, RC, FONTS, rgba, ELEV, PhoneFrame, SCREEN_W, fmtSec */
// ─────────────────────────────────────────────────────────────
// RopeCounter V3 — 「请选择拍摄模式」 step between 跳绳 start and recording.
// Synchronized with iOS RCCameraPickScreenV3_claudecode_20260907 // 2026.11.04 Naron
// ─────────────────────────────────────────────────────────────
const { useState: useStateCP } = React;

const CAMS = {
  back:  { label: '家长手持', sub: '（后置摄像头）' },
  front: { label: '靠墙自拍', sub: '（前置摄像头）' },
};
function loadCam() { try { const v = localStorage.getItem('rcv3cam'); return v === 'front' || v === 'back' ? v : 'front'; } catch (e) { return 'front'; } }
function saveCam(v) { try { localStorage.setItem('rcv3cam', v); } catch (e) {} }

function CamGlyph({ kind, size = 64, color }) {
  const s = size, sw = 2.2;
  return (
    <svg width={s} height={s * 1.3} viewBox="0 0 40 52" fill="none" stroke={color} strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
      <rect x="4" y="2" width="32" height="48" rx="6"/>
      {kind === 'front' ? <>
        <circle cx="20" cy="7.5" r="1.6" fill={color} stroke="none"/>
        <circle cx="20" cy="24" r="5"/><path d="M11 42c1.5-6 5-9 9-9s7.5 3 9 9"/>
      </> : <>
        <rect x="9" y="7" width="13" height="13" rx="4"/><circle cx="15.5" cy="13.5" r="3"/><circle cx="26" cy="10" r="1.4" fill={color} stroke="none"/>
      </>}
    </svg>
  );
}

function CamCard({ c, dark, font, kind, big, onPick }) {
  const cam = CAMS[kind];
  return (
    <button onClick={() => onPick(kind)} aria-label={`${cam.label}${cam.sub}`} style={{
      flex: big ? 1.5 : 1, minWidth: 0, height: big ? 168 : 124, cursor: 'pointer', textAlign: 'center',
      borderRadius: 22, padding: 14, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: big ? 6 : 4,
      background: c.surface, color: c.textPri,
      border: `1px solid ${c.border}`, boxShadow: dark ? 'none' : big ? ELEV[2] : ELEV[1], fontFamily: font.sans,
    }}>
      <CamGlyph kind={kind} size={big ? 30 : 22} color={c.accent}/>
      <div style={{ fontSize: big ? 22 : 16, fontWeight: 700, letterSpacing: -0.3, lineHeight: 1.15, marginTop: 4 }}>{cam.label}</div>
      <div style={{ fontSize: big ? 14 : 12.5, fontWeight: 500, lineHeight: 1.3, color: c.textSec }}>{cam.sub}</div>
    </button>
  );
}

function CameraPickScreen({ platform = 'ios', dark = false, run = { mode: 'goalTime', durationMin: 1 }, last, onBack, onPick }) {
  const c = dark ? RC.dark : RC.light;
  const font = platform === 'android' ? FONTS.android : FONTS.ios;
  const [remembered] = useStateCP(() => last || loadCam());
  const pick = (k) => { saveCam(k); onPick && onPick(k); };
  const summary = run.mode === 'goalTime' ? `定时 ${fmtSec(Math.round((run.durationMin || 1) * 60))}` : run.mode === 'goalCount' ? `定数 ${run.targetCount || 200} 个` : '自由模式';
  const top = platform === 'android' ? 64 : 70;

  return (
    <PhoneFrame platform={platform} dark={dark} statusDark={!dark} hideTab>
      <div style={{ position: 'absolute', inset: 0, background: c.bg }}/>
      <button onClick={onBack} aria-label="返回" style={{ position: 'absolute', top: top - 6, left: 12, width: 40, height: 40, borderRadius: 999, border: 'none', cursor: 'pointer', background: 'transparent', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
        <svg width="20" height="20" viewBox="0 0 20 20" fill="none" stroke={c.textPri} strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"><path d="M12.5 4 6.5 10l6 6"/></svg>
      </button>
      <div style={{ position: 'absolute', top: top + 52, left: 20, right: 20, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 8 }}>
        <div style={{ fontFamily: font.sans, fontSize: 26, fontWeight: 700, letterSpacing: -0.5, color: c.textPri, lineHeight: 1.15 }}>请选择拍摄模式</div>
        <div style={{ fontFamily: font.sans, fontSize: 14, color: c.textSec, fontWeight: 500 }}>{summary}</div>
      </div>
      <div style={{ position: 'absolute', left: 16, right: 16, top: '50%', transform: 'translateY(-50%)', display: 'flex', alignItems: 'center', gap: 12 }}>
        <CamCard c={c} dark={dark} font={font} kind="back" big={remembered === 'back'} onPick={pick}/>
        <CamCard c={c} dark={dark} font={font} kind="front" big={remembered === 'front'} onPick={pick}/>
      </div>
    </PhoneFrame>
  );
}

Object.assign(window, { CameraPickScreen, loadCam, saveCam });
