/* global React, RC, FONTS, rgba, Skipping, TabRope, TabLoops, TabFace */
// ─────────────────────────────────────────────────────────────
// RopeCounter V3 — device frames + chrome (iOS + Android)
// Synchronized with iOS RCTabBarV3_claudecode_20260905 (393×852) // 2026.11.04 Naron
// ─────────────────────────────────────────────────────────────
const SCREEN_W = 393, SCREEN_H = 852;

const TABS = [
  { key: 'measure',  label: '跳绳' },
  { key: 'trends',   label: '练' },
  { key: 'settings', label: '小天天' },
];

// ─── Status bar glyphs (shared SVGs, color-driven) ───
function StatusGlyphs({ color }) {
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
      <svg width="18" height="11" viewBox="0 0 18 11" fill={color}><rect x="0" y="7" width="3" height="4" rx="0.6"/><rect x="4.5" y="5" width="3" height="6" rx="0.6"/><rect x="9" y="2.5" width="3" height="8.5" rx="0.6"/><rect x="13.5" y="0" width="3" height="11" rx="0.6"/></svg>
      <svg width="16" height="11" viewBox="0 0 16 11" fill={color}><path d="M8 2.5c2.2 0 4.2.9 5.6 2.3l1.2-1.2A9.4 9.4 0 0 0 8 .7 9.4 9.4 0 0 0 1.2 3.6l1.2 1.2A7.9 7.9 0 0 1 8 2.5z"/><path d="M8 6c1.4 0 2.6.5 3.5 1.4l1.1-1.1A6 6 0 0 0 8 4.3a6 6 0 0 0-4.6 2l1.1 1.1A4.5 4.5 0 0 1 8 6z"/><circle cx="8" cy="9.5" r="1.4"/></svg>
      <svg width="25" height="12" viewBox="0 0 25 12"><rect x="0.5" y="0.5" width="22" height="11" rx="3" stroke={color} strokeOpacity="0.4" fill="none"/><rect x="2" y="2" width="18.5" height="8" rx="1.5" fill={color}/><path d="M23.5 4v4c.7-.3 1.2-1.1 1.2-2s-.5-1.7-1.2-2z" fill={color} fillOpacity="0.45"/></svg>
    </div>
  );
}

function StatusBar({ platform, statusDark, font }) {
  const color = statusDark ? '#0F172A' : '#FFFFFF';
  return (
    <div style={{
      position: 'absolute', top: 0, left: 0, right: 0, height: 54, zIndex: 60,
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      padding: platform === 'android' ? '15px 18px 0 22px' : '15px 30px 0',
      color, fontFamily: font, fontWeight: 600, fontSize: 16, letterSpacing: -0.2,
    }}>
      <span style={{ fontVariantNumeric: 'tabular-nums' }}>9:41</span>
      <div style={{ width: 90 }}/>
      <StatusGlyphs color={color}/>
    </div>
  );
}

// ─── Tab bar — flat full-width bar (ref: 师夷): hairline top border,
// outline icon + label per tab, active = accent, no pill / no fill.
function TabBar({ platform, dark, active, onTab }) {
  const c = dark ? RC.dark : RC.light;
  const font = platform === 'android' ? FONTS.android.sans : FONTS.ios.sans;
  const iconFor = (key, col, on) => {
    if (key === 'measure') return <TabRope size={24} color={col} on={on}/>;
    if (key === 'trends')  return <TabLoops size={24} color={col} on={on}/>;
    return <TabFace size={24} color={col} on={on}/>;
  };
  const h = platform === 'android' ? 78 : 84;
  return (
    <div style={{
      position: 'absolute', left: 0, right: 0, bottom: 0, height: h, zIndex: 55, fontFamily: font,
      background: dark ? 'rgba(20,28,42,0.86)' : 'rgba(255,255,255,0.92)',
      backdropFilter: 'blur(24px) saturate(180%)', WebkitBackdropFilter: 'blur(24px) saturate(180%)',
      borderTop: `0.5px solid ${dark ? 'rgba(255,255,255,0.10)' : 'rgba(15,23,42,0.10)'}`,
      display: 'flex', alignItems: 'flex-start', justifyContent: 'space-around', padding: '10px 24px 0',
    }}>
      {TABS.map(t => {
        const on = t.key === active;
        const col = on ? c.accent : c.textSec;
        return (
          <button key={t.key} onClick={() => onTab && onTab(t.key)} style={{
            flex: 1, background: 'none', border: 'none', cursor: 'pointer', padding: 0,
            display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 5, color: col,
          }}>
            {iconFor(t.key, col, on)}
            <span style={{ fontSize: 12, fontWeight: on ? 700 : 500, letterSpacing: 0, color: col }}>{t.label}</span>
          </button>
        );
      })}
    </div>
  );
}

// ─── PhoneFrame — outer device shell ───
function PhoneFrame({
  platform = 'ios', dark = false, statusDark = false,
  tab = null, onTab, hideTab = false, scale = 1, children, style = {},
}) {
  const c = dark ? RC.dark : RC.light;
  const font = platform === 'android' ? FONTS.android.sans : FONTS.ios.sans;
  const radius = platform === 'android' ? 36 : 47;

  return (
    <div style={{
      width: SCREEN_W, height: SCREEN_H, position: 'relative',
      borderRadius: radius, overflow: 'hidden', background: c.bg,
      fontFamily: font, WebkitFontSmoothing: 'antialiased',
      transform: scale !== 1 ? `scale(${scale})` : undefined,
      transformOrigin: 'top left', ...style,
    }}>
      {/* screen content */}
      <div style={{ position: 'absolute', inset: 0 }}>{children}</div>

      {/* tab bar (optional) */}
      {tab && !hideTab && <TabBar platform={platform} dark={dark} active={tab} onTab={onTab}/>}

      {/* status bar */}
      <StatusBar platform={platform} statusDark={statusDark} font={font}/>

      {/* camera cutout */}
      {platform === 'android'
        ? <div style={{ position: 'absolute', top: 13, left: '50%', transform: 'translateX(-50%)', width: 11, height: 11, borderRadius: 999, background: '#000', zIndex: 62 }}/>
        : <div style={{ position: 'absolute', top: 11, left: '50%', transform: 'translateX(-50%)', width: 124, height: 36, borderRadius: 20, background: '#000', zIndex: 62 }}/>
      }

      {/* system nav: iOS home bar OR Android gesture pill */}
      <div style={{
        position: 'absolute', bottom: platform === 'android' ? 7 : 8, left: '50%',
        transform: 'translateX(-50%)', zIndex: 65,
        width: platform === 'android' ? 108 : 134, height: platform === 'android' ? 4 : 5, borderRadius: 3,
        background: statusDark ? 'rgba(15,23,42,0.32)' : 'rgba(255,255,255,0.85)',
      }}/>
    </div>
  );
}

Object.assign(window, { PhoneFrame, TabBar, StatusBar, SCREEN_W, SCREEN_H, TABS });
