// SimplifiedCamera.js - 简化版摄像机API
import Camera from './Camera.js';

/**
 * 简化版摄像机类
 * 参考PixiJS、Egret和Vue 3的设计理念
 */
class SimplifiedCamera extends Camera {
  constructor(engine) {
    super(engine);
    
    // 创建简化API的快捷方法
    this._createSimplifiedAPI();
  }
  
  /**
   * 创建简化API的快捷方法
   */
  _createSimplifiedAPI() {
    // 简化的摄像机移动API
    this.move = this._moveTo.bind(this);
    
    // 简化的摄像机跟随API
    this.follow = this._followTarget.bind(this);
    
    // 简化的摄像机缩放API
    this.zoom = this._setZoom.bind(this);
    
    // 简化的摄像机旋转API
    this.rotate = this._setRotation.bind(this);
    
    // 简化的摄像机震动API
    this.shake = this._shake.bind(this);
  }
  
  /**
   * 简化的摄像机移动API
   * @param {number} x - X坐标
   * @param {number} y - Y坐标
   * @param {number} duration - 移动持续时间(毫秒)
   * @returns {Promise} 移动完成的Promise
   */
  _moveTo(x, y, duration = 0) {
    if (duration > 0) {
      // 使用动画移动摄像机
      const currentX = this.x;
      const currentY = this.y;
      
      return new Promise(resolve => {
        // 这里需要访问引擎的动画系统
        if (this.engine && this.engine.animate) {
          this.engine.animate(
            { x: currentX, y: currentY },
            { x, y },
            duration,
            {
              onUpdate: (progress) => {
                const newX = currentX + (x - currentX) * progress;
                const newY = currentY + (y - currentY) * progress;
                this.setPosition(newX, newY);
              },
              onComplete: resolve
            }
          );
        } else {
          // 如果没有动画系统，直接移动
          this.setPosition(x, y);
          resolve();
        }
      });
    } else {
      // 立即移动摄像机
      this.setPosition(x, y);
      return Promise.resolve();
    }
  }
  
  /**
   * 简化的摄像机跟随API
   * @param {GameObject} target - 跟随目标
   * @param {object} offset - 偏移量
   * @returns {Camera} 摄像机实例
   */
  _followTarget(target, offset = { x: 0, y: 0 }) {
    return this.follow(target, offset);
  }
  
  /**
   * 简化的摄像机缩放API
   * @param {number} zoom - 缩放级别
   * @param {number} duration - 缩放持续时间(毫秒)
   * @returns {Promise} 缩放完成的Promise
   */
  _setZoom(zoom, duration = 0) {
    if (duration > 0) {
      // 使用动画缩放摄像机
      const currentZoom = this.zoom;
      
      return new Promise(resolve => {
        // 这里需要访问引擎的动画系统
        if (this.engine && this.engine.animate) {
          this.engine.animate(
            { zoom: currentZoom },
            { zoom },
            duration,
            {
              onUpdate: (progress) => {
                const newZoom = currentZoom + (zoom - currentZoom) * progress;
                this.setZoom(newZoom);
              },
              onComplete: resolve
            }
          );
        } else {
          // 如果没有动画系统，直接缩放
          this.setZoom(zoom);
          resolve();
        }
      });
    } else {
      // 立即缩放摄像机
      this.setZoom(zoom);
      return Promise.resolve();
    }
  }
  
  /**
   * 简化的摄像机旋转API
   * @param {number} rotation - 旋转角度(弧度)
   * @param {number} duration - 旋转持续时间(毫秒)
   * @returns {Promise} 旋转完成的Promise
   */
  _setRotation(rotation, duration = 0) {
    if (duration > 0) {
      // 使用动画旋转摄像机
      const currentRotation = this.rotation;
      
      return new Promise(resolve => {
        // 这里需要访问引擎的动画系统
        if (this.engine && this.engine.animate) {
          this.engine.animate(
            { rotation: currentRotation },
            { rotation },
            duration,
            {
              onUpdate: (progress) => {
                const newRotation = currentRotation + (rotation - currentRotation) * progress;
                this.setRotation(newRotation);
              },
              onComplete: resolve
            }
          );
        } else {
          // 如果没有动画系统，直接旋转
          this.setRotation(rotation);
          resolve();
        }
      });
    } else {
      // 立即旋转摄像机
      this.setRotation(rotation);
      return Promise.resolve();
    }
  }
  
  /**
   * 简化的摄像机震动API
   * @param {number} intensity - 震动强度
   * @param {number} duration - 震动持续时间(毫秒)
   * @returns {Camera} 摄像机实例
   */
  _shake(intensity, duration) {
    return this.shake(intensity, duration);
  }
  
  /**
   * 简化的摄像机边界设置API
   * @param {object} bounds - 边界对象
   * @returns {Camera} 摄像机实例
   */
  setBounds(bounds) {
    if (bounds) {
      this.minX = bounds.minX || -Infinity;
      this.minY = bounds.minY || -Infinity;
      this.maxX = bounds.maxX || Infinity;
      this.maxY = bounds.maxY || Infinity;
    }
    return this;
  }
  
  /**
   * 简化的摄像机重置API
   * @returns {Camera} 摄像机实例
   */
  reset() {
    this.setPosition(0, 0);
    this.setZoom(1);
    this.setRotation(0);
    this.unfollow();
    return this;
  }
}

// 导出简化版摄像机类
export { SimplifiedCamera };