// AnimationController.js
// 高级动画控制器，用于处理复杂的游戏动画

class AnimationController {
  constructor(engine) {
    this.engine = engine;
    this.animations = new Map(); // 存储所有动画
    this.sequences = new Map(); // 存储动画序列
    this.currentSequence = null; // 当前播放的序列
  }

  // 创建帧动画
  createFrameAnimation(name, frames, frameRate = 10) {
    const animation = {
      name,
      frames,
      frameRate,
      currentFrame: 0,
      duration: (frames.length / frameRate) * 1000,
      loop: false,
      onComplete: null,
      onLoop: null
    };
    
    this.animations.set(name, animation);
    return animation;
  }

  // 播放帧动画
  playFrameAnimation(sprite, animationName, loop = false) {
    const animation = this.animations.get(animationName);
    if (!animation) {
      console.warn(`Animation ${animationName} not found`);
      return;
    }
    
    animation.loop = loop;
    animation.currentFrame = 0;
    animation.playing = true;
    
    const updateFrame = () => {
      if (!animation.playing) return;
      
      const frame = animation.frames[animation.currentFrame];
      if (frame && sprite.imgElement) {
        sprite.imgElement.src = frame;
      }
      
      animation.currentFrame++;
      
      if (animation.currentFrame >= animation.frames.length) {
        if (animation.loop) {
          animation.currentFrame = 0;
          if (animation.onLoop) animation.onLoop();
        } else {
          animation.playing = false;
          if (animation.onComplete) animation.onComplete();
          return;
        }
      }
      
      setTimeout(updateFrame, 1000 / animation.frameRate);
    };
    
    updateFrame();
    return animation;
  }

  // 停止帧动画
  stopFrameAnimation(animationName) {
    const animation = this.animations.get(animationName);
    if (animation) {
      animation.playing = false;
    }
    return this;
  }

  // 创建动画序列
  createSequence(name, animations) {
    const sequence = {
      name,
      animations,
      currentIndex: 0,
      loop: false,
      onComplete: null,
      onLoop: null
    };
    
    this.sequences.set(name, sequence);
    return sequence;
  }

  // 播放动画序列
  playSequence(sequenceName, loop = false) {
    const sequence = this.sequences.get(sequenceName);
    if (!sequence) {
      console.warn(`Sequence ${sequenceName} not found`);
      return;
    }
    
    sequence.loop = loop;
    sequence.currentIndex = 0;
    this.currentSequence = sequence;
    
    const playNext = () => {
      if (sequence.currentIndex >= sequence.animations.length) {
        if (sequence.loop) {
          sequence.currentIndex = 0;
          if (sequence.onLoop) sequence.onLoop();
        } else {
          this.currentSequence = null;
          if (sequence.onComplete) sequence.onComplete();
          return;
        }
      }
      
      const animationConfig = sequence.animations[sequence.currentIndex];
      const { sprite, animationName, duration } = animationConfig;
      
      // 播放动画
      this.playFrameAnimation(sprite, animationName);
      
      // 设置下一个动画的播放
      setTimeout(() => {
        sequence.currentIndex++;
        playNext();
      }, duration || 1000);
    };
    
    playNext();
    return sequence;
  }

  // 停止当前序列
  stopSequence() {
    if (this.currentSequence) {
      this.currentSequence.currentIndex = this.currentSequence.animations.length;
      this.currentSequence = null;
    }
    return this;
  }

  // 添加动画完成事件
  onAnimationComplete(animationName, callback) {
    const animation = this.animations.get(animationName);
    if (animation) {
      animation.onComplete = callback;
    }
    return this;
  }

  // 添加序列完成事件
  onSequenceComplete(sequenceName, callback) {
    const sequence = this.sequences.get(sequenceName);
    if (sequence) {
      sequence.onComplete = callback;
    }
    return this;
  }

  // 添加循环事件
  onLoop(animationName, callback) {
    const animation = this.animations.get(animationName);
    if (animation) {
      animation.onLoop = callback;
    }
    return this;
  }
}

export default AnimationController;