import { useEffect, useRef } from "react";
import { Link } from "react-router";
import { ArrowRight, Zap, Shield, Clock, TrendingUp, MessageCircle } from "lucide-react";

export default function HeroSection() {
  const canvasRef = useRef<HTMLCanvasElement>(null);

  useEffect(() => {
    const canvas = canvasRef.current;
    if (!canvas) return;
    const ctx = canvas.getContext("2d");
    if (!ctx) return;

    let animationId: number;
    let mouseX = 0;
    let mouseY = 0;
    let isTouch = "ontouchstart" in window;

    const resize = () => {
      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;
    };
    resize();
    window.addEventListener("resize", resize);

    const handleMouse = (e: MouseEvent) => {
      mouseX = e.clientX;
      mouseY = e.clientY;
    };
    if (!isTouch) {
      window.addEventListener("mousemove", handleMouse, { passive: true });
    }

    interface Particle {
      x: number;
      y: number;
      vx: number;
      vy: number;
      radius: number;
      alpha: number;
      hue: number;
    }

    // Fewer particles on mobile for performance
    const PARTICLE_COUNT = isTouch ? 40 : 80;
    const particles: Particle[] = [];

    for (let i = 0; i < PARTICLE_COUNT; i++) {
      particles.push({
        x: Math.random() * canvas.width,
        y: Math.random() * canvas.height,
        vx: (Math.random() - 0.5) * 0.5,
        vy: (Math.random() - 0.5) * 0.5,
        radius: Math.random() * 2 + 1,
        alpha: Math.random() * 0.5 + 0.2,
        hue: 210 + Math.random() * 30,
      });
    }

    const animate = (time: number) => {
      ctx.fillStyle = "rgba(5, 5, 5, 0.08)";
      ctx.fillRect(0, 0, canvas.width, canvas.height);

      particles.forEach((p) => {
        // Mouse attraction (desktop only)
        if (!isTouch) {
          const dx = mouseX - p.x;
          const dy = mouseY - p.y;
          const dist = Math.sqrt(dx * dx + dy * dy);
          if (dist < 200) {
            p.vx += dx * 0.0001;
            p.vy += dy * 0.0001;
          }
        }

        // Flow field
        const angle =
          Math.sin(p.x * 0.005 + time * 0.0005) *
          Math.cos(p.y * 0.005 + time * 0.0003);
        p.vx += Math.cos(angle) * 0.02;
        p.vy += Math.sin(angle) * 0.02;

        p.vx *= 0.98;
        p.vy *= 0.98;
        p.x += p.vx;
        p.y += p.vy;

        // Wrap
        if (p.x < 0) p.x = canvas.width;
        if (p.x > canvas.width) p.x = 0;
        if (p.y < 0) p.y = canvas.height;
        if (p.y > canvas.height) p.y = 0;

        // Draw glow
        const gradient = ctx.createRadialGradient(p.x, p.y, 0, p.x, p.y, p.radius * 6);
        gradient.addColorStop(0, `hsla(${p.hue}, 80%, 60%, ${p.alpha})`);
        gradient.addColorStop(0.4, `hsla(${p.hue}, 60%, 40%, ${p.alpha * 0.3})`);
        gradient.addColorStop(1, "transparent");

        ctx.beginPath();
        ctx.arc(p.x, p.y, p.radius * 6, 0, Math.PI * 2);
        ctx.fillStyle = gradient;
        ctx.fill();

        // Core
        ctx.beginPath();
        ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
        ctx.fillStyle = `hsla(${p.hue}, 90%, 70%, ${p.alpha})`;
        ctx.fill();
      });

      // Connections (fewer on mobile)
      const maxDist = isTouch ? 100 : 150;
      for (let i = 0; i < particles.length; i++) {
        for (let j = i + 1; j < particles.length; j++) {
          const dx = particles[i].x - particles[j].x;
          const dy = particles[i].y - particles[j].y;
          const dist = Math.sqrt(dx * dx + dy * dy);
          if (dist < maxDist) {
            ctx.beginPath();
            ctx.moveTo(particles[i].x, particles[i].y);
            ctx.lineTo(particles[j].x, particles[j].y);
            ctx.strokeStyle = `hsla(220, 60%, 50%, ${0.1 * (1 - dist / maxDist)})`;
            ctx.lineWidth = 0.5;
            ctx.stroke();
          }
        }
      }

      animationId = requestAnimationFrame(animate);
    };

    ctx.fillStyle = "#050505";
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    animationId = requestAnimationFrame(animate);

    return () => {
      cancelAnimationFrame(animationId);
      window.removeEventListener("resize", resize);
      window.removeEventListener("mousemove", handleMouse);
    };
  }, []);

  return (
    <section className="relative min-h-[85vh] sm:min-h-screen flex items-center justify-center overflow-hidden">
      {/* Canvas Background */}
      <canvas ref={canvasRef} className="absolute inset-0 w-full h-full" style={{ zIndex: 0 }} />

      {/* Content */}
      <div className="relative z-10 max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 text-center pt-20 sm:pt-24 lg:pt-28 pb-12 sm:pb-16">
        {/* Badge */}
        <div className="inline-flex items-center gap-1.5 sm:gap-2 px-3 sm:px-4 py-1.5 sm:py-2 rounded-full bg-white/5 border border-white/10 mb-6 sm:mb-8">
          <TrendingUp className="w-3.5 h-3.5 sm:w-4 sm:h-4 text-[#5b9aff]" />
          <span className="text-xs sm:text-sm text-[#a5a5a5]">
            Trusted by 50,000+ happy customers
          </span>
        </div>

        {/* Heading */}
        <h1 className="hero-title text-white mb-4 sm:mb-6 px-2">
          Unlock{" "}
          <span className="text-gradient">Premium Tools</span>
          <br className="hidden sm:block" />
          <span className="sm:hidden"> </span>
          at Unbeatable Prices
        </h1>

        {/* Subtitle */}
        <p className="text-base sm:text-lg lg:text-xl text-[#a5a5a5] max-w-2xl mx-auto mb-8 sm:mb-10 px-4 sm:px-0">
          Get up to <span className="text-[#ff8c42] font-semibold">90% OFF</span> on software, streaming accounts, AI tools, and digital assets. Private accounts with 1-year warranty.
        </p>

        {/* CTAs */}
        <div className="flex flex-col sm:flex-row items-center justify-center gap-3 sm:gap-4 mb-10 sm:mb-16">
          <Link
            to="/shop"
            className="group flex items-center gap-2 px-6 sm:px-8 py-3.5 sm:py-4 bg-[#5b9aff] text-[#050505] font-semibold rounded-full hover:glow-blue-strong transition-all text-sm sm:text-base w-full sm:w-auto justify-center"
          >
            <Zap className="w-4 h-4 sm:w-5 sm:h-5" />
            Explore Deals
            <ArrowRight className="w-4 h-4 sm:w-5 sm:h-5 group-hover:translate-x-1 transition-transform" />
          </Link>
          <a
            href="https://wa.me/917038146526?text=Hi! I want to browse your premium tools catalog."
            target="_blank"
            rel="noopener noreferrer"
            className="flex items-center gap-2 px-6 sm:px-8 py-3.5 sm:py-4 border border-white/20 text-white font-medium rounded-full hover:bg-white/5 transition-all text-sm sm:text-base w-full sm:w-auto justify-center"
          >
            <MessageCircle className="w-4 h-4 sm:w-5 sm:h-5 text-[#25d366]" />
            Order via WhatsApp
          </a>
        </div>

        {/* Trust Badges - responsive grid */}
        <div className="grid grid-cols-3 gap-3 sm:gap-6 max-w-md mx-auto">
          {[
            { icon: Clock, label: "1-2h Delivery" },
            { icon: Shield, label: "1-Year Warranty" },
            { icon: Zap, label: "Private Accounts" },
          ].map((badge) => (
            <div key={badge.label} className="flex flex-col items-center gap-1.5 sm:gap-2">
              <div className="w-10 h-10 sm:w-12 sm:h-12 rounded-xl bg-[#5b9aff]/10 flex items-center justify-center">
                <badge.icon className="w-4 h-4 sm:w-5 sm:h-5 text-[#5b9aff]" />
              </div>
              <span className="text-[10px] sm:text-xs text-[#a5a5a5] text-center leading-tight">{badge.label}</span>
            </div>
          ))}
        </div>
      </div>

      {/* Bottom gradient fade */}
      <div className="absolute bottom-0 left-0 right-0 h-24 sm:h-32 bg-gradient-to-t from-[#050505] to-transparent pointer-events-none" />
    </section>
  );
}
