--$$$GSEScript$$$
--$$$LuaScript$$$
--GSE action: actionAzureTranslate; GSEVersion: 14.11.0.35
--#include "AzureTranslate.lua"


function actionAzureTranslate(tInputParams)
  PBXScript.OutputTrace("Begin GSE Script (Action: actionAzureTranslate)")
  PBXScript.OutputTrace("GSEVersion: 14.11.0.35")

  local retactionAzureTranslate, szState, retVal, rcDummy, bWasDisconnect, UseExit

  szState = "Start0"
  bWasDisconnect = false

  local sTargetLanguage, sSubscriptionKey, sRegion, sSourceText, sSourceLanguage, sReturn

  sTargetLanguage = ""
  sSubscriptionKey = ""
  sRegion = ""
  sSourceText = ""
  sSourceLanguage = ""
  sReturn = ""

  PBXScript.OutputTrace("Input parameters:")
  -- extract input parameters
  sRegion = tInputParams:GetParamValueByName("sRegion", "westeurope")
  PBXScript.OutputTrace("sRegion = ", tostring(sRegion))
  sSubscriptionKey = tInputParams:GetParamValueByName("sSubscriptionKey",  "")
  PBXScript.OutputTrace("sSubscriptionKey = ", tostring(sSubscriptionKey))
  sSourceText = tInputParams:GetParamValueByName("sSourceText",  "")
  PBXScript.OutputTrace("sSourceText = ", tostring(sSourceText))
  sSourceLanguage = tInputParams:GetParamValueByName("sSourceLanguage",  "")
  PBXScript.OutputTrace("sSourceLanguage = ", tostring(sSourceLanguage))
  sTargetLanguage = tInputParams:GetParamValueByName("sTargetLanguage",  "")
  PBXScript.OutputTrace("sTargetLanguage = ", tostring(sTargetLanguage))


  while true do

    -- ////////////////////////
    if (szState == "Start0") then
      PBXScript.OutputTrace("  case [Start0]  :  \"Aktionsbeginn\"  (0)")
      retVal = gseStart()
      if (retVal == gseStateStarted) then
        szState = "SetVariable3"
      else
        PBXScript.OutputTrace("Error: Abnormal case thread \'Start0\' : retVal=", retVal)
        break
      end


    -- ////////////////////////
    elseif (szState == "End1") then
      PBXScript.OutputTrace("  case [End1]  :  \"Aktionsende\"  (1)")
      retactionAzureTranslate = sReturn
      break


    -- ////////////////////////
    elseif (szState == "Disconnect2") then
      PBXScript.OutputTrace("  case [Disconnect2]  :  \"Ruf beendet\"  (2)")
      if (bWasDisconnect ~= true) then
        szState = "End1"
        bWasDisconnect = true
      else
        PBXScript.OutputTrace("GSE anti-LOOP script executed")
        break
      end


    -- ////////////////////////
    elseif (szState == "SetVariable3") then
      PBXScript.OutputTrace("  case [SetVariable3]  :  \"Variable setzen\"  (3)")
      sReturn = "0"
      PBXScript.OutputTrace("    exp: sReturn = \"0\"")

      retVal = gseStateSetVariableNext
      retVal = CheckIfCallIsDisconnected(retVal)
      if (retVal == gseStateSetVariableNext) then
        szState = "InsertScript4"
      elseif (retVal == gseStateDisconnected) then
        szState = "Disconnect2"
      else
        PBXScript.OutputTrace("Error: Abnormal case thread \'SetVariable3\' : retVal=", retVal)
        break
      end


    -- ////////////////////////
    elseif (szState == "InsertScript4") then
      PBXScript.OutputTrace("  case [InsertScript4]  :  \"Skript Code einfügen\"  (4)")

      UseExit = 0  -- default exit

      
      local at = AzureTranslateClass:Create()
      
      at:sRegion          (sRegion)
      at:sSubscriptionKey (sSubscriptionKey)
      at:sSourceText      (sSourceText)
      at:sSourceLanguage  (sSourceLanguage)
      at:sTargetLanguage  (sTargetLanguage)
      
      sReturn = at:Run()
      
      UseExit = 0
      
      
      if (UseExit == 1) then
        retVal = gseStateKey1
      elseif (UseExit == 2) then
        retVal = gseStateKey2
      elseif (UseExit == 3) then
        retVal = gseStateKey3
      elseif (UseExit == 4) then
        retVal = gseStateKey4
      elseif (UseExit == 5) then
        retVal = gseStateKey5
      elseif (UseExit == 6) then
        retVal = gseStateKey6
      elseif (UseExit == 7) then
        retVal = gseStateKey7
      elseif (UseExit == 8) then
        retVal = gseStateKey8
      elseif (UseExit == 9) then
        retVal = gseStateKey9
      else
        retVal = gseStateKey0
      end

      retVal = CheckIfCallIsDisconnected(retVal)

      if (retVal == gseStateKey0) then
        szState = "End1"
      elseif (retVal == gseStateKey1) then
        break
      elseif (retVal == gseStateKey2) then
        break
      elseif (retVal == gseStateKey3) then
        break
      elseif (retVal == gseStateKey4) then
        break
      elseif (retVal == gseStateKey5) then
        break
      elseif (retVal == gseStateKey6) then
        break
      elseif (retVal == gseStateKey7) then
        break
      elseif (retVal == gseStateKey8) then
        break
      elseif (retVal == gseStateKey9) then
        break
      elseif (retVal == gseStateDisconnected) then
        szState = "Disconnect2"
      else
        PBXScript.OutputTrace("Error: Abnormal case thread \'InsertScript4\' : retVal=", retVal)
        break
      end


    -- #########################
    else
      -- Something wrong
      break
    end

  end

  -- update input parameters
  tInputParams:UpdateParamValue("sRegion", sRegion)
  tInputParams:UpdateParamValue("sSubscriptionKey", sSubscriptionKey)
  tInputParams:UpdateParamValue("sSourceText", sSourceText)
  tInputParams:UpdateParamValue("sSourceLanguage", sSourceLanguage)
  tInputParams:UpdateParamValue("sTargetLanguage", sTargetLanguage)

  return retactionAzureTranslate
end

---- SIG ---- Begin Signature
---- SIG ---- YPOAILPGVJANXVLBUPOSLHPOXTMZMATQFUMXAURTMDLREPFYZLOJGTPBTWVS
---- SIG ---- BVKBPNVHEMZUCRDGVHOVFZXIKMGOPIVOMPPDGVXWTWZNLFKTPKWMLMNLMLVP
---- SIG ---- VQHJKKYKXQMSMVATHZXGKNIRZVNNYALOLXVKGSIINNJBOMLWEUKXQLPIMFEN
---- SIG ---- KNOLUMNYHNCUDXANKPGTKZYSZCWPUHDDFZDOSPPBKVJKHEXNPZLAMKMENOKA
---- SIG ---- JVBPWYYLWOQHCPKCSPPWMMUDUCOMOKGIVUQGTSDLYTFLLOQUHYBCHEWJODFA
---- SIG ---- KFIUQHENLJMJEWDNAJQMVEAYBHHKKUMBLKENOVEPKQSXBTTHZOPCMYLCXDRA
---- SIG ---- PFRLOLONVGCXCZAJENVZRPFOLNQTLNIZZOFLXKECOVICIXDOPMNRPEVTNBEN
---- SIG ---- DKNDOKPNAKXPTBTPTXWOBNOYQENJPKHIZPUMANRTYOLODLRKKVRKKSSDZYBK
---- SIG ---- EUBZZFQUIQYLMEWALKJDSNOZQIOTLIDIVREAXQVSVVGOPZOSKQPPCEBMLBBQ
---- SIG ---- FGPYJFQLOHOLKYDYRDPJWBWPGXRXCOLDQPLEIKMIPPDLDRMSUBNWAZUHGHVR
---- SIG ---- GGUIKELYLOKFLLPLDYLLOKXWWRSMBZLLZKHEMPLXJNCJMPLLPRJKODFKMQMT
---- SIG ---- FMUFMMHTTLHPNDUNYHEMYMMGTGXHCWOBTJJORJCZKQEHAGAJKXPMVRBONMZQ
---- SIG ---- QGISFJZYULFFBSVAKLFHCWCQONLLYPWPKEAWBCUDDTTNZOLDKPUNNZCMZWMQ
---- SIG ---- YXMTPWMVLNCWUPNXYOZUKFOZBBNRFRBNUNXOIIFBYLDODYKKYJNAYSLJKUMT
---- SIG ---- GVNPGOMPFALMRNNECMXUWNAFEEMNHGTVHMLFOIXULRMKOZLULKDXNQKCOWJR
---- SIG ---- ZSVDYVNEDBFSWMIPSBMLNBFZPVRFBJQLESPQEKKAELTBYBTSAJLBAAKLSHNN
---- SIG ---- SPBPRIRWNLWNNRGEPDNHCNHHEYWVNKDINKEPIDLQMLLOAUUOBIMRUWKXSLKT
---- SIG ---- BYRP
---- SIG ---- End Signature