/* global React, ReactDOM */
/* Tweaks: 4 colors × light/dark = 8 themes */

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "color": "strawberry",
  "mode": "light"
}/*EDITMODE-END*/;

const COLORS = [
  { id: 'strawberry', label: 'Strawberry',
    light: ['#FF6B9D', '#FFD4E2', '#FFF6F2'],
    dark:  ['#FF7FB0', '#3B2336', '#15121C'] },
  { id: 'mint',       label: 'Mint',
    light: ['#3FB97A', '#C9EED9', '#F1FBF5'],
    dark:  ['#5FD691', '#1F3A2A', '#101A14'] },
  { id: 'soda',       label: 'Soda',
    light: ['#4D7FFF', '#C6D6FF', '#EEF6FF'],
    dark:  ['#7BA1FF', '#1F2A4A', '#0E1220'] },
  { id: 'lavender',   label: 'Lavender',
    light: ['#9659E4', '#E2D2F5', '#F6F2FB'],
    dark:  ['#B985FB', '#2F1F45', '#14101E'] },
];

function TweaksApp() {
  const { TweaksPanel, useTweaks, TweakSection } = window;
  // Initialize from whatever site.js already loaded onto <html> (which is the
  // persisted state). Otherwise we'd overwrite the user's stored choice with
  // TWEAK_DEFAULTS on every page load.
  const initial = {
    color: document.documentElement.getAttribute('data-color') || TWEAK_DEFAULTS.color,
    mode:  document.documentElement.getAttribute('data-mode')  || TWEAK_DEFAULTS.mode,
  };
  const [t, setTweak] = useTweaks(initial);

  // Sync with global theme manager
  React.useEffect(() => {
    if (window.MozaikuTheme) window.MozaikuTheme.setColor(t.color);
  }, [t.color]);
  React.useEffect(() => {
    if (window.MozaikuTheme) window.MozaikuTheme.setMode(t.mode);
  }, [t.mode]);

  // Listen for header toggle changes
  React.useEffect(() => {
    function onChange() {
      const mode = window.MozaikuTheme && window.MozaikuTheme.mode;
      if (mode && mode !== t.mode) setTweak('mode', mode);
    }
    window.addEventListener('mozaiku:themechange', onChange);
    return () => window.removeEventListener('mozaiku:themechange', onChange);
  }, [t.mode, setTweak]);

  return (
    <TweaksPanel>
      <TweakSection title="ライト／ダーク">
        <div className="mode-row">
          <button
            className={`mode-btn ${t.mode === 'light' ? 'is-active' : ''}`}
            onClick={() => setTweak('mode', 'light')}
            aria-pressed={t.mode === 'light'}
          >
            <svg viewBox="0 0 24 24" width="16" height="16"><circle cx="12" cy="12" r="4" fill="currentColor"/><g stroke="currentColor" strokeWidth="2" strokeLinecap="round"><line x1="12" y1="3" x2="12" y2="5"/><line x1="12" y1="19" x2="12" y2="21"/><line x1="3" y1="12" x2="5" y2="12"/><line x1="19" y1="12" x2="21" y2="12"/><line x1="5.5" y1="5.5" x2="6.9" y2="6.9"/><line x1="17.1" y1="17.1" x2="18.5" y2="18.5"/><line x1="5.5" y1="18.5" x2="6.9" y2="17.1"/><line x1="17.1" y1="6.9" x2="18.5" y2="5.5"/></g></svg>
            ライト
          </button>
          <button
            className={`mode-btn ${t.mode === 'dark' ? 'is-active' : ''}`}
            onClick={() => setTweak('mode', 'dark')}
            aria-pressed={t.mode === 'dark'}
          >
            <svg viewBox="0 0 24 24" width="16" height="16"><path d="M21 13 A9 9 0 1 1 11 3 a7 7 0 0 0 10 10 z" fill="currentColor"/></svg>
            ダーク
          </button>
        </div>
      </TweakSection>

      <TweakSection title="配色">
        <div className="color-grid">
          {COLORS.map(c => {
            const swatch = t.mode === 'dark' ? c.dark : c.light;
            return (
              <button
                key={c.id}
                className={`color-card ${t.color === c.id ? 'is-active' : ''}`}
                onClick={() => setTweak('color', c.id)}
                aria-pressed={t.color === c.id}
              >
                <span className="color-card__swatch">
                  {swatch.map((cc, i) => <span key={i} style={{ background: cc }} />)}
                </span>
                <strong>{c.label}</strong>
              </button>
            );
          })}
        </div>
      </TweakSection>

      <style>{`
        .mode-row { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 4px; background: #f1edf2; border-radius: 12px; }
        .mode-btn {
          appearance: none;
          border: none;
          background: transparent;
          padding: 8px;
          border-radius: 10px;
          font-family: 'M PLUS Rounded 1c', system-ui, sans-serif;
          font-weight: 800;
          font-size: 13px;
          color: #4B445A;
          cursor: pointer;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          gap: 6px;
          transition: background 0.12s, color 0.12s;
        }
        .mode-btn:hover { color: #2B2030; }
        .mode-btn.is-active { background: #fff; color: #2B2030; box-shadow: 0 1px 4px rgba(0,0,0,0.06); }

        .color-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
        .color-card {
          appearance: none;
          border: 2px solid #e6e6e8;
          background: #fff;
          padding: 10px;
          border-radius: 12px;
          cursor: pointer;
          display: flex;
          flex-direction: column;
          align-items: stretch;
          gap: 8px;
          transition: all 0.12s;
        }
        .color-card:hover { border-color: #aaa; transform: translateY(-1px); }
        .color-card.is-active {
          border-color: #2B2030;
          background: #faf6f8;
          box-shadow: 0 2px 0 rgba(0,0,0,0.08);
        }
        .color-card__swatch {
          display: flex;
          height: 28px;
          border-radius: 8px;
          overflow: hidden;
          border: 1px solid rgba(0,0,0,0.08);
        }
        .color-card__swatch span { flex: 1; }
        .color-card strong {
          font-family: 'M PLUS Rounded 1c', system-ui, sans-serif;
          font-weight: 800;
          font-size: 13px;
          color: #2B2030;
          text-align: center;
        }
      `}</style>
    </TweaksPanel>
  );
}

const tweaksRoot = document.getElementById('tweaks-root');
if (tweaksRoot) {
  ReactDOM.createRoot(tweaksRoot).render(<TweaksApp />);
}
