import VoiceResponse from 'twilio/lib/twiml/VoiceResponse';
import { InfoResponse } from '../../../doorman-api/src/functions/api/door/info';

export function doorOpenTwiml(config: InfoResponse): VoiceResponse {
  const twiml = new Twilio.twiml.VoiceResponse();

  // play audio
  twiml.play('https://buzzer-2439-prod.twil.io/buzzing_up_boosted.mp3');

  // press digit
  twiml.play({ digits: config.pressKey }); // configured in doorman what button to click and passed into this function

  // pause
  twiml.pause({ length: 1 });

  // exit
  twiml.hangup();

  // @ts-ignore
  return twiml;
}

export function dialFallbackTwiml(config: InfoResponse): VoiceResponse {
  const twiml = new Twilio.twiml.VoiceResponse();

  let dial = twiml.dial({
    timeLimit: 20,
    timeout: 20
  });

  config.fallbackNumbers.forEach(number => {
    dial.number(number);
  });

  // @ts-ignore
  return twiml;
}

export function doorOnboardTwiml(otp: string): VoiceResponse {
  const twiml = new Twilio.twiml.VoiceResponse();
  const prefix = 'https://buzzer-2439-prod.twil.io/';

  // play intro
  twiml.play(prefix + 'onboarding-intro.mp3');

  twiml.pause({ length: 3 });

  // play digits audio
  otp.split('').forEach(digit => {
    twiml.play(prefix + digit + ".mp3");
  });

  twiml.pause({ length: 3 });

  // play repeat
  twiml.play(prefix + 'onboarding-repeat.mp3');

  twiml.pause({ length: 3 });

  // play digits audio
  otp.split('').forEach(digit => {
    twiml.play(prefix + digit + ".mp3");
  });

  twiml.pause({ length: 3 });

  // @ts-ignore
  return twiml;
}
