using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using System.Threading;
 
namespace Dialog.Dialogs 
{
    public class MyDialog : DialogSet
    {
        public MyDialog(IStatePropertyAccessor<DialogState> dialogState) : base(dialogState)
        {
            var dialogSteps = new WaterfallStep[]
           { NameStep, AgeStep, SummaryStep };

            Add(new WaterfallDialog("Start", dialogSteps));
              
            Add(new TextPrompt("InputName"));
            Add(new NumberPrompt<int>("InputAge", AgeValidator));
             // DateTimePrompt 
              // ConfirmPrompt
              // ChoicePrompt
              // AttachmentPrompt
              
        }

        private static async Task<DialogTurnResult> NameStep(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            return await stepContext.PromptAsync("InputName", new PromptOptions { Prompt = MessageFactory.Text("Veuillez entrer votre nom :") }, cancellationToken);
        }
         

        private static async Task<DialogTurnResult> AgeStep(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            stepContext.Values["name"] = (string)stepContext.Result;
            var promptOptions = new PromptOptions
            {
                Prompt = MessageFactory.Text("Veuillez entrer votre âge : "),
                RetryPrompt = MessageFactory.Text("La valeur que vous avez saisi doit être entre 0 et 130 !, veeuillez saisir de nouveau votre âge :"),
            };

            return await stepContext.PromptAsync("InputAge", promptOptions, cancellationToken);
        }

        private async Task<DialogTurnResult> SummaryStep(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            stepContext.Values["age"] = (int)stepContext.Result;
            string msg = "Nom : " + stepContext.Values["name"] + "\n " + "Age : " + stepContext.Values["age"];
            await stepContext.Context.SendActivityAsync(MessageFactory.Text(msg), cancellationToken);
            return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
        }



        private static Task<bool> AgeValidator(PromptValidatorContext<int> promptContext, CancellationToken cancellationToken)
        {
            
            return Task.FromResult(promptContext.Recognized.Succeeded && promptContext.Recognized.Value > 0 && promptContext.Recognized.Value < 125);
        }








    }
}
