//
        // GET: /
        public ViewResult Index()
        {
            HTMLSanitizer hs = new HTMLSanitizer();

            var QuestionList = from questions in db.Questions
                               orderby questions.Created_At descending
                               select questions;
            List<User> usersList = new List<User>();
            List<TagsIDs> tagList = new List<TagsIDs>();
            foreach (Question vraag in QuestionList)
            {
                // Strip all HTML tags from the question content
                vraag.Content = hs.Sanitize(vraag.Content, HTMLSanitizerOption.StripTags);

                // Limit the content of a question on the front page to max 500 chars.
                if (vraag.Content.Length > 500)
                    vraag.Content = vraag.Content.Substring(0, 500) + " ...";

                // Build a list of tags that this question is tagged with.
                var TagList = from tagsQuestion in db.Tags
                              join c in db.QuestionTags on tagsQuestion.TagID equals c.TagId
                              where c.QuestionId == vraag.QuestionID
                              select tagsQuestion;
                // create a list of all the tags linked to the question which has this tag
                foreach (Tag i in TagList)
                    tagList.Add(new TagsIDs(i, vraag.QuestionID));
                // get the user stuff (for displaying the username)
                var UserList = from users in db.Users
                               where users.UserID == vraag.UserId
                               select users;
                // check ifthe list already has this user if not add it
                if(!usersList.Contains(UserList.First()))
                    usersList.Add(UserList.First());
            }

            // give it all back to the viewbag
            ViewBag.Helper = tagList;
            ViewBag.UsersList = usersList;
            ViewBag.QuestionList = QuestionList;

            return View();
        }
        //
        // GET: Vraag/View/detailNum
        public ActionResult View(int id)
        {
            HTMLSanitizer hs = new HTMLSanitizer();
            Markdown md = new Markdown();

            //questiondetails
            var questionDetails = from questionDetail in db.Questions
                                  where questionDetail.QuestionID == id
                                  select questionDetail;
            questionDetails.First().Views += 1;
            db.SaveChanges();
            // check if logged in
            if (User.Identity.IsAuthenticated)
            {
                // give back the rank of this user
                ViewBag.UserRank = (from user in db.Users
                                    where user.UserName == User.Identity.Name
                                    select user.Rank).Single();
            }
            Question questionDetailView = questionDetails.First();

            // Sanitize the post content and title
            // Then process the content with Markdown
            questionDetailView.Title = hs.EscapeHTMLentities(questionDetailView.Title);
            questionDetailView.Content = hs.Sanitize(questionDetailView.Content, HTMLSanitizerOption.UnescapeMarkDown);
            questionDetailView.Content = md.Transform(questionDetailView.Content);

            ViewBag.QuestionDetail = questionDetailView;

            List<TagsIDs> abc = new List<TagsIDs>();

            // get all the tags on this question
            var TagList = from tagsQuestion in db.Tags
                          join c in db.QuestionTags on tagsQuestion.TagID equals c.TagId
                          where c.QuestionId == id
                          select tagsQuestion;
            // add each to the list
            foreach (Tag i in TagList)
            {
                abc.Add(new TagsIDs(i, id));

                // Sanitize each tag its name
                i.Name = hs.Sanitize(i.Name, HTMLSanitizerOption.StripTags);
            }

            ViewBag.Helper = abc;

            // get the user of this question (for the username and link)
            var useringlist = from users in db.Users
                              where users.UserID == questionDetailView.UserId
                              select users;

            ViewBag.QuestionUser = useringlist.First();

            // get the comments on this question
            var commentList = from comments in db.Comments
                              orderby comments.Created_At descending
                              where comments.QuestionId == id
                              select comments;
            // add each to the list
            List<Comment> commentUserView = new List<Comment>(commentList);
            ViewBag.CommentsList = commentList;

            // get a list of all the users that made the comment
            List<User> CommentingUsers = new List<User>();
            foreach (Comment commentje in commentUserView)
            {
                var userComment = from commentse in db.Users
                                  where commentse.UserID == commentje.UserId
                                  select commentse;
                CommentingUsers.Add(userComment.First());
            }

            ViewBag.UserCommentList = CommentingUsers;

            // get the answers
            var answerQuestion = from answers in db.Answers
                                 orderby answers.Created_At descending
                                 where answers.QuestionId == id
                                 select answers;
            List<Answer> answerQuestionView = new List<Answer>(answerQuestion);
            ViewBag.AnswerQuestionList = answerQuestion;

            // Set the count to 'n' or 'geen' if there are no answers yet
            ViewBag.AnswerCount = answerQuestion.Count();
            ViewBag.AnswerQuestionCount = answerQuestion.Count() > 0 ? answerQuestion.Count().ToString() : "geen" ;

            List<User> AnsweringUsers = new List<User>();
            List<Comment> AnswerComments = new List<Comment>();
            List<User> qCommentUsers = new List<User>();
            // for each answer
            foreach (Answer answertje in answerQuestionView)
            {
                // get the userID (for the username)
                var userAnswer = from answerse in db.Users
                                 where answerse.UserID == answertje.UserId
                                 select answerse;

                AnsweringUsers.Add(userAnswer.First());

                // get al the comments
                var answerCommentList = from qcomments in db.Comments
                                        orderby qcomments.Created_At descending
                                        where qcomments.AnswerId == answertje.AnswerID
                                        select qcomments;

                if (answerCommentList.Count() > 0)
                {
                    // if there are some comments add them
                    foreach (Comment cba in answerCommentList)
                        AnswerComments.Add(cba);
                }
                // foreach comment get the user (for the username again)
                foreach (Comment qCommentje in answerCommentList)
                {
                    var userQComment = from qcommentse in db.Users
                                       where qcommentse.UserID == qCommentje.UserId
                                       select qcommentse;

                    qCommentUsers.Add(userQComment.First());
                }

                // Sanitize the content of the answer and process it with Markdown
                answertje.Content = hs.Sanitize(answertje.Content, HTMLSanitizerOption.UnescapeMarkDown);
                answertje.Content = md.Transform(answertje.Content);
            }
            ViewBag.UserAnswerList = AnsweringUsers;
            ViewBag.AnswerComments = AnswerComments;
            ViewBag.qCommentList = qCommentUsers;

            return View();
        }