"use client"

import { useEffect, useRef } from "react"

const SETTINGS = {
  darkOverlay: 0.15,
  effects: {
    smokeAlpha: 0.08,
    particleAlpha: 0.2,
    lightStreakAlpha: 0.4,
    particleCount: 40,
    streakCount: 3,
  }
}

export function AnimatedBackground() {
  const canvasRef = useRef<HTMLCanvasElement>(null)

  useEffect(() => {
    const canvas = canvasRef.current
    if (!canvas) return

    const ctx = canvas.getContext("2d")
    if (!ctx) return

    let animationFrameId: number
    let time = 0

    const resizeCanvas = () => {
      canvas.width = window.innerWidth
      canvas.height = window.innerHeight
    }

    const animate = () => {
      if (!ctx || !canvas) return

      ctx.clearRect(0, 0, canvas.width, canvas.height)

      // Dark base
      ctx.fillStyle = "#030303"
      ctx.fillRect(0, 0, canvas.width, canvas.height)

      // Smoke layers
      for (let layer = 0; layer < 4; layer++) {
        const layerY = canvas.height * (0.2 + layer * 0.2)
        const speed = 0.3 + layer * 0.1
        
        ctx.beginPath()
        ctx.moveTo(-50, layerY + 200)
        
        for (let x = -50; x <= canvas.width + 50; x += 30) {
          const wave1 = Math.sin(x * 0.003 + time * speed) * 80
          const wave2 = Math.sin(x * 0.007 + time * speed * 1.3 + layer) * 40
          const wave3 = Math.cos(x * 0.002 + time * speed * 0.5) * 60
          const y = layerY + wave1 + wave2 + wave3
          
          ctx.lineTo(x, y)
        }
        
        ctx.lineTo(canvas.width + 50, canvas.height + 50)
        ctx.lineTo(-50, canvas.height + 50)
        ctx.closePath()
        
        const gradient = ctx.createLinearGradient(0, layerY - 100, 0, layerY + 300)
        const alpha = SETTINGS.effects.smokeAlpha + layer * 0.015
        gradient.addColorStop(0, `rgba(60, 60, 75, 0)`)
        gradient.addColorStop(0.3, `rgba(50, 50, 65, ${alpha})`)
        gradient.addColorStop(0.7, `rgba(40, 40, 55, ${alpha * 0.9})`)
        gradient.addColorStop(1, `rgba(20, 20, 30, 0)`)
        
        ctx.fillStyle = gradient
        ctx.fill()
      }

      // Floating particles
      for (let i = 0; i < SETTINGS.effects.particleCount; i++) {
        const seed = i * 73.1
        const baseX = (seed * 13.7) % canvas.width
        const baseY = (seed * 17.3) % canvas.height
        
        const x = baseX + Math.sin(time * 0.15 + seed) * 40 + Math.cos(time * 0.1 + seed * 0.5) * 20
        const y = baseY + Math.cos(time * 0.12 + seed) * 30 + Math.sin(time * 0.08 + seed * 0.7) * 15
        
        const alpha = SETTINGS.effects.particleAlpha + Math.sin(time * 0.3 + seed) * 0.1
        const size = 1.2 + Math.sin(time * 0.2 + seed) * 0.6
        
        ctx.beginPath()
        ctx.arc(x, y, size, 0, Math.PI * 2)
        ctx.fillStyle = `rgba(150, 150, 170, ${alpha})`
        ctx.fill()
      }

      // Light streaks
      ctx.globalCompositeOperation = "lighter"
      for (let i = 0; i < SETTINGS.effects.streakCount; i++) {
        const streakX = canvas.width * (0.3 + i * 0.4) + Math.sin(time * 0.1 + i * 3) * 150
        const streakWidth = 400 + Math.sin(time * 0.15 + i) * 100
        
        const streakGradient = ctx.createLinearGradient(
          streakX - streakWidth / 2, 0,
          streakX + streakWidth / 2, 0
        )
        const streakAlpha = SETTINGS.effects.lightStreakAlpha + Math.sin(time * 0.2 + i * 2) * 0.015
        streakGradient.addColorStop(0, "rgba(10, 10, 10, 0)")
        streakGradient.addColorStop(0.5, `rgba(70, 70, 90, ${streakAlpha})`)
        streakGradient.addColorStop(1, "rgba(10, 10, 10, 0)")
        
        ctx.fillStyle = streakGradient
        ctx.fillRect(streakX - streakWidth / 2, 0, streakWidth, canvas.height)
      }
      ctx.globalCompositeOperation = "source-over"

      // Dark overlay
      if (SETTINGS.darkOverlay > 0) {
        ctx.fillStyle = `rgba(0, 0, 0, ${SETTINGS.darkOverlay})`
        ctx.fillRect(0, 0, canvas.width, canvas.height)
      }

      // Vignette effect
      const vignette = ctx.createRadialGradient(
        canvas.width / 2, canvas.height / 2, canvas.width * 0.25,
        canvas.width / 2, canvas.height / 2, canvas.width * 0.85
      )
      vignette.addColorStop(0, "rgba(10, 10, 10, 0)")
      vignette.addColorStop(1, "rgba(0, 0, 0, 0.5)")
      ctx.fillStyle = vignette
      ctx.fillRect(0, 0, canvas.width, canvas.height)

      time += 0.01
      animationFrameId = requestAnimationFrame(animate)
    }

    resizeCanvas()
    animate()

    window.addEventListener("resize", resizeCanvas)

    return () => {
      cancelAnimationFrame(animationFrameId)
      window.removeEventListener("resize", resizeCanvas)
    }
  }, [])

  return (
    <canvas
      ref={canvasRef}
      className="fixed inset-0 pointer-events-none z-0"
      aria-hidden="true"
    />
  )
}
