private async Task <CaseDetails> GetCaseDetailsAsync()
        {
            CaseDetails caseDetails = new CaseDetails();
            HttpClient  cxmClient   = new HttpClient();

            cxmClient.BaseAddress = new Uri(cxmEndPoint);
            String             requestParameters = "key=" + cxmAPIKey;
            HttpRequestMessage request           = new HttpRequestMessage(HttpMethod.Get, "/api/service-api/" + cxmAPIName + "/case/" + caseReference + "?" + requestParameters);

            try
            {
                HttpResponseMessage response = cxmClient.SendAsync(request).Result;
                if (response.IsSuccessStatusCode)
                {
                    HttpContent responseContent = response.Content;
                    String      responseString  = responseContent.ReadAsStringAsync().Result;
                    JObject     caseSearch      = JObject.Parse(responseString);
                    caseDetails.customerContact = (String)caseSearch.SelectToken("values.enquiry_details");
                    try
                    {
                        caseDetails.bundle = (String)caseSearch.SelectToken("values.merge_into_pdf");
                    }
                    catch (Exception) { }

                    String temp = (String)caseSearch.SelectToken("values.unitary");
                    try
                    {
                        if (((String)caseSearch.SelectToken("values.unitary")).ToLower().Equals("true"))
                        {
                            caseDetails.unitary = true;
                        }
                        else
                        {
                            caseDetails.unitary = false;
                        }
                    }
                    catch (Exception) { }
                }
                else
                {
                    await SendFailureAsync("Getting case details for " + caseReference, response.StatusCode.ToString());

                    Console.WriteLine("ERROR : GetStaffResponseAsync : " + request.ToString());
                    Console.WriteLine("ERROR : GetStaffResponseAsync : " + response.StatusCode.ToString());
                }
            }
            catch (Exception error)
            {
                await SendFailureAsync("Getting case details for " + caseReference, error.Message);

                Console.WriteLine("ERROR : GetStaffResponseAsync : " + error.StackTrace);
            }
            return(caseDetails);
        }
        public async Task FunctionHandler(object input, ILambdaContext context)
        {
            if (await GetSecrets())
            {
                try
                {
                    if (!Int32.TryParse(secrets.minResponseConfidenceLevel, out minConfidenceLevel))
                    {
                        await SendFailureAsync("minConfidenceLevel not numeric : " + secrets.minResponseConfidenceLevel, "Secrets Error");

                        Console.WriteLine("ERROR : minConfidenceLevel not numeric : " + secrets.minResponseConfidenceLevel);
                        continueProcessing = false;
                    }
                }
                catch (Exception error)
                {
                    await SendFailureAsync("minConfidenceLevel Parse Error : " + secrets.minResponseConfidenceLevel, error.Message);

                    Console.WriteLine("ERROR : minConfidenceLevel Parse Error : " + secrets.minResponseConfidenceLevel + " : " + error.Message);
                    continueProcessing = false;
                }

                try
                {
                    if (!Int32.TryParse(secrets.minAutoResponseLevel, out minAutoRespondLevel))
                    {
                        await SendFailureAsync("minAutoRespondLevel not numeric : " + secrets.minAutoResponseLevel, "Lambda Parameter Error");

                        Console.WriteLine("ERROR : minAutoRespondLevel not numeric : " + secrets.minAutoResponseLevel);
                        continueProcessing = false;
                    }
                }
                catch (Exception error)
                {
                    await SendFailureAsync("minAutoRespondLevel Parse Error : " + secrets.minAutoResponseLevel, error.Message);

                    Console.WriteLine("ERROR : minAutoRespondLevel Parse Error : " + secrets.minAutoResponseLevel + " : " + error.Message);
                    continueProcessing = false;
                }


                if (String.IsNullOrEmpty(secrets.nbcQNAurl))
                {
                    await SendFailureAsync("qnaURL parameter not set", "Secrets Parameter Error");

                    Console.WriteLine("ERROR : qnaURL parameter not set");
                    continueProcessing = false;
                }
                else
                {
                    qnaURL = secrets.nbcQNAurl;
                }
                if (String.IsNullOrEmpty(secrets.nbcQNAauth))
                {
                    await SendFailureAsync("qnaAuthorization parameter not set", "Secrets Parameter Error");

                    Console.WriteLine("ERROR : qnaAuthorization parameter not set");
                    continueProcessing = false;
                }
                else
                {
                    qnaAuthorization = secrets.nbcQNAauth;
                }
                if (continueProcessing)
                {
                    JObject o = JObject.Parse(input.ToString());
                    caseReference = (string)o.SelectToken("CaseReference");
                    Console.WriteLine(caseReference + " : Started");
                    taskToken = (string)o.SelectToken("TaskToken");
                    try
                    {
                        if (context.InvokedFunctionArn.ToLower().Contains("prod"))
                        {
                            Console.WriteLine(caseReference + " : Prod version");
                            liveInstance = true;
                        }
                    }
                    catch (Exception)
                    {
                    }

                    if (liveInstance)
                    {
                        if (caseReference.ToLower().Contains("ema"))
                        {
                            tableName   = secrets.wncEMACasesLive;
                            cxmEndPoint = secrets.cxmEndPointLive;
                            cxmAPIKey   = secrets.cxmAPIKeyLive;
                            cxmAPIName  = secrets.cxmAPINameWest;
                        }
                        if (caseReference.ToLower().Contains("emn"))
                        {
                            west        = false;
                            tableName   = secrets.nncEMNCasesLive;
                            cxmEndPoint = secrets.cxmEndPointLiveNorth;
                            cxmAPIKey   = secrets.cxmAPIKeyLiveNorth;
                            cxmAPIName  = secrets.cxmAPINameNorth;
                        }

                        caseDetails = await GetCaseDetailsAsync();

                        if (west)
                        {
                            if (await GetProposedResponse() && await UpdateCaseDetailsAsync() && await TransitionCaseAsync())
                            {
                                await SendSuccessAsync();
                            }
                        }
                        else
                        {
                            await TransitionCaseAsync();
                            await SendSuccessAsync();
                        }
                    }
                    else
                    {
                        if (caseReference.ToLower().Contains("ema"))
                        {
                            tableName   = secrets.wncEMACasesTest;
                            cxmEndPoint = secrets.cxmEndPointTest;
                            cxmAPIKey   = secrets.cxmAPIKeyTest;
                            cxmAPIName  = secrets.cxmAPINameWest;
                        }
                        if (caseReference.ToLower().Contains("emn"))
                        {
                            west        = false;
                            tableName   = secrets.nncEMNCasesTest;
                            cxmEndPoint = secrets.cxmEndPointTestNorth;
                            cxmAPIKey   = secrets.cxmAPIKeyTestNorth;
                            cxmAPIName  = secrets.cxmAPINameNorth;
                        }

                        caseDetails = await GetCaseDetailsAsync();

                        if (west)
                        {
                            if (await GetProposedResponse() && await UpdateCaseDetailsAsync() && await TransitionCaseAsync())
                            {
                                await SendSuccessAsync();
                            }
                        }
                        else
                        {
                            await TransitionCaseAsync();
                            await SendSuccessAsync();
                        }
                    }
                }
            }
        }