// SimplifiedAnimation.js - 简化版动画API
import Tween from './Tween.js';
import AnimationSystem from './AnimationSystem.js';
import AnimationController from './AnimationController.js';

/**
 * 简化版动画类
 * 参考PixiJS、Egret和Vue 3的设计理念
 */
class SimplifiedAnimation {
  /**
   * 创建补间动画
   * @param {object} target - 动画目标
   * @param {object} props - 动画属性
   * @param {number} duration - 动画持续时间(毫秒)
   * @param {object} options - 动画选项
   * @returns {Tween} 动画对象
   */
  static tween(target, props, duration = 1000, options = {}) {
    const tween = new Tween(target, duration, options.easing || 'linear')
      .to(props);
      
    if (options.onUpdate) {
      tween.onUpdate(options.onUpdate);
    }
    
    if (options.onComplete) {
      tween.onComplete(options.onComplete);
    }
    
    if (options.delay) {
      tween.delay(options.delay);
    }
    
    if (options.repeat) {
      tween.repeat(options.repeat);
    }
    
    if (options.yoyo) {
      tween.yoyo(options.yoyo);
    }
    
    if (options.start !== false) {
      tween.start();
    }
    
    return tween;
  }
  
  /**
   * 创建帧动画
   * @param {object} target - 动画目标
   * @param {array} frames - 帧数组
   * @param {number} frameRate - 帧率
   * @param {object} options - 动画选项
   * @returns {object} 动画对象
   */
  static frame(target, frames, frameRate = 10, options = {}) {
    // 创建帧动画数据
    const animationName = `frame_${Date.now()}`;
    
    // 这里需要访问引擎实例来创建帧动画
    // 在实际使用中，需要将引擎实例传递给这个方法
    if (options.engine) {
      options.engine.createFrameAnimation(animationName, frames, frameRate);
      const animation = options.engine.playFrameAnimation(target, animationName, options.loop || false);
      return animation;
    }
    
    // 如果没有引擎实例，返回一个模拟对象
    return {
      play: () => console.log('Playing frame animation'),
      stop: () => console.log('Stopping frame animation'),
      pause: () => console.log('Pausing frame animation'),
      resume: () => console.log('Resuming frame animation')
    };
  }
  
  /**
   * 创建序列动画
   * @param {array} animations - 动画数组
   * @param {object} options - 动画选项
   * @returns {object} 动画对象
   */
  static sequence(animations, options = {}) {
    // 创建序列动画数据
    const sequenceName = `sequence_${Date.now()}`;
    
    // 这里需要访问引擎实例来创建序列动画
    // 在实际使用中，需要将引擎实例传递给这个方法
    if (options.engine) {
      options.engine.createSequence(sequenceName, animations);
      const sequence = options.engine.playSequence(sequenceName, options.loop || false);
      return sequence;
    }
    
    // 如果没有引擎实例，返回一个模拟对象
    return {
      play: () => console.log('Playing sequence animation'),
      stop: () => console.log('Stopping sequence animation'),
      pause: () => console.log('Pausing sequence animation'),
      resume: () => console.log('Resuming sequence animation')
    };
  }
  
  /**
   * 创建并行动画组
   * @param {array} animations - 动画数组
   * @param {object} options - 动画选项
   * @returns {object} 动画组对象
   */
  static parallel(animations, options = {}) {
    // 同时启动所有动画
    const promises = animations.map(anim => {
      return new Promise(resolve => {
        // 如果是Tween对象
        if (anim instanceof Tween) {
          anim.onComplete(() => resolve());
          if (!anim.isPlaying()) {
            anim.start();
          }
        } 
        // 如果是对象且有play方法
        else if (anim && typeof anim.play === 'function') {
          anim.onComplete = () => resolve();
          anim.play();
        }
        // 其他情况直接resolve
        else {
          resolve();
        }
      });
    });
    
    // 返回一个包含所有动画的组对象
    return {
      play: () => {
        animations.forEach(anim => {
          if (anim instanceof Tween) {
            if (!anim.isPlaying()) {
              anim.start();
            }
          } else if (anim && typeof anim.play === 'function') {
            anim.play();
          }
        });
      },
      stop: () => {
        animations.forEach(anim => {
          if (anim instanceof Tween) {
            anim.stop();
          } else if (anim && typeof anim.stop === 'function') {
            anim.stop();
          }
        });
      },
      pause: () => {
        animations.forEach(anim => {
          if (anim instanceof Tween) {
            anim.pause();
          } else if (anim && typeof anim.pause === 'function') {
            anim.pause();
          }
        });
      },
      resume: () => {
        animations.forEach(anim => {
          if (anim instanceof Tween) {
            anim.resume();
          } else if (anim && typeof anim.resume === 'function') {
            anim.resume();
          }
        });
      },
      onComplete: (callback) => {
        Promise.all(promises).then(() => {
          if (callback) callback();
        });
      }
    };
  }
  
  /**
   * 创建链式动画
   * @param {array} animations - 动画数组
   * @param {object} options - 动画选项
   * @returns {object} 链式动画对象
   */
  static chain(animations, options = {}) {
    let currentIndex = 0;
    
    // 返回一个链式动画对象
    return {
      play: () => {
        const playNext = () => {
          if (currentIndex < animations.length) {
            const anim = animations[currentIndex];
            if (anim instanceof Tween) {
              anim.onComplete(() => {
                currentIndex++;
                playNext();
              });
              if (!anim.isPlaying()) {
                anim.start();
              }
            } else if (anim && typeof anim.play === 'function') {
              anim.onComplete = () => {
                currentIndex++;
                playNext();
              };
              anim.play();
            } else {
              currentIndex++;
              playNext();
            }
          } else {
            // 所有动画完成
            if (options.onComplete) {
              options.onComplete();
            }
          }
        };
        
        playNext();
      },
      stop: () => {
        if (currentIndex < animations.length) {
          const anim = animations[currentIndex];
          if (anim instanceof Tween) {
            anim.stop();
          } else if (anim && typeof anim.stop === 'function') {
            anim.stop();
          }
        }
      },
      pause: () => {
        if (currentIndex < animations.length) {
          const anim = animations[currentIndex];
          if (anim instanceof Tween) {
            anim.pause();
          } else if (anim && typeof anim.pause === 'function') {
            anim.pause();
          }
        }
      },
      resume: () => {
        if (currentIndex < animations.length) {
          const anim = animations[currentIndex];
          if (anim instanceof Tween) {
            anim.resume();
          } else if (anim && typeof anim.resume === 'function') {
            anim.resume();
          }
        }
      }
    };
  }
}

export { SimplifiedAnimation };