// Components for idėAs Productions

const { useState, useEffect, useRef } = React;

// ---------- Hooks ----------
function useReveal() {
  useEffect(() => {
    const els = document.querySelectorAll('.reveal, .split-line');
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) {
          e.target.classList.add('in');
          io.unobserve(e.target);
        }
      });
    }, { threshold: 0.12, rootMargin: '0px 0px -8% 0px' });
    els.forEach(el => io.observe(el));
    return () => io.disconnect();
  }, []);
}

// ---------- Nav ----------
function Nav({ lang, setLang, t }) {
  const [visible, setVisible] = useState(false);

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

  return (
    <nav className={`nav${visible ? ' nav-visible' : ''}`}>
      <a href="#top" className="brand" style={{ textDecoration: 'none', color: 'inherit' }}>
        idė<i>A</i>s · productions
      </a>
      <div className="nav-links">
        <a href="#works">{t.nav.works}</a>
        <a href="#studio">{t.nav.studio}</a>
        <a href="#places">{t.nav.places}</a>
        <a href="#contact">{t.nav.contact}</a>
      </div>
      <div className="lang-toggle">
        {['es' /*, 'en', 'fr' */].map((l, i) => (
          <React.Fragment key={l}>
            {i > 0 && <span className="sep">/</span>}
            <button className={lang === l ? 'active' : ''} onClick={() => setLang(l)}>{l}</button>
          </React.Fragment>
        ))}
      </div>
    </nav>
  );
}

// ---------- Hero ----------
function Hero({ t, videoSrc }) {
  const videoRef = useRef(null);
  useEffect(() => {
    const v = videoRef.current;
    if (v) v.play().catch(() => {});
  }, [videoSrc]);

  return (
    <section className="hero" id="top">
      <div className="hero-frame">
        <video ref={videoRef} src={videoSrc} autoPlay muted loop playsInline />
        <div className="hero-corners">
          <div className="tl"></div>
          <div className="tr"></div>
          <div className="bl"></div>
          <div className="br"></div>
        </div>
        <div className="hero-meta tl">{t.hero.meta_tl}</div>
        <div className="hero-meta tr">{t.hero.meta_tr}</div>
        <div className="hero-meta bl"><span className="blink"></span>{t.hero.meta_bl}</div>
        <div className="hero-meta br">{t.hero.meta_br}</div>
      </div>

      <div className="hero-dark-bottom">
        <div className="hero-bottom">
          <h1 className="hero-headline display">
            <span className="split-line"><span>{t.hero.headline_a}</span></span>
            <span className="split-line" style={{ '--rd': '.1s' }}><span>{t.hero.headline_b}</span></span>
            <span className="split-line" style={{ '--rd': '.2s' }}><span>{t.hero.headline_c}</span></span>
          </h1>
          <div></div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 16, alignItems: 'flex-end' }}>
            <p className="hero-subline reveal" style={{ '--rd': '.4s', textAlign: 'right' }}>{t.hero.sub}</p>
          </div>
        </div>
      </div>
    </section>
  );
}

// ---------- Marquee ----------
function Marquee({ items }) {
  const repeated = [...items, ...items, ...items];
  return (
    <div className="marquee">
      <div className="marquee-track">
        {repeated.map((it, i) => <span key={i}>{it}</span>)}
      </div>
    </div>
  );
}

// ---------- Manifesto ----------
function Manifesto({ t }) {
  return (
    <section className="section" id="studio">
      <p className="manifesto-body display reveal">
        {t.manifesto.body_pre} <em>{t.manifesto.body_em}</em> {t.manifesto.body_post} <span className="quiet">{t.manifesto.body_quiet}</span>
      </p>
      
    </section>
  );
}

// ---------- Works ----------
function Works({ t }) {
  function enterGallery() {
    if (window.__ideAsGallery) {
      window.__ideAsGallery.open(document.body, null);
    }
  }

  return (
    <section className="section" id="works">
      <div className="works-header">
        <button className="gallery-btn" onClick={enterGallery}>
          <span className="gallery-btn-cube">
            <svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M9 1.5L16 5.25V12.75L9 16.5L2 12.75V5.25L9 1.5Z" stroke="currentColor" strokeWidth="1" strokeLinejoin="round"/>
              <path d="M9 1.5V16.5M2 5.25L9 9L16 5.25M9 9L9 16.5" stroke="currentColor" strokeWidth="0.7" strokeDasharray="1.5 1"/>
            </svg>
          </span>
          <span className="gallery-btn-text">3D mode</span>
        </button>
      </div>
      <div className="works">

        <a href="#" className="work w-1 reveal">
          <div className="frame">
            <video src="assets/works/001.mp4" autoPlay muted loop playsInline />
          </div>
          <div className="meta">
            <h3>{t.works.list[0].title}</h3>
            <span className="tag">{t.works.list[0].tag}</span>
          </div>
        </a>

        <a href="#" className="work w-2 reveal" style={{ '--rd': '.06s' }}>
          <div className="frame">
            <img src="assets/works/005.jpg" alt={t.works.list[1].title} />
          </div>
          <div className="meta">
            <h3>{t.works.list[1].title}</h3>
            <span className="tag">{t.works.list[1].tag}</span>
          </div>
        </a>

        <a href="#" className="work w-3 reveal">
          <div className="frame">
            <video src="assets/works/004.mp4" autoPlay muted loop playsInline />
          </div>
          <div className="meta">
            <h3>{t.works.list[2].title}</h3>
            <span className="tag">{t.works.list[2].tag}</span>
          </div>
        </a>

        <a href="#" className="work w-4 reveal" style={{ '--rd': '.06s' }}>
          <div className="frame">
            <img src="assets/works/006.jpg" alt={t.works.list[3].title} />
          </div>
          <div className="meta">
            <h3>{t.works.list[3].title}</h3>
            <span className="tag">{t.works.list[3].tag}</span>
          </div>
        </a>

        <a href="#" className="work w-5 reveal">
          <div className="frame">
            <video src="assets/works/002.mp4" autoPlay muted loop playsInline />
          </div>
          <div className="meta">
            <h3>{t.works.list[4].title}</h3>
            <span className="tag">{t.works.list[4].tag}</span>
          </div>
        </a>

        <a href="#" className="work w-6 reveal" style={{ '--rd': '.06s' }}>
          <div className="frame">
            <video src="assets/works/003.mp4" autoPlay muted loop playsInline />
          </div>
          <div className="meta">
            <h3>{t.works.list[5].title}</h3>
            <span className="tag">{t.works.list[5].tag}</span>
          </div>
        </a>

      </div>
    </section>
  );
}

// ---------- City Map (Leaflet) ----------
function CityMap({ mapId, lat, lng }) {
  const instanceRef = useRef(null);

  useEffect(() => {
    if (instanceRef.current || !window.L) return;

    const map = L.map(mapId, {
      center: [lat, lng],
      zoom: 10,
      zoomControl: false,
      attributionControl: false,
      dragging: false,
      scrollWheelZoom: false,
      doubleClickZoom: false,
      touchZoom: false,
      keyboard: false,
    });

    L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
      maxZoom: 19,
    }).addTo(map);

    const pulseIcon = L.divIcon({
      className: '',
      html: '<div class="map-marker"><span class="map-label">id<span class="map-label-e">ė</span>As</span><div class="map-pulse"></div><div class="map-dot"></div></div>',
      iconSize: [80, 20],
      iconAnchor: [40, 10],
    });

    L.marker([lat, lng], { icon: pulseIcon }).addTo(map);
    instanceRef.current = map;

    return () => {
      map.remove();
      instanceRef.current = null;
    };
  }, []);

  return <div id={mapId} className="loc-map-leaflet" />;
}

// ---------- Locations ----------
function Locations({ t }) {
  return (
    <section className="section" id="places">
      <div className="locations reveal">
        {/* Temporarily disabled Montreal Section
        <div className="loc">
          <div className="loc-header">
            <div className="loc-name">{t.places.mtl_name}<small>{t.places.mtl_country}</small></div>
            <div className="loc-coord">{t.places.mtl_coord.split('\n').map((l,i)=>(<div key={i}>{l}</div>))}</div>
          </div>
          <CityMap mapId="map-mtl" lat={45.5017} lng={-73.5673} />
          <div className="loc-foot">
            <p>{t.places.mtl_p}</p>
            <span className="role">{t.places.mtl_role}</span>
          </div>
        </div>
        */}
        
        <div className="loc">
          <div className="loc-header">
            <div className="loc-name">{t.places.sv_name}<small>{t.places.sv_country}</small></div>
            <div className="loc-coord">{t.places.sv_coord.split('\n').map((l,i)=>(<div key={i}>{l}</div>))}</div>
          </div>
          <CityMap mapId="map-sv" lat={13.6929} lng={-89.2182} />
          <div className="loc-foot">
            <p>{t.places.sv_p}</p>
            <span className="role">{t.places.sv_role}</span>
          </div>
        </div>
      </div>
    </section>
  );
}

// ---------- Contact ----------
function Contact({ t }) {
  return (
    <section className="section" id="contact">
      <div className="contact">
        <h2 className="contact-headline display reveal">
          {t.contact.head_a} <em>{t.contact.head_em}</em> {t.contact.head_b} <em>{t.contact.head_em2}</em>{t.contact.head_c}<br/>
          <a href="mailto:info@d1aloga.com">{t.contact.head_d}</a>
        </h2>
        <div className="contact-side reveal" style={{ '--rd': '.15s' }}>
          <div className="contact-info">
            <a href="mailto:info@d1aloga.com">info@d1aloga.com</a>
            <span className="tel"></span>
          </div>
          {/* Temporarily disabled social media icons until accounts are created
          <div className="contact-socials">
            <a href="#" aria-label="LinkedIn">
              <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                <path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 01-2.063-2.065 2.064 2.064 0 112.063 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z"/>
              </svg>
            </a>
            <a href="#" aria-label="Instagram">
              <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                <path d="M12 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.919 4.919.058 1.265.069 1.645.069 4.849 0 3.205-.012 3.584-.069 4.849-.149 3.225-1.664 4.771-4.919 4.919-1.266.058-1.644.07-4.85.07-3.204 0-3.584-.012-4.849-.07-3.26-.149-4.771-1.699-4.919-4.92-.058-1.265-.07-1.644-.07-4.849 0-3.204.013-3.583.07-4.849.149-3.227 1.664-4.771 4.919-4.919 1.266-.057 1.645-.069 4.849-.069zM12 0C8.741 0 8.333.014 7.053.072 2.695.272.273 2.69.073 7.052.014 8.333 0 8.741 0 12c0 3.259.014 3.668.072 4.948.2 4.358 2.618 6.78 6.98 6.98C8.333 23.986 8.741 24 12 24c3.259 0 3.668-.014 4.948-.072 4.354-.2 6.782-2.618 6.979-6.98.059-1.28.073-1.689.073-4.948 0-3.259-.014-3.667-.072-4.947-.196-4.354-2.617-6.78-6.979-6.98C15.668.014 15.259 0 12 0zm0 5.838a6.162 6.162 0 100 12.324 6.162 6.162 0 000-12.324zM12 16a4 4 0 110-8 4 4 0 010 8zm6.406-11.845a1.44 1.44 0 100 2.881 1.44 1.44 0 000-2.881z"/>
              </svg>
            </a>
            <a href="#" aria-label="TikTok">
              <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                <path d="M12.525.02c1.31-.02 2.61-.01 3.91-.02.08 1.53.63 3.09 1.75 4.17 1.12 1.11 2.7 1.62 4.24 1.79v4.03c-1.44-.05-2.89-.35-4.2-.97-.57-.26-1.1-.59-1.62-.93-.01 2.92.01 5.84-.02 8.75-.08 1.4-.54 2.79-1.35 3.94-1.31 1.92-3.58 3.17-5.91 3.21-1.43.08-2.86-.31-4.08-1.03-2.02-1.19-3.44-3.37-3.65-5.71-.02-.5-.03-1-.01-1.49.18-1.9 1.12-3.72 2.58-4.96 1.66-1.44 3.98-2.13 6.15-1.72.02 1.48-.04 2.96-.04 4.44-.99-.32-2.15-.23-3.02.37-.63.41-1.11 1.04-1.36 1.75-.21.51-.15 1.07-.14 1.61.24 1.64 1.82 3.02 3.5 2.87 1.12-.01 2.19-.66 2.77-1.61.19-.33.4-.67.41-1.06.1-1.79.06-3.57.07-5.36.01-4.03-.01-8.05.02-12.07z"/>
              </svg>
            </a>
          </div>
          */}
        </div>
      </div>
    </section>
  );
}

// ---------- Footer ----------
function Foot({ t }) {
  return (
    <footer className="footer">
      <span><span className="brand-mini">idėAs x DiALOGA</span> · {t.footer.left}</span>
      <span>{t.footer.right}</span>
    </footer>
  );
}

Object.assign(window, {
  Nav, Hero, Marquee, Manifesto, Works, Locations, Contact, Foot, useReveal
});
