// Input manager for keyboard and mouse / 键盘和鼠标输入管理器

/**
 * Tracks keyboard and mouse state for player input / 追踪键盘和鼠标状态用于玩家输入
 */
export class InputManager {
  constructor() {
    // Currently pressed keys / 当前按下的按键
    this.keys = {}
    // Mouse state / 鼠标状态
    this.mouse = { x: 0, y: 0, left: false, right: false, groundX: 0, groundY: 0 }

    // Key bindings for movement / 移动按键绑定
    this.keyBindings = {
      moveUp: 'KeyW',
      moveDown: 'KeyS',
      moveLeft: 'KeyA',
      moveRight: 'KeyD'
    }

    // Input sequence number for reconciliation / 用于同步的输入序列号
    this.sequenceNumber = 0

    // Event handler bound references (for attach/detach) / 事件处理器绑定引用（用于挂载/卸载）
    this._onKeyDown = (e) => {
      this.keys[e.code] = true
    }
    this._onKeyUp = (e) => {
      this.keys[e.code] = false
    }
    this._onMouseMove = (e) => {
      this.mouse.x = e.clientX
      this.mouse.y = e.clientY
    }
    this._onMouseDown = (e) => {
      if (e.button === 0) this.mouse.left = true
      if (e.button === 2) this.mouse.right = true
    }
    this._onMouseUp = (e) => {
      if (e.button === 0) this.mouse.left = false
      if (e.button === 2) this.mouse.right = false
    }
    // Prevent context menu on right click / 阻止右键菜单
    this._onContextMenu = (e) => e.preventDefault()
  }

  // Attach event listeners / 挂载事件监听器
  attach() {
    window.addEventListener('keydown', this._onKeyDown)
    window.addEventListener('keyup', this._onKeyUp)
    window.addEventListener('mousemove', this._onMouseMove)
    window.addEventListener('mousedown', this._onMouseDown)
    window.addEventListener('mouseup', this._onMouseUp)
    window.addEventListener('contextmenu', this._onContextMenu)
  }

  // Remove event listeners / 移除事件监听器
  detach() {
    window.removeEventListener('keydown', this._onKeyDown)
    window.removeEventListener('keyup', this._onKeyUp)
    window.removeEventListener('mousemove', this._onMouseMove)
    window.removeEventListener('mousedown', this._onMouseDown)
    window.removeEventListener('mouseup', this._onMouseUp)
    window.removeEventListener('contextmenu', this._onContextMenu)
  }

  // Get normalized movement direction / 获取归一化的移动方向
  getMovement() {
    let dx = 0, dy = 0
    if (this.keys[this.keyBindings.moveUp] || this.keys['ArrowUp']) dy -= 1
    if (this.keys[this.keyBindings.moveDown] || this.keys['ArrowDown']) dy += 1
    if (this.keys[this.keyBindings.moveLeft] || this.keys['ArrowLeft']) dx -= 1
    if (this.keys[this.keyBindings.moveRight] || this.keys['ArrowRight']) dx += 1
    // Normalize diagonal movement / 对角线移动归一化
    if (dx !== 0 && dy !== 0) {
      dx *= 0.7071
      dy *= 0.7071
    }
    return { dx, dy }
  }

  // Build complete input state for sending to server / 构建完整的输入状态用于发送给服务器
  buildInputState(mouseGroundPos) {
    const movement = this.getMovement()
    this.sequenceNumber++
    return {
      seq: this.sequenceNumber,
      dx: movement.dx,
      dy: movement.dy,
      aimX: mouseGroundPos.x,
      aimY: mouseGroundPos.y
    }
  }
}
