// Advanced Animation Controller
class AnimationController {
  constructor() {
    this.animations = new Map()
    this.observers = new Map()
    this.init()
  }

  init() {
    this.setupIntersectionObserver()
    this.setupScrollAnimations()
    this.setupHoverAnimations()
    this.setupLoadAnimations()
  }

  setupIntersectionObserver() {
    const options = {
      threshold: [0.1, 0.3, 0.5, 0.7],
      rootMargin: "0px 0px -50px 0px",
    }

    const observer = new IntersectionObserver((entries) => {
      entries.forEach((entry) => {
        if (entry.isIntersecting) {
          this.triggerAnimation(entry.target, entry.intersectionRatio)
        }
      })
    }, options)

    // Observe all animated elements
    document.querySelectorAll("[data-animate]").forEach((el) => {
      observer.observe(el)
    })

    this.observers.set("intersection", observer)
  }

  triggerAnimation(element, ratio) {
    const animationType = element.dataset.animate
    const delay = Number.parseFloat(element.dataset.delay) || 0
    const duration = Number.parseFloat(element.dataset.duration) || 0.8

    setTimeout(() => {
      switch (animationType) {
        case "fadeInUp":
          this.fadeInUp(element, duration)
          break
        case "fadeInLeft":
          this.fadeInLeft(element, duration)
          break
        case "fadeInRight":
          this.fadeInRight(element, duration)
          break
        case "scaleIn":
          this.scaleIn(element, duration)
          break
        case "rotateIn":
          this.rotateIn(element, duration)
          break
        case "slideInUp":
          this.slideInUp(element, duration)
          break
        case "bounceIn":
          this.bounceIn(element, duration)
          break
        case "flipIn":
          this.flipIn(element, duration)
          break
        default:
          element.classList.add("animate")
      }
    }, delay * 1000)
  }

  fadeInUp(element, duration) {
    element.style.transition = `all ${duration}s cubic-bezier(0.4, 0, 0.2, 1)`
    element.style.opacity = "1"
    element.style.transform = "translateY(0)"
    element.classList.add("animate")
  }

  fadeInLeft(element, duration) {
    element.style.transition = `all ${duration}s cubic-bezier(0.4, 0, 0.2, 1)`
    element.style.opacity = "1"
    element.style.transform = "translateX(0)"
    element.classList.add("animate")
  }

  fadeInRight(element, duration) {
    element.style.transition = `all ${duration}s cubic-bezier(0.4, 0, 0.2, 1)`
    element.style.opacity = "1"
    element.style.transform = "translateX(0)"
    element.classList.add("animate")
  }

  scaleIn(element, duration) {
    element.style.transition = `all ${duration}s cubic-bezier(0.4, 0, 0.2, 1)`
    element.style.opacity = "1"
    element.style.transform = "scale(1)"
    element.classList.add("animate")
  }

  rotateIn(element, duration) {
    element.style.transition = `all ${duration}s cubic-bezier(0.4, 0, 0.2, 1)`
    element.style.opacity = "1"
    element.style.transform = "rotate(0deg) scale(1)"
    element.classList.add("animate")
  }

  slideInUp(element, duration) {
    element.style.transition = `all ${duration}s cubic-bezier(0.4, 0, 0.2, 1)`
    element.style.opacity = "1"
    element.style.transform = "translateY(0)"
    element.classList.add("animate")
  }

  bounceIn(element, duration) {
    element.style.transition = `all ${duration}s cubic-bezier(0.68, -0.55, 0.265, 1.55)`
    element.style.opacity = "1"
    element.style.transform = "scale(1)"
    element.classList.add("animate")
  }

  flipIn(element, duration) {
    element.style.transition = `all ${duration}s cubic-bezier(0.4, 0, 0.2, 1)`
    element.style.opacity = "1"
    element.style.transform = "rotateY(0deg)"
    element.classList.add("animate")
  }

  setupScrollAnimations() {
    let ticking = false

    const updateScrollAnimations = () => {
      const scrollY = window.pageYOffset

      // Parallax elements
      document.querySelectorAll("[data-parallax]").forEach((element) => {
        const speed = Number.parseFloat(element.dataset.parallax) || 0.5
        const yPos = -(scrollY * speed)
        element.style.transform = `translateY(${yPos}px)`
      })

      // Progress bars
      document.querySelectorAll("[data-progress]").forEach((element) => {
        const rect = element.getBoundingClientRect()
        const progress = Math.max(0, Math.min(1, (window.innerHeight - rect.top) / window.innerHeight))
        element.style.setProperty("--progress", progress)
      })

      ticking = false
    }

    const requestScrollUpdate = () => {
      if (!ticking) {
        requestAnimationFrame(updateScrollAnimations)
        ticking = true
      }
    }

    window.addEventListener("scroll", requestScrollUpdate, { passive: true })
  }

  setupHoverAnimations() {
    // 3D Tilt Effect
    document.querySelectorAll("[data-tilt]").forEach((element) => {
      element.addEventListener("mousemove", (e) => {
        const rect = element.getBoundingClientRect()
        const x = e.clientX - rect.left
        const y = e.clientY - rect.top

        const centerX = rect.width / 2
        const centerY = rect.height / 2

        const rotateX = ((y - centerY) / centerY) * 15
        const rotateY = ((centerX - x) / centerX) * 15

        element.style.transform = `perspective(1000px) rotateX(${rotateX}deg) rotateY(${rotateY}deg) scale(1.05)`
      })

      element.addEventListener("mouseleave", () => {
        element.style.transform = "perspective(1000px) rotateX(0deg) rotateY(0deg) scale(1)"
      })
    })

    // Magnetic Effect
    document.querySelectorAll("[data-magnetic]").forEach((element) => {
      element.addEventListener("mousemove", (e) => {
        const rect = element.getBoundingClientRect()
        const x = e.clientX - rect.left - rect.width / 2
        const y = e.clientY - rect.top - rect.height / 2

        const strength = Number.parseFloat(element.dataset.magnetic) || 0.3

        element.style.transform = `translate(${x * strength}px, ${y * strength}px)`
      })

      element.addEventListener("mouseleave", () => {
        element.style.transform = "translate(0px, 0px)"
      })
    })

    // Glow Effect
    document.querySelectorAll("[data-glow]").forEach((element) => {
      element.addEventListener("mouseenter", () => {
        const color = element.dataset.glow || "rgba(207, 174, 109, 0.5)"
        element.style.boxShadow = `0 0 30px ${color}`
      })

      element.addEventListener("mouseleave", () => {
        element.style.boxShadow = ""
      })
    })
  }

  setupLoadAnimations() {
    // Stagger animations for lists
    document.querySelectorAll("[data-stagger]").forEach((container) => {
      const children = container.children
      const delay = Number.parseFloat(container.dataset.stagger) || 0.1

      Array.from(children).forEach((child, index) => {
        child.style.animationDelay = `${index * delay}s`
        child.classList.add("stagger-animate")
      })
    })

    // Text reveal animations
    document.querySelectorAll("[data-text-reveal]").forEach((element) => {
      const text = element.textContent
      element.innerHTML = ""

      text.split("").forEach((char, index) => {
        const span = document.createElement("span")
        span.textContent = char === " " ? "\u00A0" : char
        span.style.animationDelay = `${index * 0.05}s`
        span.classList.add("char-reveal")
        element.appendChild(span)
      })
    })
  }

  // Counter animation with easing
  animateCounter(element, start = 0, end, duration = 2000, easing = "easeOutQuart") {
    const startTime = performance.now()
    const easingFunctions = {
      easeOutQuart: (t) => 1 - --t * t * t * t,
      easeInOutCubic: (t) => (t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1),
      easeOutBounce: (t) => {
        if (t < 1 / 2.75) return 7.5625 * t * t
        if (t < 2 / 2.75) return 7.5625 * (t -= 1.5 / 2.75) * t + 0.75
        if (t < 2.5 / 2.75) return 7.5625 * (t -= 2.25 / 2.75) * t + 0.9375
        return 7.5625 * (t -= 2.625 / 2.75) * t + 0.984375
      },
    }

    const animate = (currentTime) => {
      const elapsed = currentTime - startTime
      const progress = Math.min(elapsed / duration, 1)
      const easedProgress = easingFunctions[easing](progress)

      const current = start + (end - start) * easedProgress
      element.textContent = Math.floor(current)

      if (progress < 1) {
        requestAnimationFrame(animate)
      } else {
        element.textContent = end
      }
    }

    requestAnimationFrame(animate)
  }

  // Morphing shapes
  morphShape(element, paths, duration = 1000) {
    const svg = element.querySelector("svg path")
    if (!svg) return

    let currentIndex = 0
    const morph = () => {
      const nextIndex = (currentIndex + 1) % paths.length
      const currentPath = paths[currentIndex]
      const nextPath = paths[nextIndex]

      svg.style.transition = `d ${duration}ms ease-in-out`
      svg.setAttribute("d", nextPath)

      currentIndex = nextIndex
      setTimeout(morph, duration + 100)
    }

    morph()
  }

  // Particle system
  createParticleSystem(container, options = {}) {
    const defaults = {
      count: 50,
      color: "#CFAE6D",
      size: 4,
      speed: 2,
      direction: "up",
    }

    const config = { ...defaults, ...options }

    for (let i = 0; i < config.count; i++) {
      const particle = document.createElement("div")
      particle.className = "particle"
      particle.style.cssText = `
                position: absolute;
                width: ${config.size}px;
                height: ${config.size}px;
                background: ${config.color};
                border-radius: 50%;
                pointer-events: none;
                left: ${Math.random() * 100}%;
                animation: particle-${config.direction} ${Math.random() * 3 + 5}s linear infinite;
                animation-delay: ${Math.random() * 5}s;
            `
      container.appendChild(particle)
    }
  }

  // Cleanup method
  destroy() {
    this.observers.forEach((observer) => observer.disconnect())
    this.animations.clear()
    this.observers.clear()
  }
}

// Initialize animation controller
const animationController = new AnimationController()

// Smooth scroll implementation
class SmoothScroll {
  constructor() {
    this.current = 0
    this.target = 0
    this.ease = 0.1
    this.init()
  }

  init() {
    document.body.style.height = document.body.scrollHeight + "px"
    this.update()
    this.addEventListeners()
  }

  addEventListeners() {
    window.addEventListener("scroll", () => {
      this.target = window.scrollY
    })
  }

  update() {
    this.current += (this.target - this.current) * this.ease

    if (Math.abs(this.target - this.current) < 0.1) {
      this.current = this.target
    }

    document.body.style.transform = `translateY(-${this.current}px)`
    requestAnimationFrame(() => this.update())
  }
}

// Initialize smooth scroll (optional - can be enabled/disabled)
// const smoothScroll = new SmoothScroll();

// Advanced cursor effect
class CustomCursor {
  constructor() {
    this.cursor = null
    this.follower = null
    this.init()
  }

  init() {
    this.createCursor()
    this.addEventListeners()
  }

  createCursor() {
    this.cursor = document.createElement("div")
    this.cursor.className = "custom-cursor"
    this.cursor.style.cssText = `
            position: fixed;
            width: 10px;
            height: 10px;
            background: var(--accent-color);
            border-radius: 50%;
            pointer-events: none;
            z-index: 9999;
            transition: transform 0.1s ease;
        `

    this.follower = document.createElement("div")
    this.follower.className = "cursor-follower"
    this.follower.style.cssText = `
            position: fixed;
            width: 40px;
            height: 40px;
            border: 2px solid var(--accent-color);
            border-radius: 50%;
            pointer-events: none;
            z-index: 9998;
            transition: all 0.3s ease;
            opacity: 0.5;
        `

    document.body.appendChild(this.cursor)
    document.body.appendChild(this.follower)
  }

  addEventListeners() {
    document.addEventListener("mousemove", (e) => {
      this.cursor.style.left = e.clientX - 5 + "px"
      this.cursor.style.top = e.clientY - 5 + "px"

      this.follower.style.left = e.clientX - 20 + "px"
      this.follower.style.top = e.clientY - 20 + "px"
    })

    document.addEventListener("mouseenter", () => {
      this.cursor.style.opacity = "1"
      this.follower.style.opacity = "0.5"
    })

    document.addEventListener("mouseleave", () => {
      this.cursor.style.opacity = "0"
      this.follower.style.opacity = "0"
    })

    // Hover effects
    document.querySelectorAll("a, button, .card").forEach((element) => {
      element.addEventListener("mouseenter", () => {
        this.cursor.style.transform = "scale(1.5)"
        this.follower.style.transform = "scale(1.5)"
      })

      element.addEventListener("mouseleave", () => {
        this.cursor.style.transform = "scale(1)"
        this.follower.style.transform = "scale(1)"
      })
    })
  }
}

// Initialize custom cursor (optional)
// new CustomCursor();

// Export for use in other files

if (typeof module !== "undefined" && module.exports) {
  module.exports = {
    AnimationController,
    SmoothScroll,
    CustomCursor,
  }
}
