import React, { useCallback, useRef, useState } from "react";
import Link from "next/link";

export default function BottomFooter() {
  // ig easter egg
    const [lavaActive, setLavaActive] = useState(false);
  const lavaTimeout = useRef<any>(null);

  // LinkedIn easter egg
  const [shaking, setShaking] = useState(false);
  const shakeTimeout = useRef<any>(null);

  // x easter egg
  const [showCracks, setShowCracks] = useState(false);
  const [collapse, setCollapse] = useState(false);
  const crackTimeout = useRef<any>(null);

  // Lava flood handler (top-down flood)
  const handleInstagramClick = useCallback((e: React.MouseEvent) => {
    e.preventDefault();
    setLavaActive(true);
    clearTimeout(lavaTimeout.current);
    lavaTimeout.current = setTimeout(() => setLavaActive(false), 2000);
  }, []);

  // LinkedIn shake handler
  const handleLinkedInClick = useCallback((e: React.MouseEvent) => {
    e.preventDefault();
    if (shaking) return; // prevent stacking
    setShaking(true);
    const body = document.body;
    body.classList.remove("shake-screen");
    void body.offsetWidth;
    body.classList.add("shake-screen");
    shakeTimeout.current = setTimeout(() => {
      setShaking(false);
      body.classList.remove("shake-screen");
    }, 1000);
  }, [shaking]);

  // X (crack and collapse) handler
  const handleXClick = useCallback((e: React.MouseEvent) => {
    e.preventDefault();
    setShowCracks(true);
    crackTimeout.current = setTimeout(() => {
      setCollapse(true);
      setTimeout(() => {
        setShowCracks(false);
        setCollapse(false);
      }, 1500);
    }, 1000);
  }, []);

  React.useEffect(() => {
    return () => {
      clearTimeout(lavaTimeout.current);
      clearTimeout(shakeTimeout.current);
      clearTimeout(crackTimeout.current);
      document.body.classList.remove("shake-screen");
    };
  }, []);

  return (
    <>
      {/* Lava Flood Overlay */}
      {lavaActive && (
        <div className="lava-flood-overlay lava-active">
          <img src="/lava.jpg" alt="Lava flood" draggable={false} />
        </div>
      )}

      {/* Crack & Collapse Overlay */}
      {(showCracks || collapse) && (
        <div className={`crack-overlay${collapse ? " crack-collapse" : ""}`}>
          <img className="crack crack1" src="/crack1.png" alt="" />
          <img className="crack crack2" src="/crack2.png" alt="" />
          {/* Add more cracks for extra effect if you wish */}
        </div>
      )}

      {/* Footer */}
      <footer className="bg-[#16424b] text-white pt-12 pb-4 px-6 mt-12 z-0">
        <div className="max-w-6xl mx-auto flex flex-col md:flex-row justify-between gap-8">
          {/* Useful Links */}
          <div className="min-w-[200px] mb-8 md:mb-0 flex-1">
            <h3 className="font-bold underline text-lg mb-3">Useful links</h3>
            <ul className="space-y-2">
              <li>
                <Link
                  href="https://www.gov.uk/guidance/extreme-weather-and-natural-hazards"
                  className="hover:underline"
                  target="_blank"
                  rel="noopener noreferrer"
                >
                  Gov.UK guidance
                </Link>
              </li>
              <li>
                <Link
                  href="https://www.dysoninstitute.ac.uk/about-us/governance/privacy-notices/"
                  className="hover:underline"
                >
                  Privacy policy
                </Link>
              </li>
              <li>
                <Link
                  href="https://privacy.dyson.com/en/globalcookiepolicy.aspx"
                  className="hover:underline"
                >
                  Cookies policy
                </Link>
              </li>
            </ul>
          </div>

          {/* Donate Section */}
          <div className="min-w-[220px] mb-8 md:mb-0 flex-1">
            <h3 className="font-bold underline text-lg mb-3">Donate</h3>
            <p className="mb-4">
              We are a nonprofit entirely funded by your donations, every penny helps provide life saving information.
            </p>
            <Link
              href="https://shelterbox.org/"
              className="bg-gray-200 hover:bg-blue-600 hover:text-white text-black font-bold rounded-full px-8 py-2 shadow transition-colors duration-200 inline-block text-center"
            >
              Donate Now
            </Link>
          </div>
        </div>

        {/* Bottom bar */}
        <div className="max-w-6xl mx-auto mt-8 pt-6 flex flex-col md:flex-row items-center justify-between border-t border-gray-200/30">
            <div className="flex flex-row items-center w-full md:w-auto">
                <img
                src="/logo.png"
                alt="TremorTracker logo"
                className="h-16 w-auto mr-4 object-contain"
                style={{ maxHeight: 75 }}
                />
            </div>
          <span className="text-sm flex items-center">
            <span className="mr-2">&#169;</span> TremorTracker 2025
          </span>
          <div className="flex flex-col items-end">
            <span className="text-sm mb-2">Follow us on</span>
            <div className="flex space-x-3">
              <a
                href="#"
                onClick={handleInstagramClick}
                style={{ cursor: "pointer" }}
                aria-label="Instagram Lava Easter egg"
              >
                <img src="instagram.png" alt="instagram" className="h-7 w-7 rounded-full shadow" />
              </a>
              <a
                href="#"
                onClick={handleLinkedInClick}
                style={{ cursor: "pointer" }}
                aria-label="LinkedIn Shake Easter egg"
              >
                <img src="linkedin.png" alt="linkedin" className="h-7 w-7 rounded-full shadow" />
              </a>
              <a
                href="#"
                onClick={handleXClick}
                style={{ cursor: "pointer" }}
                aria-label="X Crack Easter egg"
              >
                <img src="x_logo.jpg" alt="X" className="h-7 w-7 rounded-full shadow" />
              </a>
            </div>
          </div>
        </div>
      </footer>
    </>
  );
}