"use client";

import { CursorDotBackground } from "./CursorDotBackground";
import { HorizontalAccordion } from "./HorizontalAccordion";
import { InstagramFeed } from "./InstagramFeed";
import { useEffect, useRef, useState } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { DepthMap } from "./DepthMap";
import Image from "next/image";

export function TempPlaceholder() {
  const titleRef = useRef<HTMLHeadingElement | null>(null);
  const titleInnerRef = useRef<HTMLSpanElement | null>(null);
  const bioTextRef = useRef<HTMLSpanElement | null>(null);
  const [titleWidth, setTitleWidth] = useState<number | null>(null);
  const [bioFontSizePx, setBioFontSizePx] = useState<number | null>(null);
  const baseBioFontSizeRef = useRef<number | null>(null);
  const [isEasterEggOpen, setIsEasterEggOpen] = useState(false);
  const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 });
  const [isPigeonEggOpen, setIsPigeonEggOpen] = useState(false);
  const [pigeonMousePosition, setPigeonMousePosition] = useState({ x: 0, y: 0 });

  useEffect(() => {
    const measure = () => {
      const measuredTitleWidth = titleInnerRef.current?.offsetWidth ?? null;
      setTitleWidth(measuredTitleWidth);

      if (measuredTitleWidth && bioTextRef.current) {
        const element = bioTextRef.current;
        if (baseBioFontSizeRef.current === null) {
          const initialFontSize = parseFloat(getComputedStyle(element).fontSize);
          baseBioFontSizeRef.current = isNaN(initialFontSize) ? 16 : initialFontSize;
        }

        // Temporarily ensure we measure at base font size
        const baseFontSize = baseBioFontSizeRef.current ?? 16;
        const previousInlineFontSize = element.style.fontSize;
        element.style.fontSize = `${baseFontSize}px`;
        const bioNaturalWidth = element.offsetWidth;
        // Restore previous inline style before we set state (will update after render)
        element.style.fontSize = previousInlineFontSize;

        if (bioNaturalWidth > 0) {
          // On mobile, use a more conservative scaling to prevent cramped text
          const isMobile = window.innerWidth < 640; // sm breakpoint
          const isTablet = window.innerWidth < 1024; // lg breakpoint
          
          let maxScale;
          if (isMobile) {
            maxScale = 0.8; // Limit scaling on mobile
          } else if (isTablet) {
            maxScale = 1.2; // Allow more scaling on tablet
          } else {
            maxScale = 1.8; // Allow much more scaling on desktop
          }
          
          const scale = Math.min(measuredTitleWidth / bioNaturalWidth, maxScale);
          setBioFontSizePx(baseFontSize * scale);
        }
      }
    };
    measure();
    window.addEventListener("resize", measure);
    return () => window.removeEventListener("resize", measure);
  }, []);
  return (
    <section className="py-8 md:py-16 bg-black text-white min-h-screen">
      <div className="container mx-auto px-4 sm:px-6 lg:px-8 max-w-3xl">
        <div className="p-4 sm:p-6 md:p-8 relative">
          <CursorDotBackground 
            dotSize={2}
            dotSpacing={20}
            fadeDistance={80}
            opacity={0.5}
            className="rounded"
          />
          <div className="relative z-10">
        <p className="text-sm text-gray-500 mb-6">Last updated: 10-12-2025</p>

        <h1 ref={titleRef} className="text-3xl sm:text-4xl md:text-5xl font-bold mb-4 leading-tight">
          <span ref={titleInnerRef} className="inline-block">
            You've gotta be <em className="text-gray-400">shittin'</em> me.
          </span>
        </h1>
        <p className="text-base sm:text-lg mb-2 text-gray-300">This is the 20th time this has happened.</p>
        <p className="text-base sm:text-lg mb-6 md:mb-8 text-gray-400">
          <em>Nicholai broke the website, again.</em>
        </p>

        <div className="mb-8">
          <HorizontalAccordion trigger="How did we get here?">
            <div className="w-full">
              <p className="mb-4 text-gray-400 text-sm">
                <em>(TLDR: perfectionism is the mind killer)</em>
              </p>
              <ol className="list-decimal list-inside space-y-3 text-gray-300 leading-relaxed break-words">
                <li>We needed a website (circa January 2023)</li>
                <li>We tried to build one on squarespace (that shit sucks)</li>
                <li>
                  Nicholai figured "I know some html and javascript, why not just{" "}
                  <em>make</em> one."
                </li>
                <li>
                  But of course, <strong>the html site sucked</strong> and was
                  difficult to host.
                </li>
                <li>
                  And naturally, the website for some reason <em>needed</em> to look
                  good.
                </li>
                <li>
                  So then began a longwinded journey of Nicholai learning <em>react</em>
                </li>
                <li>Nicholai should've stuck to python.</li>
              </ol>
            </div>
          </HorizontalAccordion>
        </div>
        <h1 
          onClick={(e) => {
            setMousePosition({ x: e.clientX, y: e.clientY });
            setIsEasterEggOpen(true);
          }}
          className="text-4xl sm:text-5xl md:text-7xl lg:text-8xl xl:text-9xl font-black mb-4 md:mb-6 font-exo-2 text-center mx-auto leading-none cursor-pointer transition-opacity hover:opacity-80" 
          style={{ 
            color: '#000000',
            textShadow: '2px 2px 0px #ff4d00, 4px 4px 0px #ff4d00',
            width: titleWidth ? `${titleWidth}px` : undefined
          }}
        >
          <span ref={bioTextRef} className="inline-block" style={{ fontSize: bioFontSizePx ? `${bioFontSizePx}px` : undefined }}>
            BIOHAZARD
          </span>
        </h1>
        
        <AnimatePresence>
          {isEasterEggOpen && (
            <>
              <motion.div
                initial={{ opacity: 0 }}
                animate={{ opacity: 1 }}
                exit={{ opacity: 0 }}
                transition={{ duration: 0.3 }}
                className="fixed inset-0 z-50 bg-black/80"
                onClick={() => setIsEasterEggOpen(false)}
              />
              <motion.div
                initial={{ opacity: 0, scale: 0.7, x: '-50%', y: '-50%' }}
                animate={{ opacity: 1, scale: 1, x: '-50%', y: '-50%' }}
                exit={{ opacity: 0, scale: 0.7, x: '-50%', y: '-50%' }}
                transition={{ 
                  duration: 0.4,
                  ease: [0.16, 1, 0.3, 1]
                }}
                style={{
                  position: 'fixed',
                  left: mousePosition.x,
                  top: mousePosition.y,
                }}
                className="z-50 w-[90vw] max-w-[350px]"
                onMouseLeave={() => setIsEasterEggOpen(false)}
              >
                <div className="relative bg-black rounded-lg overflow-hidden shadow-2xl">
                  <motion.div
                    initial={{ opacity: 0, scale: 0.9 }}
                    animate={{ opacity: 1, scale: 1 }}
                    transition={{ 
                      duration: 0.3,
                      delay: 0.1,
                      ease: [0.16, 1, 0.3, 1]
                    }}
                    className="relative w-full aspect-square"
                  >
                    <DepthMap 
                      originalImg="/OLIVER.jpeg" 
                      depthImg="/OLIVER_depth.jpeg"
                      verticalThreshold={40}
                      horizontalThreshold={70}
                    />
                  </motion.div>
                </div>
              </motion.div>
            </>
          )}
        </AnimatePresence>
        
        <AnimatePresence>
          {isPigeonEggOpen && (
            <>
              <motion.div
                initial={{ opacity: 0 }}
                animate={{ opacity: 1 }}
                exit={{ opacity: 0 }}
                transition={{ duration: 0.3 }}
                className="fixed inset-0 z-50 bg-black/80"
                onClick={() => setIsPigeonEggOpen(false)}
              />
              <motion.div
                initial={{ opacity: 0, scale: 0.7, x: '-50%', y: '-50%' }}
                animate={{ opacity: 1, scale: 1, x: '-50%', y: '-50%' }}
                exit={{ opacity: 0, scale: 0.7, x: '-50%', y: '-50%' }}
                transition={{ 
                  duration: 0.4,
                  ease: [0.16, 1, 0.3, 1]
                }}
                style={{
                  position: 'fixed',
                  left: pigeonMousePosition.x,
                  top: pigeonMousePosition.y,
                }}
                className="z-50 w-[90vw] max-w-[400px]"
                onMouseLeave={() => setIsPigeonEggOpen(false)}
              >
                <div className="relative bg-black rounded-lg overflow-hidden shadow-2xl">
                  <motion.div
                    initial={{ opacity: 0, scale: 0.9 }}
                    animate={{ opacity: 1, scale: 1 }}
                    transition={{ 
                      duration: 0.3,
                      delay: 0.1,
                      ease: [0.16, 1, 0.3, 1]
                    }}
                    className="relative w-full"
                  >
                    <Image 
                      src="/no_pigeons_zone.gif"
                      alt="No pigeons zone"
                      width={400}
                      height={400}
                      className="w-full h-auto"
                      unoptimized
                    />
                  </motion.div>
                </div>
              </motion.div>
            </>
          )}
        </AnimatePresence>
        <p className="mb-6 md:mb-8 text-base sm:text-lg text-gray-300">
          <strong>Who we are:</strong> artists and technical people, we're
          better at VFX than we are at web design, I promise.
        </p>

        <p className="mb-4 text-base sm:text-lg">
          <strong>Here's our reel:</strong>{" "}
          <a
            href="https://f.io/Wgx3EAHu"
            className="hover:underline transition-all duration-300 hover:brightness-110 hover:scale-105 inline-block break-words"
            style={{ color: '#ff4d00' }}
            target="_blank"
            rel="noopener noreferrer"
          >
            Biohazard Reel Mar 2025 - Frame.io
          </a>
        </p>

        <p className="mb-4 md:mb-6 text-base sm:text-lg">
          <strong>Some projects we've worked on:</strong>
        </p>

        <ul className="mb-6 md:mb-8 space-y-3 md:space-y-4 text-sm sm:text-base">
          <li className="flex items-start">
            <span className="text-gray-400 mr-3 mt-1">•</span>
            <a
              href="https://www.instagram.com/biohazardvfx/"
              className="hover:underline transition-all duration-200 hover:brightness-105 block leading-relaxed"
              style={{ color: '#ff4d00' }}
              target="_blank"
              rel="noopener noreferrer"
            >
              Gstar Raw - Pommelhorse
            </a>
          </li>
          <li className="flex items-start">
            <span className="text-gray-400 mr-3 mt-1">•</span>
            <a
              href="https://www.youtube.com/watch?v=4QIZE708gJ4"
              className="hover:underline transition-all duration-200 hover:brightness-105 block leading-relaxed"
              style={{ color: '#ff4d00' }}
              target="_blank"
              rel="noopener noreferrer"
            >
              Post Malone - I Had Some Help<br className="sm:hidden" />
              <span className="sm:hidden">(feat. Morgan Wallen)</span>
              <span className="hidden sm:inline"> (feat. Morgan Wallen)</span>
            </a>
          </li>
          <li className="flex items-start">
            <span className="text-gray-400 mr-3 mt-1">•</span>
            <a
              href="https://www.youtube.com/watch?v=z2tUpLHdd4M"
              className="hover:underline transition-all duration-200 hover:brightness-105 block leading-relaxed"
              style={{ color: '#ff4d00' }}
              target="_blank"
              rel="noopener noreferrer"
            >
              The Wait Is Over | OFFICIAL TRAILER
            </a>
          </li>
          <li className="flex items-start">
            <span className="text-gray-400 mr-3 mt-1">•</span>
            <a
              href="https://www.youtube.com/watch?v=RCZ9wl1Up40"
              className="hover:underline transition-all duration-200 hover:brightness-105 block leading-relaxed"
              style={{ color: '#ff4d00' }}
              target="_blank"
              rel="noopener noreferrer"
            >
              2hollis Star Album Trailer
            </a>
          </li>
          <li className="flex items-start">
            <span className="text-gray-400 mr-3 mt-1">•</span>
            <a
              href="https://www.youtube.com/watch?v=yLxoVrFpLmQ"
              className="hover:underline transition-all duration-200 hover:brightness-105 block leading-relaxed"
              style={{ color: '#ff4d00' }}
              target="_blank"
              rel="noopener noreferrer"
            >
              Thanksgiving With Kai, Kevin &amp; Druski
            </a>
          </li>
          <li className="flex items-start">
            <span className="text-gray-400 mr-3 mt-1">•</span>
            <a
              href="https://www.youtube.com/watch?v=a2Zqdo9RbNs"
              className="hover:underline transition-all duration-200 hover:brightness-105 block leading-relaxed"
              style={{ color: '#ff4d00' }}
              target="_blank"
              rel="noopener noreferrer"
            >
              ENHYPEN (엔하이픈) Bad Desire<br className="sm:hidden" />
              <span className="sm:hidden">(With or Without You) Official MV</span>
              <span className="hidden sm:inline"> (With or Without You) Official MV</span>
            </a>
          </li>
        </ul>

        <InstagramFeed />

        <p className="mb-2 text-sm sm:text-base text-gray-300">
        Contact us:{" "}
          <a
            href="mailto:contact@biohazardvfx.com"
            className="hover:underline break-words inline-block"
            style={{ color: '#ff4d00' }}
            target="_blank"
            rel="noopener noreferrer"
          >
            contact@biohazardvfx.com
          </a>
        </p>
        <p className="text-sm sm:text-base text-gray-300">
          File a complaint:{" "}
          <a
            href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"
            className="hover:underline break-words inline-block"
            style={{ color: '#ff4d00' }}
            target="_blank"
            rel="noopener noreferrer"
          >
            help@biohazardvfx.com
          </a>
        </p>
        
        <p 
          onClick={(e) => {
            setPigeonMousePosition({ x: e.clientX, y: e.clientY });
            setIsPigeonEggOpen(true);
          }}
          className="text-xs text-gray-600 mt-4 cursor-pointer hover:text-gray-500 transition-colors"
        >
          No pigeons allowed in this zone 
        </p>
          </div>
        </div>
      </div>
    </section>
  );
}