// Camera.js
// 摄像机系统

class Camera {
  constructor(engine, options = {}) {
    this.engine = engine;
    this.x = options.x || 0;
    this.y = options.y || 0;
    this.width = options.width || engine.width;
    this.height = options.height || engine.height;
    this.zoom = options.zoom || 1;
    this.rotation = options.rotation || 0;
    this.following = null;
    this.followOffset = { x: 0, y: 0 };
    this.bounds = options.bounds || null;
    this.shakeIntensity = 0;
    this.shakeDuration = 0;
    this.shakeTimer = 0;
  }

  // 设置位置
  setPosition(x, y) {
    this.x = x;
    this.y = y;
    this.applyBounds();
    return this;
  }

  // 移动摄像机
  move(x, y) {
    this.x += x;
    this.y += y;
    this.applyBounds();
    return this;
  }

  // 设置缩放
  setZoom(zoom) {
    this.zoom = zoom;
    return this;
  }

  // 设置旋转
  setRotation(rotation) {
    this.rotation = rotation;
    return this;
  }

  // 跟随目标
  follow(target, offset = { x: 0, y: 0 }) {
    this.following = target;
    this.followOffset = offset;
    return this;
  }

  // 停止跟随
  unfollow() {
    this.following = null;
    return this;
  }

  // 设置边界
  setBounds(bounds) {
    this.bounds = bounds;
    this.applyBounds();
    return this;
  }

  // 应用边界限制
  applyBounds() {
    if (this.bounds) {
      const { minX, minY, maxX, maxY } = this.bounds;
      this.x = Math.max(minX, Math.min(maxX, this.x));
      this.y = Math.max(minY, Math.min(maxY, this.y));
    }
  }

  // 屏幕震动
  shake(intensity, duration) {
    this.shakeIntensity = intensity;
    this.shakeDuration = duration;
    this.shakeTimer = duration;
    return this;
  }

  // 更新摄像机
  update(deltaTime) {
    // 更新跟随目标
    if (this.following) {
      this.x = this.following.x - this.width / 2 / this.zoom + this.followOffset.x;
      this.y = this.following.y - this.height / 2 / this.zoom + this.followOffset.y;
      this.applyBounds();
    }

    // 更新震动效果
    if (this.shakeTimer > 0) {
      this.shakeTimer -= deltaTime;
      if (this.shakeTimer <= 0) {
        this.shakeTimer = 0;
        this.shakeIntensity = 0;
      }
    }
  }

  // 应用摄像机变换到舞台
  apply() {
    if (!this.engine.stage) return;

    // 计算变换中心点
    const centerX = this.width / 2;
    const centerY = this.height / 2;

    // 计算震动偏移
    let shakeX = 0;
    let shakeY = 0;
    if (this.shakeIntensity > 0 && this.shakeTimer > 0) {
      shakeX = (Math.random() - 0.5) * this.shakeIntensity * 2;
      shakeY = (Math.random() - 0.5) * this.shakeIntensity * 2;
    }

    // 构造变换字符串
    const translateX = centerX - this.x * this.zoom + shakeX;
    const translateY = centerY - this.y * this.zoom + shakeY;
    
    let transform = `translate(${translateX}px, ${translateY}px)`;
    
    if (this.zoom !== 1) {
      transform += ` scale(${this.zoom})`;
    }
    
    if (this.rotation !== 0) {
      transform += ` rotate(${this.rotation}deg)`;
    }

    // 应用变换
    this.engine.stage.style.transform = transform;
    this.engine.stage.style.transformOrigin = `${centerX}px ${centerY}px`;
  }

  // 世界坐标转屏幕坐标
  worldToScreen(worldX, worldY) {
    return {
      x: (worldX - this.x) * this.zoom + this.width / 2,
      y: (worldY - this.y) * this.zoom + this.height / 2
    };
  }

  // 屏幕坐标转世界坐标
  screenToWorld(screenX, screenY) {
    return {
      x: (screenX - this.width / 2) / this.zoom + this.x,
      y: (screenY - this.height / 2) / this.zoom + this.y
    };
  }

  // 获取摄像机视野范围
  getViewBounds() {
    const halfWidth = (this.width / 2) / this.zoom;
    const halfHeight = (this.height / 2) / this.zoom;
    
    return {
      left: this.x - halfWidth,
      right: this.x + halfWidth,
      top: this.y - halfHeight,
      bottom: this.y + halfHeight,
      width: halfWidth * 2,
      height: halfHeight * 2
    };
  }
}

export default Camera;
export { Camera };