// Component.js
// 组件基类

class Component {
  constructor() {
    this.gameObject = null;
  }

  // 当组件被添加到游戏对象时调用
  onAdd(gameObject) {
    this.gameObject = gameObject;
  }

  // 当组件从游戏对象中移除时调用
  onRemove(gameObject) {
    this.gameObject = null;
  }

  // 更新方法（子类重写）
  update(deltaTime) {
    // 子类实现具体逻辑
  }

  // 销毁方法（子类重写）
  onDestroy(gameObject) {
    // 子类实现清理逻辑
  }
}

// 物理组件
class PhysicsComponent extends Component {
  constructor(options = {}) {
    super();
    this.velocityX = options.velocityX || 0;
    this.velocityY = options.velocityY || 0;
    this.accelerationX = options.accelerationX || 0;
    this.accelerationY = options.accelerationY || 0;
    this.gravity = options.gravity || 0;
    this.friction = options.friction || 0;
  }

  update(deltaTime) {
    if (!this.gameObject) return;
    
    const dt = deltaTime / 1000; // 转换为秒
    
    // 应用加速度
    this.velocityX += this.accelerationX * dt;
    this.velocityY += (this.accelerationY + this.gravity) * dt;
    
    // 应用摩擦力
    this.velocityX *= (1 - this.friction);
    this.velocityY *= (1 - this.friction);
    
    // 更新位置
    this.gameObject.x += this.velocityX * dt;
    this.gameObject.y += this.velocityY * dt;
  }
}

// 碰撞组件
class ColliderComponent extends Component {
  constructor(options = {}) {
    super();
    this.width = options.width || 0;
    this.height = options.height || 0;
    this.offsetX = options.offsetX || 0;
    this.offsetY = options.offsetY || 0;
    this.onCollision = options.onCollision || null;
  }

  // 检查与其他碰撞器的碰撞
  checkCollision(other) {
    if (!this.gameObject || !other.gameObject) return false;
    
    const thisX = this.gameObject.x + this.offsetX;
    const thisY = this.gameObject.y + this.offsetY;
    const otherX = other.gameObject.x + other.offsetX;
    const otherY = other.gameObject.y + other.offsetY;
    
    return thisX < otherX + (other.width || other.gameObject.width) &&
           thisX + (this.width || this.gameObject.width) > otherX &&
           thisY < otherY + (other.height || other.gameObject.height) &&
           thisY + (this.height || this.gameObject.height) > otherY;
  }

  // 获取碰撞器的边界
  getBounds() {
    if (!this.gameObject) return { x: 0, y: 0, width: 0, height: 0 };
    
    return {
      x: this.gameObject.x + this.offsetX,
      y: this.gameObject.y + this.offsetY,
      width: this.width || this.gameObject.width,
      height: this.height || this.gameObject.height
    };
  }
}

// 动画组件
class AnimationComponent extends Component {
  constructor(options = {}) {
    super();
    this.animations = new Map();
    this.currentAnimation = null;
    this.isPlaying = false;
    this.loop = options.loop || false;
  }

  // 添加动画
  addAnimation(name, frames, frameRate = 10) {
    this.animations.set(name, {
      name,
      frames,
      frameRate,
      currentFrame: 0,
      duration: (frames.length / frameRate) * 1000
    });
    return this;
  }

  // 播放动画
  play(name, loop = false) {
    if (this.animations.has(name)) {
      this.currentAnimation = this.animations.get(name);
      this.currentAnimation.currentFrame = 0;
      this.isPlaying = true;
      this.loop = loop;
    }
    return this;
  }

  // 停止动画
  stop() {
    this.isPlaying = false;
    this.currentAnimation = null;
    return this;
  }

  update(deltaTime) {
    if (!this.isPlaying || !this.currentAnimation || !this.gameObject) return;
    
    const animation = this.currentAnimation;
    const frameTime = 1000 / animation.frameRate;
    
    // 更新帧
    animation.elapsed = (animation.elapsed || 0) + deltaTime;
    
    if (animation.elapsed >= frameTime) {
      animation.currentFrame++;
      animation.elapsed = 0;
      
      if (animation.currentFrame >= animation.frames.length) {
        if (this.loop) {
          animation.currentFrame = 0;
        } else {
          this.isPlaying = false;
          return;
        }
      }
      
      // 更新精灵图像
      if (this.gameObject.imgElement) {
        this.gameObject.imgElement.src = animation.frames[animation.currentFrame];
      }
    }
  }
}

// 输入组件
class InputComponent extends Component {
  constructor(options = {}) {
    super();
    this.keys = options.keys || {};
    this.onKeyPress = options.onKeyPress || null;
    this.onKeyRelease = options.onKeyRelease || null;
  }

  update(deltaTime) {
    if (!this.gameObject || !this.gameObject.engine) return;
    
    // 检查按键状态
    for (const key in this.keys) {
      const action = this.keys[key];
      const isPressed = this.gameObject.engine.input.keyboard[key];
      
      if (isPressed && this.onKeyPress) {
        this.onKeyPress(key, action, this.gameObject);
      }
    }
  }
}

// 生命周期组件
class LifecycleComponent extends Component {
  constructor(options = {}) {
    super();
    this.onStart = options.onStart || null;
    this.onUpdate = options.onUpdate || null;
    this.onDestroy = options.onDestroy || null;
    this.started = false;
  }

  onAdd(gameObject) {
    super.onAdd(gameObject);
    if (this.onStart) {
      this.onStart(gameObject);
    }
    this.started = true;
  }

  update(deltaTime) {
    if (this.onUpdate && this.gameObject) {
      this.onUpdate(this.gameObject, deltaTime);
    }
  }

  onDestroy(gameObject) {
    if (this.onDestroy) {
      this.onDestroy(gameObject);
    }
  }
}

export default Component;
export {
  Component,
  PhysicsComponent,
  ColliderComponent,
  AnimationComponent,
  InputComponent,
  LifecycleComponent
};