import * as THREE from 'three'

export class EventManager {
  private mouse: THREE.Vector2
  private raycaster: THREE.Raycaster
  private mouseWorldPosition: THREE.Vector3
  private camera: THREE.PerspectiveCamera
  private renderer: THREE.WebGLRenderer

  constructor(
    mouse: THREE.Vector2,
    raycaster: THREE.Raycaster,
    mouseWorldPosition: THREE.Vector3,
    camera: THREE.PerspectiveCamera,
    renderer: THREE.WebGLRenderer
  ) {
    this.mouse = mouse
    this.raycaster = raycaster
    this.mouseWorldPosition = mouseWorldPosition
    this.camera = camera
    this.renderer = renderer
  }

  setupEventListeners(): void {
    // Mouse move event
    window.addEventListener('mousemove', (event) => {
      this.mouse.x = (event.clientX / window.innerWidth) * 2 - 1
      this.mouse.y = -(event.clientY / window.innerHeight) * 2 + 1

      // Update raycaster
      this.raycaster.setFromCamera(this.mouse, this.camera)
      
      // Calculate mouse position in world space
      const distance = 10 // Distance from camera
      const direction = this.raycaster.ray.direction.clone()
      direction.multiplyScalar(distance)
      
      // Update the mouse world position by setting its components directly
      this.mouseWorldPosition.copy(this.camera.position).add(direction)
      
      // Debug log to verify mouse interaction is working
      console.log('Mouse world position:', this.mouseWorldPosition.x.toFixed(2), this.mouseWorldPosition.y.toFixed(2), this.mouseWorldPosition.z.toFixed(2))
    })

    // Window resize event
    window.addEventListener('resize', () => {
      this.camera.aspect = window.innerWidth / window.innerHeight
      this.camera.updateProjectionMatrix()
      this.renderer.setSize(window.innerWidth, window.innerHeight)
    })
  }
}
