import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class AudioService {
  private audioCache = new Map<string, HTMLAudioElement>();

  private getAudio(soundName: string): HTMLAudioElement {
    if (this.audioCache.has(soundName)) {
      return this.audioCache.get(soundName)!;
    }

    const audio = new Audio(`/sounds/${soundName}`);
    this.audioCache.set(soundName, audio);
    return audio;
  }

  playBetSound(): void {
    const audio = this.getAudio('bet.mp3');
    audio.currentTime = 0;
    audio.play().catch((error) => console.error('Error playing bet sound:', error));
  }

  playWinSound(): void {
    const audio = this.getAudio('win.mp3');
    audio.currentTime = 0;
    audio.play().catch((error) => console.error('Error playing win sound:', error));
  }

  getDragSound(): HTMLAudioElement {
    return this.getAudio('drag.mp3');
  }

  playDragStepSound(): void {
    const audio = this.getAudio('drag.mp3');
    audio.currentTime = 0;
    audio.volume = 0.5;
    audio.play().catch((error) => console.error('Error playing drag step sound:', error));
  }
}
