/// <summary>
        ///  check if the users answer matches the correct answer
        /// </summary>
        /// <param name="intent"></param>
        /// <param name="correctAnswerIndex"></param>
        /// <param name="resource"></param>
        /// <returns>true if a match fails otherwise</returns
        private bool MatchUserGuessWithAnswer(Intent intent, int correctAnswerIndex, QuestionsResource resource)
        {
            Dictionary <string, Slot> slots = intent.Slots;
            double num;

            if (slots == null)
            {
                return(false);
            }

            // Get the 'Answer' slot from the list slots.
            Slot answerSlot = slots[ANSWER_SLOT];

            if (answerSlot != null)
            {
                var  answer = int.Parse(answerSlot.Value);
                bool flag   = double.TryParse(answerSlot.Value, out num);

                // Check for answer and create output to user.
                if (flag)
                {
                    if (answer == correctAnswerIndex && answer <= ANSWER_COUNT && answer > 0)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        /// <summary>
        ///  GetGameQuestions
        /// </summary>
        /// <param name="resource, resource for current locale"></param>
        /// <param name="withPreface"></param>
        /// <returns>list of question</returns
        private List <Questions> GetGameQuestions(QuestionsResource resource, bool withPreface)
        {
            // Pick GAME_LENGTH random questions from the list to ask the user, make sure there are no repeats.
            List <Questions> gameQuestions = new List <Questions>();
            var index = resource.Questions.Count;

            if (GAME_LENGTH > index)
            {
                throw new Exception("Invalid Game Length. " + index);
            }

            int i = 0;

            foreach (var question in resource.Questions.OrderBy(ans => rand.Next()))
            {
                gameQuestions.Add(question);
                i++;

                if (i == GAME_LENGTH)
                {
                    break;
                }
            }
            return(gameQuestions);
        }
        /// <summary>
        ///  handle a game start message and start the game
        /// </summary>
        /// <param name="game"></param>
        /// <param name="resource"></param>
        /// <returns>void</returns
        private void GameStartHandler(bool newGame, QuestionsResource resource)
        {
            var message = string.Format(resource.NewGameMessage, resource.gameName)
                          + string.Format(resource.WelcomeMessage, GAME_LENGTH.ToString());
            var speechOutput = newGame ? message : string.Empty;

            List <Questions> gameQuestions = GetGameQuestions(resource, false);

            int    currentQuestionIndex = 0;
            int    correctAnswerIndex   = (rand.Next(1, ANSWER_COUNT)) - 1;
            var    roundAnswers         = GetShuffledAnswers(gameQuestions.ElementAt(currentQuestionIndex), currentQuestionIndex, correctAnswerIndex);
            string correctAnswerText    = GetCorrectAnswer(roundAnswers, correctAnswerIndex);
            string reprompt             = string.Format(resource.TellQuestionMessage, (currentQuestionIndex + 1).ToString()) +
                                          GetAllAnswersText(gameQuestions.ElementAt(currentQuestionIndex), roundAnswers, currentQuestionIndex);

            correctAnswerIndex++;
            speechOutput = speechOutput + reprompt;
            gameState    = GAME_STATE.TRIVIA;

            skillResponse.SessionAttributes = new Dictionary <string, object>()
            {
                [SPEECHOUTPUT_KEY]    = reprompt,
                [REPROMPT_KEY]        = reprompt,
                [CURRENTQUESTION_KEY] = currentQuestionIndex,
                [CORRECTANSWERINDEX]  = correctAnswerIndex,
                [GAME_QUESTIONS_KEY]  = gameQuestions,
                [SCORE]          = 0,
                [ANSWERTEXT_KEY] = correctAnswerText,
                [STATE_KEY]      = gameState.ToString()
            };

            BuildSpeechResponse(resource.gameName, speechOutput, reprompt, false);
        }
        /// <summary>
        ///  configure and return a help message
        /// </summary>
        /// <param name="game"></param>
        /// <param name="resource"></param>
        /// <returns>void</returns
        private void HelpHandler(bool game, QuestionsResource resource)
        {
            var    askMessage   = game ? resource.AskMessageStart : resource.RepeatQuestionMessage + resource.AskMessageStart;
            string speechOutput = string.Format(resource.HelpMessage, GAME_LENGTH) + askMessage;

            gameState = GAME_STATE.HELP;
            BuildSpeechResponse(string.Empty, speechOutput, string.Empty, false);
        }
        /// <summary>
        /// Process any help request
        /// </summary>
        /// <param name="game"></param>
        /// <param name="skillinput"></param>
        /// <param name="intentrequest"></param>
        /// <param name="resource"></param>
        /// <returns>void</returns
        private void GameStateHandler(SkillRequest input, Request intentRequest, QuestionsResource resource)
        {
            if (IsDialogIntentRequest(input))
            {
                if (!IsDialogSequenceComplete(input))
                {
                    // delegate to Alexa until dialog is complete
                    CreateDelegateResponse();
                    return;
                }
            }

            switch (input.Request.Intent.Name)
            {
            case AlexaConstants.StartOverIntent:
                GameStartHandler(true, resource);
                break;

            case AlexaConstants.RepeatIntent:
                RebuildSession(input.Session, GAME_STATE.TRIVIA);
                string reprompt = input.Session.Attributes[REPROMPT_KEY].ToString();
                BuildSpeechResponse(resource.gameName, reprompt, reprompt, false);
                break;

            case AlexaConstants.CancelIntent:
            case AlexaConstants.StopIntent:
                BuildSpeechResponse(string.Empty, resource.CancelMessage, string.Empty, true);
                break;

            case AlexaConstants.HelpIntent:
                HelpStateHandler(false, input, input.Request, resource);
                break;

            case "AnswerIntent":
                HandleUserGuess(true, input, input.Request.Intent, resource);
                break;

            case "DontKnowIntent":
                HandleUserGuess(true, input, input.Request.Intent, resource);
                break;

            default:
                BuildSpeechResponse(string.Empty, resource.HelpReprompt, string.Empty, false);
                break;
            }
        }
        /// <summary>
        ///  handle a game start message and start the game
        /// </summary>
        /// <param name="userGaveUp"></param>
        /// <param name="input"></param>
        /// <param name="intent"></param>
        /// <param name="resource"></param>
        /// <returns>void</returns
        private void HandleUserGuess(bool userGaveUp, SkillRequest input, Intent intent, QuestionsResource resource)
        {
            Dictionary <string, object> sessionData = input.Session.Attributes;

            var speechOutput         = string.Empty;
            var speechOutputAnalysis = string.Empty;

            if (sessionData != null)
            {
                var gameQuestions = input.Session.Attributes[GAME_QUESTIONS_KEY];
                var correctAnswerIndex_previousQuestion = int.Parse(input.Session.Attributes[CORRECTANSWERINDEX].ToString());
                currentScore = int.Parse(input.Session.Attributes[SCORE].ToString());
                var currentQuestionIndex_previousQuestion = int.Parse(input.Session.Attributes[CURRENTQUESTION_KEY].ToString());
                var correctAnswerText = input.Session.Attributes[ANSWERTEXT_KEY];

                if (MatchUserGuessWithAnswer(intent, correctAnswerIndex_previousQuestion, resource))
                {
                    currentScore++;
                    speechOutputAnalysis = resource.AnswerCorrectMessage;
                }
                else
                {
                    if (!userGaveUp)
                    {
                        speechOutputAnalysis = resource.AnswerWrongMessage;
                    }
                    speechOutputAnalysis += string.Format(resource.CorrectAnswerMessage, correctAnswerIndex_previousQuestion, correctAnswerText);
                }

                // Check if we can exit the game session after GAME_LENGTH questions (zero-indexed)
                if (currentQuestionIndex_previousQuestion >= GAME_LENGTH - 1)
                {
                    speechOutput  = userGaveUp ? string.Empty : resource.AnswerIsMessage;
                    speechOutput += speechOutputAnalysis + string.Format(resource.GameOverMessage, currentScore.ToString(), GAME_LENGTH.ToString());

                    BuildSpeechResponse(string.Empty, speechOutput, string.Empty, true);
                    return;
                }
                else
                {
                    var currentQuestionIndexNextQuestion = currentQuestionIndex_previousQuestion + 1;
                    var correctAnswerIndexNextQuestion   = (rand.Next(1, ANSWER_COUNT)) - 1;

                    List <Questions> questions     = JsonConvert.DeserializeObject <List <Questions> >(gameQuestions.ToString());
                    List <Questions> questionsList = new List <Questions>();

                    foreach (var item in questions)
                    {
                        questionsList.Add(new Questions()
                        {
                            Question   = item.Question,
                            AnswerList = GetAnswersList(item.AnswerList)
                        });
                    }

                    var    shuffledAnswers = GetShuffledAnswers(questionsList.ElementAt(currentQuestionIndexNextQuestion), currentQuestionIndexNextQuestion, correctAnswerIndexNextQuestion);
                    var    correctAnswerText_nextQuestion = GetCorrectAnswer(shuffledAnswers, correctAnswerIndexNextQuestion);
                    string reprompt = string.Format(resource.TellQuestionMessage, (currentQuestionIndexNextQuestion + 1).ToString()) +
                                      GetAllAnswersText(questionsList.ElementAt(currentQuestionIndexNextQuestion), shuffledAnswers, currentQuestionIndexNextQuestion);

                    speechOutput  = userGaveUp ? string.Empty : resource.AnswerIsMessage;
                    speechOutput += speechOutputAnalysis + " " + string.Format(resource.ScoreIsMessage, currentScore.ToString()) + " " + reprompt;

                    correctAnswerIndexNextQuestion++;;
                    gameState = GAME_STATE.TRIVIA;

                    skillResponse.SessionAttributes = new Dictionary <string, object>()
                    {
                        [SPEECHOUTPUT_KEY]    = reprompt,
                        [REPROMPT_KEY]        = reprompt,
                        [CURRENTQUESTION_KEY] = currentQuestionIndexNextQuestion,
                        [CORRECTANSWERINDEX]  = correctAnswerIndexNextQuestion,
                        [GAME_QUESTIONS_KEY]  = questionsList,
                        [SCORE]          = currentScore,
                        [ANSWERTEXT_KEY] = correctAnswerText_nextQuestion,
                        [STATE_KEY]      = gameState.ToString()
                    };

                    BuildSpeechResponse(resource.gameName, speechOutput, reprompt, false);
                    return;
                }
            }

            BuildSpeechResponse(string.Empty, AlexaConstants.ErrorMessage, string.Empty, false);
        }
        /// <summary>
        /// Process any help request
        /// </summary>
        /// <param name="game"></param>
        /// <param name="skillinput"></param>
        /// <param name="intentrequest"></param>
        /// <param name="resource"></param>
        /// <returns>void</returns
        private void HelpStateHandler(bool game, SkillRequest input, Request intentRequest, QuestionsResource resource)
        {
            Dictionary <string, object> sessionData = input.Session.Attributes;
            bool   newGame      = true;
            string speechOutput = string.Empty;
            bool   endSession   = false;

            switch (intentRequest.Intent.Name)
            {
            case AlexaConstants.StartOverIntent:
                gameState = GAME_STATE.START;
                GameStartHandler(false, resource);
                return;

            case AlexaConstants.RepeatIntent:
            case AlexaConstants.HelpIntent:
                if (sessionData != null &&
                    sessionData.ContainsKey("SPEECHOUTPUT_KEY") &&
                    sessionData.ContainsKey("REPROMPT_KEY"))
                {
                    if (input.Session.Attributes[SPEECHOUTPUT_KEY].ToString() != null &&
                        input.Session.Attributes[REPROMPT_KEY].ToString() != null)
                    {
                        newGame = false;
                    }
                }

                HelpHandler(newGame, resource);
                return;

            case AlexaConstants.StopIntent:
            case AlexaConstants.CancelIntent:
                speechOutput = resource.CancelMessage;
                endSession   = true;
                break;

            case AlexaConstants.SessionEndedRequest:
                speechOutput = "Error in help state. Session ended";
                endSession   = true;
                break;

            default:
                speechOutput = resource.HelpUnhandeled;
                break;
            }

            BuildSpeechResponse(string.Empty, speechOutput, string.Empty, endSession);
        }