/**
 * 背景音乐服务
 */
interface BgmItem {
  id: number;
  name: string;
  url: string;
  mood: string;
  duration: number;
  size?: number;
}

export class BgmService {
  // 背景音乐数据
  private bgmList: BgmItem[] = [
    { id: 1, name: '轻柔钢琴', url: 'https://example.com/bgm/light-piano.mp3', mood: '轻柔', duration: 180, size: 2400000 },
    { id: 2, name: '温暖弦乐', url: 'https://example.com/bgm/warm-strings.mp3', mood: '温暖', duration: 240, size: 3200000 },
    { id: 3, name: '自然雨声', url: 'https://example.com/bgm/rain-nature.mp3', mood: '自然', duration: 300, size: 4000000 },
    { id: 4, name: '咖啡馆氛围', url: 'https://example.com/bgm/cafe-ambient.mp3', mood: '氛围', duration: 200, size: 2600000 },
    { id: 5, name: '企业配乐', url: 'https://example.com/bgm/corporate.mp3', mood: '商务', duration: 150, size: 2000000 },
    { id: 6, name: '欢快节拍', url: 'https://example.com/bgm/upbeat.mp3', mood: '欢快', duration: 180, size: 2400000 },
    { id: 7, name: '抒情慢歌', url: 'https://example.com/bgm/ballad.mp3', mood: '抒情', duration: 220, size: 2900000 },
    { id: 8, name: '古风配乐', url: 'https://example.com/bgm/ancient-chinese.mp3', mood: '古风', duration: 260, size: 3400000 },
    { id: 9, name: '科技感', url: 'https://example.com/bgm/tech.mp3', mood: '科技', duration: 190, size: 2500000 },
    { id: 10, name: '儿童音乐', url: 'https://example.com/bgm/children.mp3', mood: '童趣', duration: 160, size: 2100000 },
  ];

  // 风格分类
  private moods = ['轻柔', '温暖', '自然', '氛围', '商务', '欢快', '抒情', '古风', '科技', '童趣'];

  /**
   * 获取所有背景音乐
   */
  getBgmList(): BgmItem[] {
    return this.bgmList;
  }

  /**
   * 按风格筛选背景音乐
   */
  getBgmByMood(mood: string): BgmItem[] {
    if (!mood) return this.bgmList;
    return this.bgmList.filter(bgm => bgm.mood === mood);
  }

  /**
   * 获取单个背景音乐
   */
  getBgmById(id: number): BgmItem | undefined {
    return this.bgmList.find(bgm => bgm.id === id);
  }

  /**
   * 获取所有风格
   */
  getMoods(): string[] {
    return this.moods;
  }

  /**
   * 添加背景音乐
   */
  addBgm(data: { name: string; url: string; mood: string; duration: number }): BgmItem {
    const newBgm: BgmItem = {
      id: this.bgmList.length + 1,
      ...data,
    };
    this.bgmList.push(newBgm);
    return newBgm;
  }

  /**
   * 更新背景音乐
   */
  updateBgm(id: number, data: Partial<{ name: string; url: string; mood: string; duration: number; size: number }>): BgmItem | null {
    const index = this.bgmList.findIndex(bgm => bgm.id === id);
    if (index === -1) return null;
    this.bgmList[index] = { ...this.bgmList[index], ...data };
    return this.bgmList[index];
  }

  /**
   * 删除背景音乐
   */
  deleteBgm(id: number): boolean {
    const index = this.bgmList.findIndex(bgm => bgm.id === id);
    if (index === -1) return false;
    this.bgmList.splice(index, 1);
    return true;
  }
}

export const bgmService = new BgmService();