/* global React, Icon */
// ===== סרגל נגישות (WCAG 2.1 AA / ת"י 5568) =====
// Toggles body classes consumed by CSS in index.html.
const { useState: useStateA11y, useEffect: useEffectA11y } = React;

const A11Y_KEY = 'mln_a11y';

const AccessibilityBar = () => {
  const [open, setOpen] = useStateA11y(false);
  const [settings, setSettings] = useStateA11y(() => {
    try { return JSON.parse(localStorage.getItem(A11Y_KEY)) || {}; } catch (e) { return {}; }
  });

  // Apply classes to <body> + persist
  useEffectA11y(() => {
    const b = document.body;
    b.classList.toggle('a11y-large-text', !!settings.largeText);
    b.classList.toggle('a11y-xl-text', !!settings.xlText);
    b.classList.toggle('a11y-high-contrast', !!settings.highContrast);
    b.classList.toggle('a11y-readable-font', !!settings.readableFont);
    b.classList.toggle('a11y-spacing', !!settings.spacing);
    b.classList.toggle('a11y-links', !!settings.highlightLinks);
    b.classList.toggle('a11y-pause-motion', !!settings.pauseMotion);
    try { localStorage.setItem(A11Y_KEY, JSON.stringify(settings)); } catch (e) {}
  }, [settings]);

  const toggle = (key) => setSettings(s => ({ ...s, [key]: !s[key] }));
  const reset = () => setSettings({});

  const items = [
    { key: 'largeText',      label: 'הגדלת טקסט',       icon: 'plus' },
    { key: 'xlText',         label: 'טקסט גדול מאוד',   icon: 'plus' },
    { key: 'highContrast',   label: 'ניגודיות גבוהה',    icon: 'eye' },
    { key: 'readableFont',   label: 'גופן קריא',         icon: 'edit' },
    { key: 'spacing',        label: 'ריווח שורות',       icon: 'menu' },
    { key: 'highlightLinks', label: 'הדגשת קישורים',     icon: 'arrowRight' },
    { key: 'pauseMotion',    label: 'עצירת אנימציות',    icon: 'x' },
  ];

  return (
    <React.Fragment>
      {/* Toggle button — fixed bottom-start corner */}
      <button
        onClick={() => setOpen(o => !o)}
        aria-label="תפריט נגישות"
        aria-expanded={open}
        title="נגישות"
        style={{
          position: 'fixed', bottom: 20, insetInlineStart: 20, zIndex: 400,
          width: 54, height: 54, borderRadius: '50%',
          background: '#1A4FA0', color: '#fff', border: '3px solid #fff',
          cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center',
          boxShadow: '0 6px 20px rgba(0,0,0,0.28)',
        }}
      >
        {/* universal accessibility icon */}
        <svg width="28" height="28" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
          <circle cx="12" cy="3.5" r="1.8" />
          <path d="M12 6c-2.5 0-5 .5-7 1 0 0-.3 1.6 .2 1.8 2 .6 4 .8 4 .8s.1 2-.2 3.4c-.3 1.6-1.6 5.4-1.6 5.4s.2 1.3 1.2 1.1c.7-.2 1.6-3.8 2.2-5.4h2.4c.6 1.6 1.5 5.2 2.2 5.4 1 .2 1.2-1.1 1.2-1.1s-1.3-3.8-1.6-5.4c-.3-1.4-.2-3.4-.2-3.4s2-.2 4-.8c.5-.2.2-1.8.2-1.8-2-.5-4.5-1-7-1z" />
        </svg>
      </button>

      {/* Panel */}
      {open && (
        <div role="dialog" aria-label="הגדרות נגישות" style={{
          position: 'fixed', bottom: 84, insetInlineStart: 20, zIndex: 400,
          width: 'min(300px, calc(100vw - 40px))',
          background: '#fff', borderRadius: 16, boxShadow: '0 16px 50px rgba(0,0,0,0.25)',
          border: '1px solid #e0e0e0', padding: 18, animation: 'mlnPop 240ms ease',
        }}>
          <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 14 }}>
            <strong style={{ fontFamily: 'var(--font-body)', fontSize: 16, color: '#1C1C1C' }}>נגישות</strong>
            <button onClick={() => setOpen(false)} aria-label="סגירה" style={{ background: 'none', border: 'none', cursor: 'pointer', color: '#888', display: 'flex' }}><Icon name="x" size={18} /></button>
          </div>
          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 }}>
            {items.map(it => {
              const active = !!settings[it.key];
              return (
                <button key={it.key} onClick={() => toggle(it.key)} aria-pressed={active}
                  style={{
                    display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6,
                    padding: '12px 8px', borderRadius: 10, cursor: 'pointer',
                    fontFamily: 'var(--font-body)', fontSize: 12.5, fontWeight: 600,
                    border: active ? '2px solid #1A4FA0' : '1.5px solid #e0e0e0',
                    background: active ? '#EAF1FB' : '#fff',
                    color: active ? '#1A4FA0' : '#444',
                    textAlign: 'center', lineHeight: 1.3,
                  }}>
                  <Icon name={it.icon} size={18} />
                  {it.label}
                </button>
              );
            })}
          </div>
          <button onClick={reset} style={{
            marginTop: 12, width: '100%', padding: '10px', borderRadius: 10,
            border: '1.5px solid #e0e0e0', background: '#f7f7f7', cursor: 'pointer',
            fontFamily: 'var(--font-body)', fontSize: 13.5, fontWeight: 600, color: '#666',
          }}>איפוס הגדרות</button>
          <p style={{ fontFamily: 'var(--font-body)', fontSize: 11, color: '#999', textAlign: 'center', margin: '10px 0 0' }}>
            תואם תקן ישראלי 5568 · WCAG 2.1 AA
          </p>
        </div>
      )}
    </React.Fragment>
  );
};
window.AccessibilityBar = AccessibilityBar;
