;Version("3.1.2.147")
;InformationalVersion("3.1.2.0")

[nixxis-ivr-v2]

exten => s,1,NoOp(Getting a default IVR call)

 same => n,Set(ACCOUNTCODE=${SIP_HEADER(x-cr-acc)})
 same => n,Set(ACTIVITY=${SIP_HEADER(x-cr-act)})
 same => n,Set(IVRCONFIG=${SIP_HEADER(x-cr-ivrcfg)})
 same => n,Set(IVRDIR=${SIP_HEADER(x-cr-ivrdir)})
 same => n,Set(LAN=${SIP_HEADER(x-cr-lan)})
 same => n,Set(VAR=${SIP_HEADER(x-cr-var)})

 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),GotoIf($["${IVRCONFIG}" = ""]?s,1000)
 same => n,Set(CHANNEL(language)=${LAN})
 same => n,Set(Config=${FILE(${NixxisSoundPath}/${ACCOUNTCODE}/Configs/${IVRCONFIG})})
 same => n,GoSub(ParseSettings(${Config}))
 
 same => n,NoOp(Account set to: ${ACCOUNTCODE})
 same => n,NoOp(Activity set to: ${ACTIVITY})
 same => n,NoOp(Language set to: ${LAN})
 same => n,NoOp(VARIABLES set to: ${VAR})
 same => n,Set(CHANNEL(musicclass)=${ACTIVITY})

 same => n,Answer
 same => n,Wait(2)
 same => n,Set(TIMEOUT(absolute)=14400)
 same => n,Set(PLAYDIR=${NixxisSoundPath}/${ACCOUNTCODE})

 same => n(CheckLan),GotoIf($["${playlanguage}" != "False"]?nixxis-ivr-v2,s,playlan:nixxis-ivr-v2-menu,s,1)
 same => n(playlan),NoOp(Play welcome if we need to)
 same => n,ExecIf($["${WELCOME}" != "" & "${WELCOME}" != "-" ]?Background(${PLAYDIR}/${WELCOME}))

 same => n(lan),GotoIf($["${LAN}" != ""]?nixxis-ivr-v2-menu,s,checkmenu)
 same => n,Set(i=1)
 same => n,While($[${i} < 10])
 same => n,ExecIf($["${LANGUAGE${i}}" != "" & "${LANGUAGE${i}}" != "-" ]?Background(${PLAYDIR}/${LANGUAGE${i}}))
 same => n,Set(i=$[${i} + 1])
 same => n,EndWhile
 same => n,ExecIf($["${LANGUAGE0}" != "" & "${LANGUAGE0}" != "-"]?Background(${PLAYDIR}/${language0}))
 same => n,ExecIf($["${LANGUAGE*}" != "" & "${LANGUAGE*}" != "-"]?Background(${PLAYDIR}/${language*}))
 same => n,ExecIf($["${LANGUAGE#}" != "" & "${LANGUAGE#}" != "-"]?Background(${PLAYDIR}/${language#}))

 same => n,WaitExten(4)
 same => n,GoTo(s,lan)

exten => _[*#0-9],1,NoOp(Got: ${EXTEN})
 same => n,GotoIf($["${language_code${EXTEN}}" = ""]?nixxis-ivr-v2,s,lan)
 same => n,Set(LAN=${language_code${EXTEN}})
 same => n,SendText(${language_value${EXTEN}})
 same => n,Set(IVRCHOICES=${IVRCHOICES}${language_value${EXTEN}})
 same => n,GoTo(nixxis-ivr-v2-menu,s,checkmenu)

 ; Go here and do busy if we are missing stuff...
exten => s,1000,NoOp(No activity id, returning congestion)
 same => 1001,SIPAddHeader(x-cr-err: No activity id)
 same => 1002,n,Busy()
 same => 1003,HangUp

 same => n(ParseSettings),NoOp(Parsing settings)
 same => n,While($["${SET(AnswerPart=${SHIFT(ARG1,\n)})}" != ""])
 same => n,Set(PartName=${CUT(AnswerPart,=,1)})
 same => n,Set(PartValue=${CUT(AnswerPart,=,2)}) 
 same => n,ExecIf($["${PartValue}" = ""]?Set(PartValue=-)
 same => n,Set(${PartName}=${SHIFT(PartValue,.)})
 same => n,EndWhile
 same => n,Return


 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-ani"]?Set(ANI=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-act"]?Set(ACTIVITY=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-ivrcfg"]?Set(IVRCONFIG=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-ivrdir"]?Set(IVRDIR=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-lan"]?Set(LAN=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-var"]?Set(VAR=${PartValue}))
 same => n,EndWhile
 same => n,Return


[nixxis-ivr-v2-menu]

exten => s,1,NoOp(Play welcome if we need to: ${WELCOME})
 same => n,ExecIf($["${WELCOME}" != "" & "${WELCOME}" != "-"]?Background(${PLAYDIR}/${WELCOME}))

 same => n(checkmenu),NoOp(Checking if we need to Play menu)
 same => n,GotoIf($["${playmenu}" != "False"]?nixxis-ivr-v2-menu,s,menu:nixxis-ivr-v2-end,s,1)
 same => n(menu),NoOp(Check if we need to play menu)
 same => n,Set(i=1)
 same => n,While($[${i} < 10])
 same => n,ExecIf($["${menu-${LAN}_${i}}" != "" & "${menu-${LAN}_${i}}" != "-"]?Background(${PLAYDIR}/${MENU-${LAN}_${i}}))
 same => n,Set(i=$[${i} + 1])
 same => n,EndWhile
 same => n,ExecIf($["${menu-${LAN}_0}" != "" & "${menu-${LAN}_0}" != "-"]?Background(${PLAYDIR}/${MENU-${LAN}_0}))
 same => n,ExecIf($["${menu-${LAN}_*}" != "" & "${menu-${LAN}_*}" != "-"]?Background(${PLAYDIR}/${MENU-${LAN}_*}))
 same => n,ExecIf($["${menu-${LAN}_#}" != "" & "${menu-${LAN}_#}" != "-"]?Background(${PLAYDIR}/${MENU-${LAN}_#}))

 same => n,WaitExten(4)
 same => n,GoTo(s,menu)


exten => _[*#0-9],1,NoOp(Got: ${EXTEN})

 same => n,GotoIf($["${menu${EXTEN}_value}" = ""]?nixxis-ivr-v2-menu,s,menu)
 same => n,GoSub(ParseMenuEntry(${menu${EXTEN}_value}\;))
 same => n,ExecIf($["${CRIVRplayPrompt}" != "" ]?Background(${PLAYDIR}/${CRIVRplayPrompt}))
 same => n,GotoIf($[${CRIVRstay} = 1]?nixxis-ivr-v2-menu,s,menu)
 same => n,SendText(CRIVRmenu:${EXTEN})
 same => n,SendText(${menu${EXTEN}_value})
 same => n,Set(IVRCHOICES=${IVRCHOICES}\,CRIVRmenu:${EXTEN}\,${menu${EXTEN}_value})
 same => n,GoTo(nixxis-ivr-v2-end,s,1)

 same => n(ParseMenuEntry),NoOp(Parsing MenuEntry)
 same => n,Set(CRIVRplayPrompt=)
 same => n,Set(CRIVRstay=0)
 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}" = "CRIVRplayPrompt"]?Set(CRIVRplayPrompt=${PartValue}))
 same => n,ExecIf($["${PartName}" = "CRIVRstay"]?Set(CRIVRstay=${PartValue}))
 same => n,EndWhile
 same => n,Return





[nixxis-ivr-v2-end]

exten => s,1,NoOp(Done with IVR sending END)
 same => n,Set(i=1)
 same => n,While($[${i} < 3])
 same => n,SendText(${IVRCHOICES}\,CRIVRend)
 same => n,Set(i=$[${i} + 1])
 same => n,Wait(5)
 same => n,EndWhile
 same => n,HangUp()
