;Version("3.3.1255.1")
;InformationalVersion("3.3.1255.0")

[nixxis-queue-v3]

exten => queue,1,NoOp(Getting a call to put on hold for activity ${SIP_HEADER(X-cr-act)})

 same => n,Set(ACCOUNTCODE=${SIP_HEADER(x-cr-acc)})
 same => n,Set(ACTIVITY=${SIP_HEADER(x-cr-act)})
 same => n,Set(LAN=${SIP_HEADER(x-cr-lan)})
 same => n,Set(CHANNEL(language)=${SIP_HEADER(x-cr-lan)})

; Estimated Waiting Time value
 same => n,Set(EWT=${SIP_HEADER(x-cr-ewt)})

; EWT transmission frequency 
 same => n,Set(REW=${SIP_HEADER(x-cr-rew)})

; Postion in queue
 same => n,Set(POS=${SIP_HEADER(x-cr-pos)})

; Position transmission frequency 
 same => n,Set(RPO=${SIP_HEADER(x-cr-rpo)})

; Music loop length
 same => n,Set(HMT=${SIP_HEADER(x-cr-hmt)})

; MOH class
 same => n,Set(MOH=${SIP_HEADER(x-cr-moh)})

; Overflow prompting starting loop
 same => n,Set(LQS=${SIP_HEADER(x-cr-lqs)})

; Overflow prompt
 same => n,Set(LQP=${SIP_HEADER(x-cr-lqp)})

 same => n,GotoIf($["${SIP_HEADER(x-cr-iex)}" = "http"]?ivrInfoHttp:ivrInfoDone)

 same => n(ivrInfoHttp),Set(NixxisAppServerUri=${SIP_HEADER(x-cr-hsr)})
 same => n,Set(CURLOPT(httptimeout)=3)
 same => n,Set(RequestUri=${NixxisAppServerUri}/sip?action=getIvrInfo&sipid=${SIPCALLID}&version=${CUT(NixxisFileVersion,\",2)})
 same => n,Set(RawAnswer=${CURL(${RequestUri})})
 same => n,GoSub(ParseIvrInfo(${RawAnswer}))

 same => n(ivrInfoDone),Set(PLAYING=0)
 same => n,Set(LOOPCOUNT=1)

 same => n,NoOp(Check if we have all VARs if not send Text and Busy)
 same => n,GotoIf($["${ACTIVITY}" = "" ]?failed)
 same => n,GotoIf($["${HMT}" = "" ]?NoHmt:CheckMOH)
 same => n(NoHmt),Set(HMT=25)
 same => n(CheckMOH),GotoIf($["${MOH}" = "" ]?NoMOH:Answer)
 same => n(NoMOH),Set(MOH=${ACTIVITY})


 same => n(Answer),Answer
 same => n,Set(TIMEOUT(absolute)=14400)
 same => n,Set(CHANNEL(musicclass)=${MOH})

; POS
 same => n(loop),GotoIf($["${POS}" < "0" ]?EWT)
 same => n,GotoIf($["${RPO}" = "1" & "${LOOPCOUNT}" = "1" ]?SayPos)
 same => n,GotoIf($["${RPO}" = "2" & "${LASTPOS}" != "${POS}" ]?SayPos)
 same => n,GotoIf($["${RPO}" = "3" ]?SayPos)
 same => n,GoTo(EWT)
 

 same => n(SayPos),Set(LASTPOS=${POS})
 same => n,GotoIf($["${POS}" < "1" ]?SayLast)
 same => n,Playback(queue-thereare)
 same => n,Saynumber(${POS})
 same => n,Playback(queue-callswaiting)
 same => n,GoTo(EndSayPos)

 same => n(SayLast),Playback(queue-youarenext)
 
 same => n(EndSayPos),Set(PLAYING=0)
 

; EWT
 same => n(EWT),GotoIf($["${EWT}" = "" ]?LQA)
 same => n,GotoIf($["${REW}" = "1" & "${LOOPCOUNT}" = "1" ]?SayEWT)
 same => n,GotoIf($["${REW}" = "2" & "${LASTEWT}" != "${EWT}" ]?SayEWT)
 same => n,GotoIf($["${REW}" = "3" ]?SayEWT)
 same => n,GoTo(LQA)

 same => n(SayEWT),Set(LASTEWT=${EWT})
 same => n,SET(EWTMIN=${MATH(${EWT}/60,i)})
 same => n,Playback(queue-reporthold)
 same => n,GotoIf($["${EWTMIN}" > "0" ]?min)
 same => n,Saynumber(${EWT})
 same => n,Playback(queue-seconds) 
 same => n,Goto(EndSayEWT)

 same => n(min),Saynumber(${EWTMIN})
 same => n,Playback(queue-minutes)
 
 same => n(EndSayEWT),Set(Playing=0)
 

; Leave Queue Announcement
 same => n(LQA),GotoIf($["${LQP}" = "" ]?music)
 same => n,NoOp(LAQ Start:${LQS} Count:${LOOPCOUNT} Prompt:${LQP})
 same => n,GotoIf($[${LQS} >= ${LOOPCOUNT}]?music)
 same => n,Background(${NixxisSoundPath}/${ACCOUNTCODE}/${LQP},m)
 same => n,WaitExten(2)
 same => n,Set(PLAYING=0)
 same => n,GoTo(music)


 same => n(music),NoOp(Check if we need to (re)start music)
 same => n,ExecIf($["${PLAYING}" = "0"]?StartMusicOnHold)
 same => n,Set(PLAYING=1)
 same => n,Wait(${HMT})
 same => n,Set(LOOPCOUNT=${MATH(${LOOPCOUNT}+1)}) 

 same => n,GotoIf($["${REW}" < "2" & "${RPO}" < "2" ]?loop)

 same => n,Set(RawAnswer=${CURL(${RequestUri})})
 same => n,GoSub(ParseIvrInfo(${RawAnswer}))
 
 same => n,GoTo(loop)

 same => n(failed),Busy()

 same => n(ParseIvrInfo),NoOp(Parsing IvrInfo)
 same => n,While($["${SET(AnswerPart=${SHIFT(ARG1,&)})}" != ""])
 same => n,Set(PartName=${CUT(AnswerPart,=,1)})
 same => n,Set(PartValue=${CUT(AnswerPart,=,2)})
 same => n,ExecIf($["${PartName}" = "x-cr-acc"]?Set(ACCOUNTCODE=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-act"]?Set(ACTIVITY=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-lan"]?Set(LAN=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-lan"]?Set(CHANNEL(language)=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-ewt"]?Set(EWT=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-rew"]?Set(REW=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-pos"]?Set(POS=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-rpo"]?Set(RPO=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-hmt"]?Set(HMT=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-moh"]?Set(MOH=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-lqs"]?Set(LQS=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-lqp"]?Set(LQP=${PartValue}))
 same => n,EndWhile
 same => n,Return


; Parse DTMF result
exten => _X,1,NoOp(Got DTMF: ${EXTEN})
 same => n,Set(LQD=${EXTEN})
 same => n,SendText(CRIVRlqd:${LQD:0:1})
 same => n,Set(PLAYING=0)
 same => n,GoTo(queue,music)

exten => *,1,NoOp(Got DTMF: ${EXTEN})
 same => n,Set(LQD=${EXTEN})
 same => n,SendText(CRIVRlqd:${LQD:0:1})
 same => n,Set(PLAYING=0)
 same => n,GoTo(queue,music)

exten => #,1,NoOp(Got DTMF: ${EXTEN})
 same => n,Set(LQD=${EXTEN})
 same => n,SendText(CRIVRlqd:${LQD:0:1})
 same => n,Set(PLAYING=0)
 same => n,GoTo(queue,music)

;exten => _X.,1,NoOp(Got DTMF: ${EXTEN})
; same => n,Set(LQD=${EXTEN})
; same => n,SendText(CRIVRlqd:${LQD:0:1})
; same => n,GoTo(queue,music)

exten => i,1,GoTo(queue,music)
