"use client";
import { useEffect, useRef } from "react";
import Image from "next/image";
import { ArrowLeftRight } from "lucide-react";

import heroImg from "../../_assets/premium-basmati-rice-sack.png";
import priceImg from "../../_assets/pakistan-basmati-export-pricing-chart.png";
import nutritionImg from "../../_assets/basmati-rice-nutrition-facts-infographic.png";
import featuresImg from "../../_assets/pakistani-basmati-key-features-seal.png";

export function InteractiveHero() {
  const heroImageRef = useRef<HTMLDivElement>(null);
  const heroExportImageRef = useRef<HTMLDivElement>(null);
  const heroNutritionImageRef = useRef<HTMLDivElement>(null);
  const heroFeaturesImageRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const handleMouseMove = (e: MouseEvent) => {
      if (
        !heroImageRef.current ||
        !heroExportImageRef.current ||
        !heroNutritionImageRef.current ||
        !heroFeaturesImageRef.current
      )
        return;
      const cursorX = e.clientX;
      const cursorY = e.clientY;

      const rect = heroImageRef.current.getBoundingClientRect();
      const centerX = rect.left + rect.width / 2;
      const centerY = rect.top + rect.height / 2;

      const normalizedX = (cursorX - centerX) / (rect.width / 2);
      const normalizedY = (cursorY - centerY) / (rect.height / 2);

      const moveHeroImgX = Math.max(Math.min(normalizedX * 6, 24), -24);
      const moveHeroImgY = Math.max(Math.min(normalizedY * 6, 24), -24);

      const moveCardX = Math.max(Math.min(normalizedX * 12, 48), -48);
      const moveCardY = Math.max(Math.min(normalizedY * 12, 48), -48);

      heroImageRef.current.style.transform = `
            perspective(1000px)
            translate3d(${moveHeroImgX}px, ${moveHeroImgY}px, 0px)
            `;

      heroExportImageRef.current.style.transform = `
            perspective(1000px)
            translate3d(${moveCardX}px, ${moveCardY}px, 0px)
            `;

      heroNutritionImageRef.current.style.transform = `
            perspective(1000px)
            translate3d(${moveCardX}px, ${moveCardY}px, 0px)
            `;

      heroFeaturesImageRef.current.style.transform = `
            perspective(1000px)
            translate3d(${moveCardX}px, ${moveCardY}px, 0px)
            `;
    };
    window.addEventListener("mousemove", handleMouseMove);

    return () => {
      window.removeEventListener("mousemove", handleMouseMove);
    };
  }, []);

  return (
    <div className="flex lg:w-2/3 items-center justify-end z-20">
      <div className="w-full flex justify-end mr-24">
        <div className="relative w-fit transform-3d perspective-midrange translate-x-4 rotate-x-6 rotate-y-6">
          <div
            ref={heroImageRef}
            className="transition-transform duration-1000 ease-out border-12 w-fit rounded-2xl bg-stone-500 border-stone-500"
          >
            <Image
              src={heroImg}
              alt="Large burlap sack of premium long-grain Pakistani Basmati rice sitting against a clean background"
              loading="eager"
              placeholder="blur"
              priority
              className="rounded-md brightness-95 h-90 lg:h-140 w-auto"
            />
          </div>
          <div
            ref={heroExportImageRef}
            className="absolute -left-16 top-10 lg:-left-30 lg:top-30 border-12 border-stone-500/50 rounded-xl transition-transform duration-1000 ease-out"
          >
            <Image
              alt="Export pricing chart for Pakistani Basmati rice showing consistent market growth and global demand"
              src={priceImg}
              placeholder="blur"
              className="rounded-sm w-40 lg:w-56 h-auto"
            />
          </div>
          <div
            ref={heroNutritionImageRef}
            className="absolute bottom-16 -right-20 lg:bottom-30 lg:-right-24 border-12 border-stone-500/50 rounded-xl transition-transform duration-1000 ease-out"
          >
            <span className="absolute bg-gray-800 border border-gray-600 rounded-full p-3.5 -top-7 -right-7">
              <ArrowLeftRight
                size={22}
                strokeWidth={3}
                className="text-green-500"
              />
            </span>
            <Image
              alt="Infographic showing the nutritional value breakdown of basmati rice including calories and carbohydrates"
              src={nutritionImg}
              placeholder="blur"
              className="rounded-sm w-32 lg:w-48 h-auto"
            />
          </div>
          <div
            ref={heroFeaturesImageRef}
            className="absolute -left-18 bottom-0 border-8 border-stone-500/50 rounded-full transition-transform duration-1000 ease-out"
          >
            <Image
              alt="Gold and green quality seal highlight key features of premium Pakistani Basmati rice"
              src={featuresImg}
              placeholder="blur"
              className="rounded-full w-48 lg:w-64 h-auto"
            />
          </div>
        </div>
      </div>
    </div>
  );
}
