public async Task ShowWishesAsync(IDialogContext context, IAwaitable <IMessageActivity> activity)
        {
            List <string> wishes = StateHelper.GetWishlistItems(context);

            //Retrive wishes information
            var to_retrieve = wishes;

            //options
            List <ButtonType> buttons = new List <ButtonType>();

            //fetch only a limited number of wishes
            if (wishes.Count() > Constants.N_ITEMS_CARROUSSEL)
            {
                to_retrieve = wishes.Skip(this.skip)
                              .Take(Constants.N_ITEMS_CARROUSSEL)
                              .ToList();
            }

            var products = new List <Product>();

            foreach (string i in to_retrieve)
            {
                products.Add(ProductController.getProduct(i));
            }

            //Prepare answer

            var reply = context.MakeMessage();
            var text  = "";

            var button_text = "";

            // No products on wishlsit
            if (products.Count == 0)
            {
                text = Interactions.getWishList(Interactions.State.FAIL, 0);
                await context.PostAsync(text);
            }
            // Has Products
            else
            {
                text = Interactions.getWishList(Interactions.State.SUCCESS, skip / Constants.N_ITEMS_CARROUSSEL + 1);
                await Interactions.SendMessage(context, text, 0, 2500);

                //display products
                reply = context.MakeMessage();
                reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                List <Attachment> cards = new List <Attachment>();

                //limit
                for (var i = 0; i < products.Count && i < Constants.N_ITEMS_CARROUSSEL; i++)
                {
                    cards.Add(ProductCard.GetProductCard(products[i], ProductCard.CardType.WISHLIST).ToAttachment());
                }

                reply.Attachments = cards;
                await context.PostAsync(reply);

                //Check if pagination is needed and display wishes
                if (wishes.Count() > this.skip + Constants.N_ITEMS_CARROUSSEL)
                {
                    buttons.Add(ButtonType.PAGINATION);
                    skip        += skip + Constants.N_ITEMS_CARROUSSEL;
                    button_text += "Não consegui exibir todos os seus produtos favoritos. Se desejar ver mais clique no botão abaixo.";
                }
            }

            //add option add more products
            buttons.Add(ButtonType.ADD_PRODUCT);
            button_text += "\nSe desejar adicionar mais produtos aos seus favoritos faça uma pesquisa no nosso catálogo. Para isso, clique no botão abaixo.";

            await Interactions.SendMessage(context, button_text, 2000, 2000);

            //show options
            reply = context.MakeMessage();
            reply.Attachments.Add(getCardButtonsAttachment(buttons, DialogType.WISHLIST));
            await context.PostAsync(reply);

            context.Wait(this.InputHandler);
        }
Exemplo n.º 2
0
        private async Task ShowRecommendations(IDialogContext context, IAwaitable <object> result)
        {
            await Interactions.SendMessage(context, "Tenho tantos produtos que sei que poderiam ser do seu interesse. Espere só um momento, vou analisar o nosso catálogo.", 0, 4000);

            List <Product> products = new List <Product>();
            List <Filter>  popular  = RecommendationsLogic.GetPopularFilters(StateHelper.GetFiltersCount(context));

            while (true)
            {
                FilterDefinition <Product> joinedFilters = FilterLogic.GetJoinedFilter(popular);

                //fetch +1 product to see if pagination is needed
                products = ProductController.getProductsFilter(
                    joinedFilters,
                    Constants.N_ITEMS_CARROUSSEL + 1,
                    this.lastFetchId);

                //filters didn't retrieved any products at the first try
                if (products.Count == 0 && lastFetchId == null)
                {
                    popular.RemoveAt(popular.Count - 1);
                }
                else
                {
                    break;
                }
            }

            List <string> currentWishlist = StateHelper.GetWishlistItems(context);

            foreach (string str in currentWishlist)
            {
                ObjectId       obj = new ObjectId(str);
                List <Product> l   = Logic.RecommendationsLogic.GetSimilarProducts(obj);
                products.InsertRange(0, l);
            }

            if (products.Count > Constants.N_ITEMS_CARROUSSEL)
            {
                lastFetchId = products[products.Count - 2].Id;
            }

            var reply = context.MakeMessage();

            reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
            List <Attachment> cards = new List <Attachment>();

            for (int i = 0; i < products.Count && i < Constants.N_ITEMS_CARROUSSEL; i++)
            {
                cards.Add(ProductCard.GetProductCard(products[i], ProductCard.CardType.RECOMMENDATION).ToAttachment());
            }

            await Interactions.SendMessage(context, "Estas são as minhas recomendações:", 0, 2000);

            reply.Attachments = cards;
            await context.PostAsync(reply);

            //Check if pagination is needed
            if (products.Count <= Constants.N_ITEMS_CARROUSSEL)
            {
                context.Done(new CODE(DIALOG_CODE.DONE));
            }
            else
            {
                await Interactions.SendMessage(context, "Ainda tenho mais recomendações para si. Se for do seu interesse não hesite, carregue no botão abaixo.", 2000, 2000);

                reply = context.MakeMessage();

                reply.Attachments.Add(
                    getCardButtonsAttachment(new List <ButtonType> {
                    ButtonType.PAGINATION
                }, DialogType.RECOMMENDATION));
                await context.PostAsync(reply);


                context.Wait(this.InputHandler);
            }
        }