/* global React */
const { useState: useNavState, useEffect: useNavEffect } = React;

function ThemeToggle({ dark, onToggle }) {
  return (
    <button
      type="button"
      className="fk-theme-toggle"
      onClick={onToggle}
      aria-label={dark ? 'Switch to day mode' : 'Switch to night mode'}
      title={dark ? 'Day' : 'Night'}
    >
      <span className={`fk-theme-toggle__track ${dark ? 'is-night' : 'is-day'}`} aria-hidden="true">
        <span className="fk-theme-toggle__thumb">
          {/* Sun */}
          <svg className="fk-theme-toggle__icon sun" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round">
            <circle cx="12" cy="12" r="4" />
            <path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41" />
          </svg>
          {/* Moon */}
          <svg className="fk-theme-toggle__icon moon" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round">
            <path d="M20 14.5A8 8 0 0 1 9.5 4a8 8 0 1 0 10.5 10.5z" />
          </svg>
        </span>
        <svg className="fk-theme-toggle__bg sun-bg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
          <circle cx="12" cy="12" r="3.4" />
          <path d="M12 3.5v1.6M12 18.9v1.6M5.4 5.4l1.1 1.1M17.5 17.5l1.1 1.1M3.5 12h1.6M18.9 12h1.6M5.4 18.6l1.1-1.1M17.5 6.5l1.1-1.1" />
        </svg>
        <svg className="fk-theme-toggle__bg moon-bg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
          <path d="M19 13.5A6.5 6.5 0 0 1 10.5 5a6.5 6.5 0 1 0 8.5 8.5z" />
          <circle cx="6.2" cy="6.6" r="0.6" fill="currentColor" />
          <circle cx="20" cy="7" r="0.5" fill="currentColor" />
          <circle cx="18.5" cy="3.2" r="0.4" fill="currentColor" />
        </svg>
      </span>
    </button>
  );
}

function Nav({ onJump, dark, onToggleTheme }) {
  const items = [
    { id: 'about', label: 'About' },
    { id: 'work', label: 'Work' },
    { id: 'journal', label: 'Journal' },
    { id: 'speaking', label: 'Speaking' },
    { id: 'contact', label: 'Contact' },
  ];
  const [scrolled, setScrolled] = useNavState(false);
  useNavEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 40);
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  return (
    <nav className="fk-nav" style={ scrolled ? { boxShadow: '0 1px 0 0 var(--rule)' } : null }>
      <a className="fk-nav__brand" href="#top" onClick={(e) => { e.preventDefault(); onJump('top'); }}>
        <img src="assets/monogram-fk.svg" alt="" />
        <svg className="fk-nav__wm-img" viewBox="0 0 790 140" xmlns="http://www.w3.org/2000/svg" aria-label="Farid Kheloco.">
          <text x="0" y="108" fontFamily="'Schibsted Grotesk',sans-serif" fontWeight="500" fontSize="116" letterSpacing="-0.025em" fill="#6E9A92">Farid</text>
          <text x="278" y="108" fontFamily="'Schibsted Grotesk',sans-serif" fontWeight="900" fontSize="116" letterSpacing="-0.04em" fill="#0E3A40">Kheloco</text>
          <circle cx="762" cy="102" r="11" fill="#DD6629"></circle>
        </svg>
      </a>
      <ul className="fk-nav__links">
        {items.map((it) => (
          <li key={it.id}>
            <a href={`#${it.id}`} onClick={(e) => { e.preventDefault(); onJump(it.id); }}>
              {it.label}
            </a>
          </li>
        ))}
      </ul>
      <div className="fk-nav__right">
        <ThemeToggle dark={dark} onToggle={onToggleTheme} />
        <a className="fk-btn fk-btn--pacific" href="#contact" onClick={(e) => { e.preventDefault(); onJump('contact'); }}>
          Get in touch →
        </a>
      </div>
    </nav>
  );
}

window.Nav = Nav;
