import Tween from './Tween.js';

class AnimationSystem {
  constructor() {
    this.tweens = [];
    this.groups = new Map(); // 动画组
    this.timelines = []; // 时间轴动画
  }

  // 创建补间动画
  tween(target, duration, easing) {
    const tween = new Tween(target, duration, easing);
    this.tweens.push(tween);
    return tween;
  }

  // 创建动画组
  createGroup(name) {
    const group = {
      name,
      tweens: [],
      paused: false
    };
    this.groups.set(name, group);
    return group;
  }

  // 将动画添加到组
  addToGroup(groupName, tween) {
    const group = this.groups.get(groupName);
    if (group) {
      group.tweens.push(tween);
    }
    return this;
  }

  // 控制组动画
  playGroup(groupName) {
    const group = this.groups.get(groupName);
    if (group) {
      group.paused = false;
      group.tweens.forEach(tween => {
        if (tween.isPaused) {
          tween.resume();
        } else if (!tween.startTime) {
          tween.start();
        }
      });
    }
    return this;
  }

  pauseGroup(groupName) {
    const group = this.groups.get(groupName);
    if (group) {
      group.paused = true;
      group.tweens.forEach(tween => tween.pause());
    }
    return this;
  }

  stopGroup(groupName) {
    const group = this.groups.get(groupName);
    if (group) {
      group.tweens.forEach(tween => tween.stop());
      group.tweens = [];
    }
    return this;
  }

  // 创建时间轴动画
  createTimeline() {
    const timeline = {
      tweens: [],
      currentTime: 0,
      duration: 0,
      playing: false,
      onComplete: null
    };
    this.timelines.push(timeline);
    return timeline;
  }

  // 添加动画到时间轴
  addtoTimeline(timeline, tween, startTime = 0) {
    timeline.tweens.push({
      tween,
      startTime,
      endTime: startTime + tween.duration
    });
    timeline.duration = Math.max(timeline.duration, startTime + tween.duration);
    return this;
  }

  // 播放时间轴
  playTimeline(timeline) {
    timeline.playing = true;
    const startTime = performance.now();
    
    const update = () => {
      if (!timeline.playing) return;
      
      const elapsed = performance.now() - startTime;
      timeline.currentTime = elapsed;
      
      // 更新时间轴中的所有动画
      timeline.tweens.forEach(timelineTween => {
        const { tween, startTime: tweenStart } = timelineTween;
        if (elapsed >= tweenStart && elapsed < timelineTween.endTime) {
          // 计算动画进度并手动更新
          const progress = (elapsed - tweenStart) / tween.duration;
          if (!tween.startTime) {
            // 初始化动画
            tween.startTime = performance.now() - progress * tween.duration;
          }
          // 手动调用更新
          tween.progress = progress;
          // 这里简化处理，实际应该调用tween的更新逻辑
        }
      });
      
      if (elapsed < timeline.duration) {
        requestAnimationFrame(update);
      } else {
        timeline.playing = false;
        if (timeline.onComplete) {
          timeline.onComplete();
        }
      }
    };
    
    update();
    return this;
  }

  // 暂停时间轴
  pauseTimeline(timeline) {
    timeline.playing = false;
    return this;
  }

  // 停止时间轴
  stopTimeline(timeline) {
    timeline.playing = false;
    timeline.currentTime = 0;
    timeline.tweens.forEach(timelineTween => {
      timelineTween.tween.stop();
    });
    return this;
  }

  // 更新所有活动的补间动画
  update() {
    // 过滤已完成的动画
    this.tweens = this.tweens.filter(tween => {
      return tween.startTime !== null && 
             (performance.now() - tween.startTime) < tween.duration;
    });
    
    // 更新组状态
    for (const [name, group] of this.groups) {
      group.tweens = group.tweens.filter(tween => {
        return tween.startTime !== null && 
               (performance.now() - tween.startTime) < tween.duration;
      });
    }
  }

  // 停止特定目标的所有动画
  stop(target) {
    this.tweens = this.tweens.filter(tween => tween.target !== target);
    
    // 从组中移除
    for (const [name, group] of this.groups) {
      group.tweens = group.tweens.filter(tween => tween.target !== target);
    }
    
    return this;
  }

  // 暂停特定目标的所有动画
  pause(target) {
    this.tweens.filter(tween => tween.target === target)
              .forEach(tween => tween.pause());
    return this;
  }

  // 恢复特定目标的所有动画
  resume(target) {
    this.tweens.filter(tween => tween.target === target)
              .forEach(tween => tween.resume());
    return this;
  }

  // 停止所有动画
  stopAll() {
    this.tweens.forEach(tween => tween.stop());
    this.tweens = [];
    
    // 停止所有组动画
    for (const [name, group] of this.groups) {
      group.tweens.forEach(tween => tween.stop());
      group.tweens = [];
    }
    
    // 停止所有时间轴
    this.timelines.forEach(timeline => this.stopTimeline(timeline));
    
    return this;
  }

  // 暂停所有动画
  pauseAll() {
    this.tweens.forEach(tween => tween.pause());
    
    // 暂停所有组动画
    for (const [name, group] of this.groups) {
      group.tweens.forEach(tween => tween.pause());
      group.paused = true;
    }
    
    // 暂停所有时间轴
    this.timelines.forEach(timeline => this.pauseTimeline(timeline));
    
    return this;
  }

  // 恢复所有动画
  resumeAll() {
    this.tweens.forEach(tween => tween.resume());
    
    // 恢复所有组动画
    for (const [name, group] of this.groups) {
      group.tweens.forEach(tween => tween.resume());
      group.paused = false;
    }
    
    // 恢复所有时间轴（需要重新播放）
    this.timelines.forEach(timeline => {
      if (timeline.currentTime < timeline.duration) {
        this.playTimeline(timeline);
      }
    });
    
    return this;
  }
}

export default AnimationSystem;
export { AnimationSystem };