public ActionResult FindReplacement(int StackId, int QuestionId, int Level)
        {
            var sqReplacement = new StackQuestionReplacementViewModel
            {
                QuestionId = QuestionId,
                StackId    = StackId,
                Level      = Level
            };

            return(RedirectToAction("Index", "StackQuestionReplacement", sqReplacement));
        }
        // GET: StackQuestionReplacement
        public ActionResult Index(StackQuestionReplacementViewModel stackQuestionReplacement)
        {
            //to do vidi dali difficulty ili level?
            //to do categorii?
            var stackQuery = _context.Questionstacks.Where(sq => sq.StackId == stackQuestionReplacement.StackId)
                             .Select(sq =>
                                     new Questionstacks
            {
                Stack = sq.Stack, StackId = sq.StackId, Type = sq.Type, Timestamp = sq.Timestamp
            });

            if (!stackQuery.Any())
            {
                return(NotFound());
            }

            int QuestionType = stackQuery.FirstOrDefault().Type;

            int levelFromQId    = _context.Questionstackitems.FirstOrDefault(x => x.QuestionId == stackQuestionReplacement.QuestionId && x.StackId == stackQuestionReplacement.StackId).StackLevel;
            int difficultyFromQ = _context.Qleveldifficultymaping.FirstOrDefault(x => x.Level == levelFromQId && x.Maping == MappingType.ToString()).Difficulty;

            if (stackQuery.FirstOrDefault().StackType == QuestionTypeDescription.Qualification)
            {
                difficultyFromQ = 1;
            }

            var gamequestionfromrequest = _context.Gamequestions.FirstOrDefault(x => x.QuestionId == stackQuestionReplacement.QuestionId);

            if (gamequestionfromrequest != null)
            {
            }

            List <Gamequestions> replacementQuestions           = new List <Gamequestions>();
            List <Gamequestions> additionalReplacementQuestions = new List <Gamequestions>();

            if (UseNewQuestions)
            {
                replacementQuestions = _context.GetReplacementQuestions(QuestionType, difficultyFromQ, TimesAnswered: 0);
                //.Take(3).ToList() //= SELECT TOP 3

                if (stackQuestionReplacement.UpValue.HasValue)
                {
                    for (int i = 1; i <= Math.Abs((int)stackQuestionReplacement.UpValue); i++)
                    {
                        additionalReplacementQuestions.AddRange(_context.GetReplacementQuestions(QuestionType, Difficulty: difficultyFromQ + i, 0));
                    }
                }

                if (stackQuestionReplacement.DownValue.HasValue)
                {
                    for (int i = 1; i <= Math.Abs((int)stackQuestionReplacement.DownValue); i++)
                    {
                        additionalReplacementQuestions.AddRange(_context.GetReplacementQuestions(QuestionType, Difficulty: difficultyFromQ - i, 0));
                    }
                }

                replacementQuestions.AddRange(additionalReplacementQuestions);
            }

            var OldQuestionsInUse = UseOldQuestions || UseOldQuestionsForReplacement;

            //polni gi so pominati ako nema novi
            if (OldQuestionsInUse)
            {
                //if (replacementQuestions.Count == 0)
                replacementQuestions.AddRange(_context.GetReplacementQuestions(QuestionType, difficultyFromQ, TimesAnswered: 1).Take(5));

                if (additionalReplacementQuestions.Count == 0)
                {
                    if (stackQuestionReplacement.UpValue.HasValue)
                    {
                        for (int i = 1; i <= Math.Abs((int)stackQuestionReplacement.UpValue); i++)
                        {
                            additionalReplacementQuestions.AddRange(_context.GetReplacementQuestions(QuestionType, Difficulty: difficultyFromQ + i, 1));
                        }
                    }

                    if (stackQuestionReplacement.DownValue.HasValue)
                    {
                        for (int i = 1; i <= Math.Abs((int)stackQuestionReplacement.DownValue); i++)
                        {
                            additionalReplacementQuestions.AddRange(_context.GetReplacementQuestions(QuestionType, Difficulty: difficultyFromQ - i, 1));
                        }
                    }

                    replacementQuestions.AddRange(additionalReplacementQuestions);
                }
            }

            List <ReplacementQuestion> foundReplacementQuestions = new List <ReplacementQuestion>();

            foreach (Gamequestions q in replacementQuestions)
            {
                foundReplacementQuestions.Add(new ReplacementQuestion
                {
                    QuestionId       = q.QuestionId,
                    Question         = q.Question,
                    Difficulty       = (short)q.Difficulty,
                    CategoryId       = (short)q.CategoryId,
                    SubcategoryId    = (short)q.SubcategoryId,
                    MappedLevel      = -1,
                    LastTimeAnswered = q.LastDateAnswered,
                    TimesAnswered    = q.TimesAnswered
                });
            }
            foreach (ReplacementQuestion q in foundReplacementQuestions)
            {
                int levelfromr = -1;
                if (_context.Qleveldifficultymaping?.FirstOrDefault(x => x.Difficulty == q.Difficulty && x.Maping == MappingType.ToString()) != null)
                {
                    levelfromr = _context.Qleveldifficultymaping
                                 .FirstOrDefault(x => x.Difficulty == q.Difficulty && x.Maping == MappingType.ToString())
                                 .Level;
                }

                q.MappedLevel = (short)levelfromr;
            }

            var categoryQuery = _context.Questioncategories.Where(r => r.CategoryId >= 0);

            StackQuestionReplacementViewModel viewModel = new StackQuestionReplacementViewModel
            {
                replacementQuestions = foundReplacementQuestions.OrderBy(x => x.QuestionId).ThenBy(x => x.TimesAnswered).ToList()
            };

            if (stackQuestionReplacement != null && stackQuestionReplacement.SelectedCategoryId > 0)
            {
                viewModel.replacementQuestions = viewModel.replacementQuestions?.Where(x => x.CategoryId == stackQuestionReplacement.SelectedCategoryId);
            }
            if (stackQuestionReplacement != null && stackQuestionReplacement.SelectedSubcategoryId > 0)
            {
                viewModel.replacementQuestions = viewModel.replacementQuestions?.Where(x => x.SubcategoryId == stackQuestionReplacement.SelectedSubcategoryId);
            }
            if (stackQuestionReplacement != null && OldQuestionsInUse && stackQuestionReplacement.LastTimeAnsweredDateTo.HasValue)
            {
                viewModel.replacementQuestions = viewModel.replacementQuestions?.Where(x => !x.LastTimeAnswered.HasValue || x.LastTimeAnswered <= stackQuestionReplacement.LastTimeAnsweredDateTo.Value);
            }

            viewModel.replacementQuestions = new HashSet <ReplacementQuestion>(viewModel.replacementQuestions).ToList();

            viewModel.questionCategories = categoryQuery.ToList();

            viewModel.UseOldQuestions = OldQuestionsInUse;

            if (stackQuestionReplacement != null)
            {
                ViewBag.StackId = stackQuestionReplacement.StackId;
            }
            ViewBag.Level = levelFromQId;

            ViewData["Title"] = levelFromQId;

            return(View(viewModel));
        }