const { useState, useEffect } = React;

function LangToggle() {
  const en = window.LANG === 'en';
  const page = window.location.pathname.includes('project.html') ? 'project.html' + window.location.search : 'index.html';
  const ptHref = en ? '../' + page : page;
  const enHref = en ? page : 'en/' + page;
  const st = (act) => ({ fontFamily: 'Helvetica,Arial,sans-serif', fontWeight: act ? 700 : 400, fontSize: 12, letterSpacing: '.14em', color: '#fff', opacity: act ? 1 : .45, textDecoration: 'none', transition: 'opacity .25s ease' });
  const hov = (e) => { e.currentTarget.style.opacity = '1'; };
  return (
    <div style={{ position: 'fixed', top: 20, left: '50%', transform: 'translateX(-50%)', zIndex: 300, display: 'flex', alignItems: 'center', gap: 8, mixBlendMode: 'difference' }}>
      <a href={ptHref} style={st(!en)} onMouseEnter={hov} onMouseLeave={(e) => { e.currentTarget.style.opacity = en ? '.45' : '1'; }}>PT</a>
      <span aria-hidden="true" style={{ color: '#fff', opacity: .3, fontSize: 11 }}>/</span>
      <a href={enHref} style={st(en)} onMouseEnter={hov} onMouseLeave={(e) => { e.currentTarget.style.opacity = en ? '1' : '.45'; }}>EN</a>
    </div>
  );
}

function WebsiteNav({ showLinks = true } = {}) {
  const [scrolled, setScrolled] = useState(false);
  const [menuOpen, setMenuOpen] = useState(false);
  const { isMobile } = useViewport();

  useEffect(() => {
    const fn = () => setScrolled(window.scrollY > window.innerHeight * 0.8);
    window.addEventListener('scroll', fn, { passive: true });
    fn();
    return () => window.removeEventListener('scroll', fn);
  }, []);

  // lock page scroll while the overlay menu is open
  useEffect(() => {
    document.body.style.overflow = menuOpen ? 'hidden' : '';
    return () => { document.body.style.overflow = ''; };
  }, [menuOpen]);

  // close menu if we grow back to desktop
  useEffect(() => { if (!isMobile) setMenuOpen(false); }, [isMobile]);

  const links = [['#portfolio', T('trabalhos', 'works')], ['#servicos', T('serviços', 'services')], ['#sobre', T('sobre', 'about')], ['#contato', T('contato', 'contact')]];

  if (isMobile) {
    if (!showLinks) {
      return (
        <nav style={{
          position: 'fixed', top: 0, left: 0, right: 0, zIndex: 200,
          display: 'flex', alignItems: 'center', justifyContent: 'space-between',
          padding: '0 20px', height: 60,
          background: scrolled ? '#F6F3F0' : 'transparent',
          borderBottom: scrolled ? '1px solid #DDD9D4' : '1px solid transparent',
          transition: 'background 200ms ease, border-color 200ms ease',
        }}>
          <a href={(window.PREFIX || '') + 'index.html#inicio'} style={{display:'flex', alignItems:'center', gap:12, textDecoration:'none'}}>
            <img src={AP('../../assets/logo-icon-dark.png')} alt="LSQ" style={{height:40, width:'auto'}} />
          </a>
          <LangToggle />
        </nav>
      );
    }
    const bar = (rot, ty) => ({
      width: 22, height: 2, borderRadius: 2,
      background: menuOpen ? '#1D1D1B' : '#fff',
      transition: 'transform .35s cubic-bezier(.76,0,.24,1), background .2s',
      transform: menuOpen ? 'translateY(' + ty + 'px) rotate(' + rot + 'deg)' : 'none',
    });
    return (
      <React.Fragment>
        <LangToggle />
        {/* Hamburger — always reachable on mobile; mix-blend so it reads on dark hero & light sections */}
        <button aria-label="Menu" onClick={() => setMenuOpen((o) => !o)} style={{
          position: 'fixed', top: 16, right: 16, zIndex: 300,
          width: 48, height: 48, borderRadius: '50%',
          display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 6,
          background: menuOpen ? '#E3DDD8' : 'transparent',
          border: 'none', cursor: 'pointer', padding: 0,
          mixBlendMode: menuOpen ? 'normal' : 'difference',
          WebkitTapHighlightColor: 'transparent',
        }}>
          <span style={bar(45, 4)}></span>
          <span style={bar(-45, -4)}></span>
        </button>

        {/* Full-screen overlay menu */}
        <div style={{
          position: 'fixed', inset: 0, zIndex: 250,
          background: '#111110',
          display: 'flex', flexDirection: 'column', justifyContent: 'space-between',
          padding: '92px 26px 40px',
          transform: menuOpen ? 'translateY(0)' : 'translateY(-101%)',
          transition: 'transform .6s cubic-bezier(.76,0,.24,1)',
          pointerEvents: menuOpen ? 'auto' : 'none',
        }}>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
            {links.map(([href, label]) => (
              <a key={label} href={href} onClick={() => setMenuOpen(false)} style={{
                fontFamily: 'HelveticaCompressed, "Arial Narrow", Arial, sans-serif', fontWeight: 700,
                fontSize: 'clamp(42px, 14vw, 76px)', textTransform: 'uppercase', lineHeight: 1.05,
                color: '#E3DDD8', textDecoration: 'none', letterSpacing: '-.01em',
              }}>{label}</a>
            ))}
          </div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 10, borderTop: '1px solid #2C2C2A', paddingTop: 24 }}>
            <a href="mailto:lucasqueiroz@gmail.com" style={{ fontFamily: 'Helvetica, Arial, sans-serif', fontSize: 14, letterSpacing: '.06em', color: '#E3DDD8', textDecoration: 'none' }}>lucasqueiroz@gmail.com</a>
            <a href="https://wa.me/5511975514667?text=Quero%20trabalhar%20com%20a%20LSQ." target="_blank" style={{ fontFamily: 'Helvetica, Arial, sans-serif', fontSize: 14, letterSpacing: '.06em', color: '#5A5A57', textDecoration: 'none' }}>+55 11 97551 4667</a>
          </div>
        </div>
      </React.Fragment>
    );
  }

  return (
    <React.Fragment>
    <LangToggle />
    <nav style={{
      position: 'fixed', top: 0, left: 0, right: 0, zIndex: 200,
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      padding: '0 56px', height: 60,
      background: scrolled ? '#F6F3F0' : 'transparent',
      borderBottom: scrolled ? '1px solid #DDD9D4' : '1px solid transparent',
      transform: scrolled ? 'translateY(0)' : 'translateY(-100%)',
      transition: 'transform 280ms ease, background 200ms ease, border-color 200ms ease',
      pointerEvents: scrolled ? 'auto' : 'none',
    }}>
      <a href={showLinks ? '#inicio' : (window.PREFIX || '') + 'index.html#inicio'} style={{display:'flex', alignItems:'center', gap:12, textDecoration:'none'}}>
        <img src={AP('../../assets/logo-icon-dark.png')} alt="LSQ" style={{height:48, width:'auto'}} />
      </a>

      {showLinks &&
      <div style={{display:'flex', alignItems:'center', gap:36}}>
        {links.map(([href, label]) => (
          <a key={label} href={href} style={{
            fontFamily:'Helvetica, Arial, sans-serif', fontWeight:400, fontSize:14,
            color:'#1D1D1B', textDecoration:'none', letterSpacing:'.01em',
            display:'flex', alignItems:'center', gap:4,
            transition:'opacity 150ms',
          }}
          onMouseEnter={e => e.currentTarget.style.opacity='.5'}
          onMouseLeave={e => e.currentTarget.style.opacity='1'}
          >{label} <span style={{fontWeight:400}}>→</span></a>
        ))}
      </div>}
    </nav>
    </React.Fragment>
  );
}

Object.assign(window, { WebsiteNav });
