// SimplifiedParticleSystem.js - 简化版粒子系统API
import { Particle, ParticleEmitter, ParticleSystem } from './ParticleSystem.js';

/**
 * 简化版粒子系统类
 * 参考PixiJS、Egret和Vue 3的设计理念
 */
class SimplifiedParticleSystem extends ParticleSystem {
  constructor(x = 0, y = 0, options = {}) {
    super(x, y, options);
    
    // 创建简化API的快捷方法
    this._createSimplifiedAPI();
  }
  
  /**
   * 创建简化API的快捷方法
   */
  _createSimplifiedAPI() {
    // 简化的粒子发射器创建API
    this.emitter = this._createEmitter.bind(this);
    
    // 简化的粒子系统启动API
    this.start = this._start.bind(this);
    
    // 简化的粒子系统停止API
    this.stop = this._stop.bind(this);
    
    // 简化的粒子系统暂停API
    this.pause = this._pause.bind(this);
    
    // 简化的粒子系统恢复API
    this.resume = this._resume.bind(this);
  }
  
  /**
   * 简化的粒子发射器创建API
   * @param {object} options - 发射器选项
   * @returns {ParticleEmitter} 粒子发射器
   */
  _createEmitter(options = {}) {
    const emitter = new ParticleEmitter(
      options.x || 0,
      options.y || 0,
      options.options || {}
    );
    
    this.addEmitter(emitter);
    return emitter;
  }
  
  /**
   * 简化的粒子系统启动API
   * @returns {ParticleSystem} 粒子系统实例
   */
  _start() {
    this.active = true;
    return this;
  }
  
  /**
   * 简化的粒子系统停止API
   * @returns {ParticleSystem} 粒子系统实例
   */
  _stop() {
    this.active = false;
    // 清除所有粒子
    this.particles = [];
    return this;
  }
  
  /**
   * 简化的粒子系统暂停API
   * @returns {ParticleSystem} 粒子系统实例
   */
  _pause() {
    this.paused = true;
    return this;
  }
  
  /**
   * 简化的粒子系统恢复API
   * @returns {ParticleSystem} 粒子系统实例
   */
  _resume() {
    this.paused = false;
    return this;
  }
  
  /**
   * 简化的粒子系统配置API
   * @param {object} config - 配置对象
   * @returns {ParticleSystem} 粒子系统实例
   */
  config(config = {}) {
    // 应用配置
    if (config.gravity !== undefined) this.gravity = config.gravity;
    if (config.wind !== undefined) this.wind = config.wind;
    if (config.maxParticles !== undefined) this.maxParticles = config.maxParticles;
    
    return this;
  }
  
  /**
   * 简化的粒子系统重置API
   * @returns {ParticleSystem} 粒子系统实例
   */
  reset() {
    this.stop();
    this.emitters = [];
    this.particles = [];
    return this.start();
  }
}

/**
 * 简化版粒子发射器类
 */
class SimplifiedParticleEmitter extends ParticleEmitter {
  constructor(x = 0, y = 0, options = {}) {
    super(x, y, options);
    
    // 创建简化API的快捷方法
    this._createSimplifiedAPI();
  }
  
  /**
   * 创建简化API的快捷方法
   */
  _createSimplifiedAPI() {
    // 简化的发射器启动API
    this.start = this._start.bind(this);
    
    // 简化的发射器停止API
    this.stop = this._stop.bind(this);
    
    // 简化的发射器配置API
    this.config = this._config.bind(this);
  }
  
  /**
   * 简化的发射器启动API
   * @returns {ParticleEmitter} 粒子发射器实例
   */
  _start() {
    this.active = true;
    return this;
  }
  
  /**
   * 简化的发射器停止API
   * @returns {ParticleEmitter} 粒子发射器实例
   */
  _stop() {
    this.active = false;
    return this;
  }
  
  /**
   * 简化的发射器配置API
   * @param {object} config - 配置对象
   * @returns {ParticleEmitter} 粒子发射器实例
   */
  _config(config = {}) {
    // 应用配置
    if (config.rate !== undefined) this.rate = config.rate;
    if (config.angle !== undefined) this.angle = config.angle;
    if (config.spread !== undefined) this.spread = config.spread;
    if (config.speed !== undefined) this.speed = config.speed;
    if (config.life !== undefined) this.life = config.life;
    if (config.size !== undefined) this.size = config.size;
    if (config.color !== undefined) this.color = config.color;
    
    return this;
  }
  
  /**
   * 简化的发射器粒子创建API
   * @param {object} options - 粒子选项
   * @returns {Particle} 粒子实例
   */
  createParticle(options = {}) {
    return new Particle(
      this.x + (options.offsetX || 0),
      this.y + (options.offsetY || 0),
      options
    );
  }
}

/**
 * 简化版粒子工厂类
 */
class SimplifiedParticleFactory {
  /**
   * 创建粒子系统
   * @param {object} options - 粒子系统选项
   * @returns {SimplifiedParticleSystem} 粒子系统
   */
  static system(options = {}) {
    return new SimplifiedParticleSystem(
      options.x || 0,
      options.y || 0,
      options
    );
  }
  
  /**
   * 创建粒子发射器
   * @param {object} options - 粒子发射器选项
   * @returns {SimplifiedParticleEmitter} 粒子发射器
   */
  static emitter(options = {}) {
    return new SimplifiedParticleEmitter(
      options.x || 0,
      options.y || 0,
      options
    );
  }
  
  /**
   * 创建粒子
   * @param {object} options - 粒子选项
   * @returns {Particle} 粒子
   */
  static particle(options = {}) {
    return new Particle(
      options.x || 0,
      options.y || 0,
      options
    );
  }
  
  /**
   * 创建预定义的粒子效果
   * @param {string} type - 效果类型 ('explosion', 'fire', 'smoke', 'snow')
   * @param {object} options - 效果选项
   * @returns {SimplifiedParticleSystem} 粒子系统
   */
  static effect(type, options = {}) {
    const system = new SimplifiedParticleSystem(
      options.x || 0,
      options.y || 0,
      options
    );
    
    switch (type.toLowerCase()) {
      case 'explosion':
        const explosionEmitter = system.emitter({
          rate: 100,
          angle: 0,
          spread: Math.PI * 2,
          speed: { min: 50, max: 200 },
          life: { min: 500, max: 1000 },
          size: { min: 2, max: 10 },
          color: '#ff5500'
        });
        explosionEmitter.start();
        // 爆炸效果只发射一次
        setTimeout(() => explosionEmitter.stop(), 100);
        break;
        
      case 'fire':
        system.emitter({
          rate: 50,
          angle: -Math.PI / 2,
          spread: Math.PI / 4,
          speed: { min: 30, max: 100 },
          life: { min: 500, max: 1000 },
          size: { min: 5, max: 15 },
          color: ['#ff5500', '#ffaa00', '#ffff00']
        }).start();
        break;
        
      case 'smoke':
        system.emitter({
          rate: 20,
          angle: -Math.PI / 2,
          spread: Math.PI / 6,
          speed: { min: 20, max: 50 },
          life: { min: 2000, max: 3000 },
          size: { min: 10, max: 30 },
          color: '#888888'
        }).start();
        break;
        
      case 'snow':
        system.config({ gravity: 20 });
        system.emitter({
          rate: 30,
          angle: -Math.PI / 2,
          spread: Math.PI,
          speed: { min: 20, max: 80 },
          life: { min: 3000, max: 5000 },
          size: { min: 3, max: 8 },
          color: '#ffffff'
        }).start();
        break;
    }
    
    return system;
  }
}

// 导出简化版粒子系统类和工厂类
export { SimplifiedParticleSystem, SimplifiedParticleEmitter, SimplifiedParticleFactory };