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

@Injectable({
  providedIn: 'root'
})
export class AlarmSoundService {
  private alertInterval: any;
  private alertDuration: number = 30000;
  private audio= new Audio();
  constructor() {
    this.audio.src = 'assets/sound/alarm2.mp3';
    this.audio.load();
  }

  playSound(): void {
    this.audio.play().catch(error => {
      console.error('Error playing audio:', error);
    });
  }

  stopSound(): void {
    if (this.audio) {
      this.audio.pause();
      this.audio.currentTime = 0;
    }
  }

  startAlarm(state5: boolean, isReady: boolean, state1: boolean, state2: boolean): void {
    if (state5 && isReady && (state1 || state2)) {
      this.playSound();
      this.alertInterval = setInterval(() => {
          this.stopAlarm()
      }, this.alertDuration);
    }
  }

  stopAlarm(): void {
    this.stopSound()
    clearInterval(this.alertInterval);
  }

  simulateClick(element: HTMLElement) {
    element.click();
  }

}
