#include <XT_DAC_Audio.h>

#include "alarm.h"
#include "soc/rtc_io_reg.h"
#include "alarmSound.h"
#include "silentSound.h"

XT_Wav_Class SirenSound(alarmSound);
XT_Wav_Class SilentSound(silentSound);
XT_DAC_Audio_Class DacAudio(25, 0);

class Siren
{
private:
  AlarmStatus* status;
  

  void playSound(){
    DacAudio.FillBuffer();           // Fill the sound buffer with data
    if (SirenSound.Playing == false) // if not playing,
      DacAudio.Play(&SirenSound);    // play it, this will cause it to repeat and repeat...
  }

  void playSilentSound(){
    DacAudio.FillBuffer();           // Fill the sound buffer with data
    if (SilentSound.Playing == false) // if not playing,
      DacAudio.Play(&SilentSound);    // play it, this will cause it to repeat and repeat...
  }



public:
  Siren(AlarmStatus *statusRef)
  {
    status = statusRef;
  }

  /**
  * Only use DAC enabled pinout (25 or 26)
  */
  Siren(AlarmStatus *statusRef, int pin)
  {
    status = statusRef;
    DacAudio = XT_DAC_Audio_Class(pin, 0);
  }

  void Init()
  {

  }

  void SoundSiren()
  {
    if (status->muted)
      return;

    if(status->silent){
      playSilentSound();
      return;
    }

    playSound();
    // delay(500);

  }

};

// float sinVal;
//   int toneVal;
// void playSound()
// {
//   int max = 180;
//   Serial.println("Sound on " + String(sirenPin));

//   if (status->silent)
//     max = 2;
//   for (int x = 0; x < max; x++)
//   {
//     // convert angle of sinusoidal to radian measure
//     sinVal = (sin(x * (3.1412 / 180)));
//     // generate sound of different frequencies by sinusoidal value
//     toneVal = 2000 + (int(sinVal * 1000));
//     // Set a frequency for Pin-out 8

//     // ledcAttach(sirenPin, toneVal, 3);
//     tone(sirenPin, toneVal, 3);
//     // buzzer->tone(toneVal, 3);
//     // TimerFreeTone(TONE_PIN, toneVal, 2, 5);
//     delay(2);
//   }
//   noTone(sirenPin);
//   // buzzer->noTone();
//   // ledcWrite(sirenPin,0); // No sound
//   // ledcDetach(sirenPin);
// }