/* global React */
const { useState, useEffect, useRef } = React;

// ============================================================
//  Worldonix Infotech — shared chrome (Navbar / Footer / etc.)
// ============================================================

function Navbar() {
  const { go, path } = useRouter();
  const { open } = useBooking();
  const [scrolled, setScrolled] = useState(false);
  const [menuOpen, setMenuOpen] = useState(false);

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 12);
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  useEffect(() => {
    document.body.style.overflow = menuOpen ? "hidden" : "";
    return () => {
      document.body.style.overflow = "";
    };
  }, [menuOpen]);

  const navItems = [
    { to: "/", label: "Home" },
    { to: "/about", label: "About" },
    { to: "/services", label: "Services" },
    { to: "/industries", label: "Industries" },
    { to: "/delivery", label: "Delivery" },
    { to: "/contact", label: "Contact" },
  ];

  const isHome = path === "/";
  const dark = scrolled || !isHome;

  return (
    <header
      style={{
        position: "fixed",
        top: 0,
        left: 0,
        right: 0,
        zIndex: 50,
        padding: scrolled ? "12px 0" : "20px 0",
        transition:
          "padding 250ms ease, background 250ms ease, backdrop-filter 250ms ease, border-color 250ms ease",
        background: dark ? "rgba(10,18,40,0.82)" : "transparent",
        backdropFilter: dark ? "blur(14px)" : "none",
        WebkitBackdropFilter: dark ? "blur(14px)" : "none",
        borderBottom: `1px solid ${dark ? "rgba(248,250,255,0.08)" : "transparent"}`,
      }}
    >
      <div
        className="container"
        style={{
          display: "flex",
          alignItems: "center",
          justifyContent: "space-between",
          gap: 24,
        }}
      >
        <Link
          to="/"
          className="u-link"
          style={{
            display: "inline-flex",
            alignItems: "center",
            gap: 10,
            color: "#f8faff",
          }}
        >
          <SpikeMark size={20} />
          <span
            style={{
              fontFamily: "var(--font-display)",
              fontSize: 22,
              letterSpacing: "-0.5px",
              color: "#f8faff",
            }}
          >
            Worldonix
          </span>
          <span
            style={{
              fontSize: 11,
              letterSpacing: "1.5px",
              textTransform: "uppercase",
              color: "rgba(248,250,255,0.55)",
              marginLeft: 6,
            }}
          >
            Infotech
          </span>
        </Link>

        <nav
          className="nav-desktop"
          style={{ display: "flex", alignItems: "center", gap: 30 }}
        >
          {navItems.map((n) => {
            const active = path === n.to;
            return (
              <Link
                key={n.to}
                to={n.to}
                className="u-link"
                style={{
                  color: active ? "#f8faff" : "rgba(248,250,255,0.7)",
                  fontSize: 14,
                  fontWeight: active ? 500 : 400,
                  fontFamily: "var(--font-body)",
                }}
              >
                {n.label}
              </Link>
            );
          })}
        </nav>

        <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
          <Magnetic strength={0.25}>
            <button
              className="btn-coral nav-cta"
              data-magnetic
              onClick={() => open()}
            >
              Get free consultation <Arrow />
            </button>
          </Magnetic>
          <button
            className="nav-burger"
            aria-label="Open menu"
            onClick={() => setMenuOpen(true)}
            style={{
              display: "none",
              width: 44,
              height: 44,
              borderRadius: 9999,
              border: "1px solid rgba(248,250,255,0.25)",
              background: "transparent",
              cursor: "pointer",
              color: "#f8faff",
            }}
          >
            <svg
              width="18"
              height="18"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              strokeWidth="1.5"
            >
              <path d="M3 7h18M3 17h18" />
            </svg>
          </button>
        </div>
      </div>

      {menuOpen && (
        <div
          style={{
            position: "fixed",
            inset: 0,
            background: "var(--color-canvas)",
            zIndex: 100,
            padding: "32px 24px",
          }}
        >
          <div
            style={{
              display: "flex",
              justifyContent: "space-between",
              alignItems: "center",
            }}
          >
            <span
              style={{ display: "inline-flex", alignItems: "center", gap: 10 }}
            >
              <SpikeMark size={20} />
              <span style={{ fontFamily: "var(--font-display)", fontSize: 22 }}>
                Worldonix
              </span>
            </span>
            <button
              onClick={() => setMenuOpen(false)}
              style={{
                width: 44,
                height: 44,
                borderRadius: 9999,
                border: "1px solid var(--color-hairline)",
                background: "transparent",
              }}
            >
              <svg
                width="18"
                height="18"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                strokeWidth="1.5"
              >
                <path d="M5 5l14 14M5 19L19 5" />
              </svg>
            </button>
          </div>
          <nav
            style={{
              display: "flex",
              flexDirection: "column",
              gap: 8,
              marginTop: 40,
            }}
          >
            {navItems.map((n) => (
              <Link
                key={n.to}
                to={n.to}
                onClick={() => setMenuOpen(false)}
                style={{
                  fontFamily: "var(--font-display)",
                  fontSize: 40,
                  color: "var(--color-ink)",
                  padding: "10px 0",
                }}
              >
                {n.label}
              </Link>
            ))}
          </nav>
          <button
            className="btn-coral"
            style={{ marginTop: 32 }}
            onClick={() => {
              setMenuOpen(false);
              open();
            }}
          >
            Book a call <Arrow />
          </button>
        </div>
      )}

      <style>{`
        @media (max-width: 980px) {
          .nav-desktop { display: none !important; }
          .nav-burger { display: inline-flex !important; align-items: center; justify-content: center; }
        }
      `}</style>
    </header>
  );
}

// ============================================================
function Footer() {
  const { open } = useBooking();
  return (
    <footer className="dark" style={{ paddingTop: 96, paddingBottom: 48 }}>
      <div className="container">
        <div
          style={{
            display: "grid",
            gridTemplateColumns: "1.4fr 1fr 1fr 1fr",
            gap: 48,
          }}
          className="footer-grid"
        >
          <div>
            <div
              style={{ display: "inline-flex", alignItems: "center", gap: 10 }}
            >
              <SpikeMark size={22} color="var(--color-on-dark)" />
              <span
                style={{
                  fontFamily: "var(--font-display)",
                  fontSize: 28,
                  letterSpacing: "-0.5px",
                  color: "var(--color-on-dark)",
                }}
              >
                Worldonix
              </span>
            </div>
            <p
              style={{
                marginTop: 16,
                maxWidth: 360,
                color: "var(--color-on-dark-soft)",
              }}
            >
              We build, run, and secure the systems that move modern businesses
              forward.
            </p>
            <button
              className="btn-coral"
              style={{ marginTop: 28 }}
              onClick={() => open()}
            >
              Book a call <Arrow />
            </button>
          </div>
          <div>
            <div
              className="eyebrow"
              style={{ color: "var(--color-on-dark-soft)" }}
            >
              Company
            </div>
            <ul
              style={{
                listStyle: "none",
                padding: 0,
                margin: "16px 0 0",
                display: "flex",
                flexDirection: "column",
                gap: 10,
              }}
            >
              <li>
                <Link
                  to="/about"
                  style={{ color: "var(--color-on-dark)" }}
                  className="u-link"
                >
                  About
                </Link>
              </li>
              <li>
                <Link
                  to="/services"
                  style={{ color: "var(--color-on-dark)" }}
                  className="u-link"
                >
                  Services
                </Link>
              </li>
              <li>
                <Link
                  to="/industries"
                  style={{ color: "var(--color-on-dark)" }}
                  className="u-link"
                >
                  Industries
                </Link>
              </li>
              <li>
                <Link
                  to="/delivery"
                  style={{ color: "var(--color-on-dark)" }}
                  className="u-link"
                >
                  Delivery model
                </Link>
              </li>
            </ul>
          </div>
          <div>
            <div
              className="eyebrow"
              style={{ color: "var(--color-on-dark-soft)" }}
            >
              Services
            </div>
            <ul
              style={{
                listStyle: "none",
                padding: 0,
                margin: "16px 0 0",
                display: "flex",
                flexDirection: "column",
                gap: 10,
                color: "var(--color-on-dark)",
              }}
            >
              <li>IT consulting</li>
              <li>Application development</li>
              <li>Cloud & infrastructure</li>
              <li>Cybersecurity</li>
              <li>Managed IT, 24/7</li>
            </ul>
          </div>
          <div>
            <div
              className="eyebrow"
              style={{ color: "var(--color-on-dark-soft)" }}
            >
              Reach us
            </div>
            <ul
              style={{
                listStyle: "none",
                padding: 0,
                margin: "16px 0 0",
                display: "flex",
                flexDirection: "column",
                gap: 10,
                color: "var(--color-on-dark)",
              }}
            >
              <li>info@worldonixit.com</li>
              <li>7497814202</li>
              <li>Gurgaon</li>
            </ul>
          </div>
        </div>

        {/* giant wordmark */}
        <div style={{ marginTop: 80, overflow: "hidden" }}>
          <Reveal kind="reveal-up">
            <div
              style={{
                fontFamily: "var(--font-display)",
                color: "var(--color-on-dark)",
                fontSize: "clamp(80px, 18vw, 280px)",
                letterSpacing: "-0.04em",
                lineHeight: 0.9,
                textAlign: "center",
                whiteSpace: "nowrap",
              }}
            >
              Worldonix.
            </div>
          </Reveal>
        </div>

        <div className="rule-dark" style={{ marginTop: 48 }} />
        <div
          style={{
            display: "flex",
            justifyContent: "space-between",
            alignItems: "center",
            marginTop: 24,
            color: "var(--color-on-dark-soft)",
            fontSize: 13,
          }}
          className="footer-bottom"
        >
          <span>© 2026 Worldonix Infotech Solutions Pvt. Ltd.</span>
          <span style={{ display: "inline-flex", gap: 24 }}>
            <span>Privacy</span>
            <span>Terms</span>
            <span>Security</span>
          </span>
        </div>
      </div>
      <style>{`
        @media (max-width: 880px) {
          .footer-grid { grid-template-columns: 1fr 1fr !important; }
          .footer-bottom { flex-direction: column; gap: 8px; }
        }
      `}</style>
    </footer>
  );
}

// ============================================================
function Marquee({ items, speed = "slow", dark = false }) {
  const content = (
    <div className={`marquee-track ${speed}`}>
      {[...items, ...items, ...items].map((it, i) => (
        <span
          key={i}
          style={{
            display: "inline-flex",
            alignItems: "center",
            gap: 24,
            fontFamily: "var(--font-display)",
            fontSize: "clamp(48px, 9vw, 120px)",
            letterSpacing: "-0.03em",
            lineHeight: 1,
            color: dark ? "var(--color-on-dark)" : "var(--color-ink)",
            whiteSpace: "nowrap",
          }}
        >
          {it}
          <SpikeMark
            size={36}
            color={dark ? "var(--color-primary)" : "var(--color-primary)"}
          />
        </span>
      ))}
    </div>
  );
  return <div className="marquee">{content}</div>;
}

// ============================================================
function CTASection() {
  const { open } = useBooking();
  return (
    <section
      className="dark"
      style={{ padding: "120px 0", position: "relative", overflow: "hidden" }}
      data-screen-label="cta"
    >
      <div
        className="container"
        style={{ textAlign: "center", position: "relative", zIndex: 1 }}
      >
        <Reveal kind="reveal-up">
          <div
            className="eyebrow"
            style={{ color: "var(--color-on-dark-soft)" }}
          >
            <span className="dot" /> Ready when you are
          </div>
        </Reveal>
        <h2
          style={{
            fontFamily: "var(--font-display)",
            fontWeight: 400,
            fontSize: "clamp(48px, 8vw, 120px)",
            letterSpacing: "-0.03em",
            lineHeight: 1.02,
            marginTop: 24,
            color: "var(--color-on-dark)",
          }}
        >
          <WordReveal text="Let's build something" />
          <br />
          <span style={{ fontStyle: "italic", color: "var(--color-primary)" }}>
            <WordReveal text="that ships." baseDelay={300} />
          </span>
        </h2>
        <Reveal kind="reveal" delay={500}>
          <p
            style={{
              maxWidth: 560,
              margin: "28px auto 0",
              color: "var(--color-on-dark-soft)",
              fontSize: 17,
            }}
          >
            A 30-minute call with one of our principals. No deck. No sales
            pitch. Just a clear answer to: where are you stuck, and what does it
            take to fix it.
          </p>
        </Reveal>
        <Reveal kind="reveal" delay={700}>
          <div
            style={{
              display: "inline-flex",
              gap: 12,
              marginTop: 36,
              flexWrap: "wrap",
              justifyContent: "center",
            }}
          >
            <Magnetic>
              <button
                className="btn-coral"
                data-magnetic
                onClick={() => open()}
              >
                Book a 30-min call <Arrow />
              </button>
            </Magnetic>
            <Link to="/services">
              <button className="btn-ghost on-dark">Explore services</button>
            </Link>
          </div>
        </Reveal>
      </div>

      {/* spike mark watermark */}
      <div
        style={{
          position: "absolute",
          right: -120,
          bottom: -120,
          opacity: 0.06,
          pointerEvents: "none",
        }}
      >
        <SpikeMark size={520} color="var(--color-on-dark)" />
      </div>
    </section>
  );
}

Object.assign(window, { Navbar, Footer, Marquee, CTASection });
