/* tweaks.jsx — Tweaks panel + bridge.
   Renders the floating panel and writes every value onto <html> as
   data-attributes / CSS vars, then fires 'rosca:apply' so app.js can
   restart the hero typing once attributes are live. */

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "tone": "balance",
  "hero": "editor",
  "accent": "#00ff88",
  "codefont": "jetbrains",
  "typing": true,
  "motion": true
}/*EDITMODE-END*/;

function hexToRgb(hex) {
  let h = String(hex).replace('#', '');
  if (h.length === 3) h = h.replace(/./g, (c) => c + c);
  const n = parseInt(h.slice(0, 6), 16);
  return { r: (n >> 16) & 255, g: (n >> 8) & 255, b: n & 255 };
}
function relLum({ r, g, b }) {
  const a = [r, g, b].map((v) => {
    v /= 255;
    return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
  });
  return 0.2126 * a[0] + 0.7152 * a[1] + 0.0722 * a[2];
}

function applyAccent(hex) {
  const root = document.documentElement;
  const { r, g, b } = hexToRgb(hex);
  const attack = root.getAttribute('data-tone') === 'attack';
  root.style.setProperty('--accent', hex);
  root.style.setProperty('--accent-soft', `rgba(${r},${g},${b},${attack ? .17 : .13})`);
  root.style.setProperty('--accent-line', `rgba(${r},${g},${b},.32)`);
  root.style.setProperty('--accent-ink', relLum({ r, g, b }) > 0.4 ? '#04130c' : '#ffffff');
  if (attack) root.style.setProperty('--glow', `0 0 0 1px rgba(${r},${g},${b},.32), 0 0 28px rgba(${r},${g},${b},.30)`);
  else root.style.removeProperty('--glow');
}

function App() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);
  const firstRef = React.useRef(true);
  const prevRef = React.useRef({ ...TWEAK_DEFAULTS });

  React.useEffect(() => {
    const root = document.documentElement;
    root.setAttribute('data-tone', t.tone);
    root.setAttribute('data-hero', t.hero);
    root.setAttribute('data-codefont', t.codefont);
    root.setAttribute('data-typing', t.typing ? 'on' : 'off');
    root.setAttribute('data-motion', t.motion ? 'on' : 'off');
    applyAccent(t.accent);   // depends on tone attr being set first

    const prev = prevRef.current;
    const changed = {};
    Object.keys(t).forEach((k) => { if (t[k] !== prev[k]) changed[k] = t[k]; });
    prevRef.current = { ...t };

    if (firstRef.current) { firstRef.current = false; return; }
    window.dispatchEvent(new CustomEvent('rosca:apply', { detail: changed }));
  }, [t]);

  return (
    <TweaksPanel title="Tweaks">
      <TweakSection label="トーン" />
      <TweakRadio label="全体の温度感" value={t.tone}
        options={[{ value: 'attack', label: '攻め' }, { value: 'balance', label: 'バランス' }, { value: 'calm', label: '落ち着き' }]}
        onChange={(v) => setTweak('tone', v)} />
      <TweakColor label="アクセント" value={t.accent}
        options={['#00ff88', '#22d3ee', '#f5b740', '#818cf8']}
        onChange={(v) => setTweak('accent', v)} />

      <TweakSection label="ヒーロー" />
      <TweakRadio label="コード表示" value={t.hero}
        options={[{ value: 'editor', label: 'エディタ' }, { value: 'terminal', label: 'ターミナル' }, { value: 'minimal', label: 'ミニマル' }]}
        onChange={(v) => setTweak('hero', v)} />
      <TweakRadio label="コードフォント" value={t.codefont}
        options={[{ value: 'jetbrains', label: 'JetBrains' }, { value: 'fira', label: 'Fira Code' }]}
        onChange={(v) => setTweak('codefont', v)} />
      <TweakToggle label="タイピング演出" value={t.typing}
        onChange={(v) => setTweak('typing', v)} />

      <TweakSection label="モーション" />
      <TweakToggle label="フェードイン演出" value={t.motion}
        onChange={(v) => setTweak('motion', v)} />
    </TweaksPanel>
  );
}

ReactDOM.createRoot(document.getElementById('tweaks-root')).render(<App />);
