public ActionResult RemoveUser(int RemoveUserProjectID, List<string> SelectedAssignedUsers)
        {
            if (ModelState.IsValid)
            {
                //Instantiate the helper and find the user in the DB
                ProjectsHelper helper = new ProjectsHelper(db);
                var project = db.Projects.Find(RemoveUserProjectID);

                //ADD NEW USER(S)


                if(SelectedAssignedUsers != null)
                {
                    foreach (var u in SelectedAssignedUsers)
                    {
                        //If the user isn't assigned to this project
                        if (helper.HasProject(u, RemoveUserProjectID))
                        {
                            //Add the user to the project
                            helper.RemoveUser(u, RemoveUserProjectID);
                        }
                    }

                    db.Entry(project).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

             
            }

            return RedirectToAction("Index");
        }
        public ActionResult ChangeUser(int? id)
        {
            var project = db.Projects.Find(id);
            ProjectUserViewModel ProjectModel = new ProjectUserViewModel();
            ProjectsHelper helper = new ProjectsHelper(db);
            var assigned = helper.AssignedUser(id);
            var unassigned = helper.UnassignedUsers(id);

          
            ProjectModel.AssignedUserList = new MultiSelectList(assigned, "id", "DisplayName");
            ProjectModel.UnassignedUserList = new MultiSelectList(unassigned, "id", "DisplayName");
            ProjectModel.Project = project;

            return View(ProjectModel);

        }
        public async Task<ActionResult> Edit([Bind(Include = "Id,Title,Description,Created,Updated,ProjectId,TicketTypeId,TicketPriorityId,TicketStatusId,AssignedToUserId,OwnerUserId")] Ticket ticket)
        {
            var user = db.Users.Find(User.Identity.GetUserId());

            UserRolesHelper rolesHelper = new UserRolesHelper(db);
            ProjectsHelper helper = new ProjectsHelper(db);
            StringBuilder sb = new StringBuilder();


            //var ticketHistory = db.TicketHistory.Where(t => t.TicketId == ticket.Id).ToList();
            var oldTicket = db.Tickets.AsNoTracking().FirstOrDefault(t => t.Id == ticket.Id);

            if (ModelState.IsValid)
            {
                ticket.Updated = DateTimeOffset.Now;
                db.Entry(ticket).State = EntityState.Modified;
                db.SaveChanges();

                //var oldTicket = db.Tickets.AsNoTracking().FirstOrDefault(t => t.Id == ticket.Id);

                var newTicket = db.Tickets.Find(ticket.Id);

                if (oldTicket != ticket)
                {
                    sb.AppendLine("Changes on " + DateTimeOffset.Now + ":");
                    sb.Append("<br />");

                    if (oldTicket.Title != ticket.Title)
                    {
                        sb.AppendLine("Title changed from " + oldTicket.Title + " to " + ticket.Title + ".");
                        sb.Append("<br />");
                    }
                    if (oldTicket.Description != ticket.Description)
                    {
                        sb.AppendLine("Description changed from " + oldTicket.Description + " to " + ticket.Description + ".");
                        sb.Append("<br />");
                    }
                    if (oldTicket.TicketStatusId != newTicket.TicketStatusId)
                    {
                        var newTicketStatus = db.TicketStatus.Where(s => s.Id == newTicket.TicketStatusId).Select(q => q.Name).FirstOrDefault();
                        sb.AppendLine("Status changed from " + oldTicket.TicketStatus.Name + " to " + newTicketStatus + ".");
                        sb.Append("<br />");
                    }
                    if (oldTicket.TicketPriorityId != ticket.TicketPriorityId)
                    {
                        var newTicketPriority = db.TicketPriority.Where(s => s.Id == newTicket.TicketPriorityId).Select(q => q.Name).FirstOrDefault();
                        sb.AppendLine("Priority changed from " + oldTicket.TicketPriority.Name + " to " + newTicketPriority + ".");
                        sb.Append("<br />");
                    }
                    if (oldTicket.TicketTypeId != ticket.TicketTypeId)
                    {
                        var newTicketType = db.TicketType.Where(s => s.Id == newTicket.TicketTypeId).Select(q => q.Name).FirstOrDefault();
                        sb.AppendLine("Type changed from " + oldTicket.TicketType.Name + " to " + newTicketType + ".");
                        sb.Append("<br />");
                    }
                    if (oldTicket.AssignedToUserId != ticket.AssignedToUserId)
                    {
                        var newTicketUserF = db.Users.Where(s => s.Id == newTicket.AssignedToUserId).Select(q => q.FirstName).FirstOrDefault();
                        var newTicketUserL = db.Users.Where(s => s.Id == newTicket.AssignedToUserId).Select(q => q.LastName).FirstOrDefault();
                        var oldTicketUserF = db.Users.Where(s => s.Id == oldTicket.AssignedToUserId).Select(q => q.FirstName).FirstOrDefault();
                        var oldTicketUserL = db.Users.Where(s => s.Id == oldTicket.AssignedToUserId).Select(q => q.LastName).FirstOrDefault();
                        sb.AppendLine("Assigned User changed from " + oldTicketUserF + " " + oldTicketUserL + " to " + newTicketUserF + " " + newTicketUserL + ".");
                        sb.Append("<br />");
                    }

                    var tHistory = new TicketHistory();
                    tHistory.TicketId = ticket.Id;
                    tHistory.Body = sb.ToString();

                    db.TicketHistory.Add(tHistory);
                    db.SaveChanges();

                }
                else
                {
                    db.SaveChanges();
                }
                              

                await UserManager.SendEmailAsync(ticket.AssignedToUserId, "Ticket Assigned/Modified", "You have been assigned a new ticket, or a ticket you are currently assigned to has been modified.");
                return RedirectToAction("Index");
            }

            ViewBag.AssignedToUserId = new SelectList(rolesHelper.UsersInRole("Developer"), "Id", "DisplayName", ticket.AssignedToUserId);
            //ViewBag.OwnerUserId = new SelectList(db.Users, "Id", "DisplayName", ticket.OwnerUserId);
            ViewBag.ProjectId = new SelectList(helper.AssignedProjects(user.Id), "Id", "Title", ticket.ProjectId);
            ViewBag.TicketPriorityId = new SelectList(db.TicketPriority, "Id", "Name", ticket.TicketPriorityId);
            ViewBag.TicketStatusId = new SelectList(db.TicketStatus, "Id", "Name", ticket.TicketStatusId);
            ViewBag.TicketTypeId = new SelectList(db.TicketType, "Id", "Name", ticket.TicketTypeId);
            return View(ticket);
                }
        public ActionResult Edit(int? id)
        {
            var user = db.Users.Find(User.Identity.GetUserId());
            ProjectsHelper projectHelper = new ProjectsHelper(db);
            UserRolesHelper rolesHelper = new UserRolesHelper(db);
            var userRoles = rolesHelper.ListUserRoles(user.Id);
            Ticket ticket = db.Tickets.Find(id);


            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            if (ticket == null)
            {
                return HttpNotFound();
            }





            //This line specifies the users that are in the role of Developer- only developers can be assigned to a ticket
            ViewBag.AssignedToUserId = new SelectList(rolesHelper.UsersInRole("Developer"), "Id", "DisplayName", ticket.AssignedToUserId);
            ViewBag.OwnerUserId = new SelectList(db.Users, "Id", "DisplayName", ticket.OwnerUserId);
            //if User is assigned to project, then he can make a ticket for that project;
            ViewBag.ProjectId = new SelectList(projectHelper.AssignedProjects(user.Id), "Id", "Title", ticket.ProjectId);
            ViewBag.TicketPriorityId = new SelectList(db.TicketPriority, "Id", "Name", ticket.TicketPriorityId);
            ViewBag.TicketStatusId = new SelectList(db.TicketStatus, "Id", "Name", ticket.TicketStatusId);
            ViewBag.TicketTypeId = new SelectList(db.TicketType, "Id", "Name", ticket.TicketTypeId);


            if (userRoles.Contains("Project Manager"))
            {
                if (ticket.Project.ApplicationUsers.Contains(user))
                {
                    return View(ticket);
                }
            }

            if (userRoles.Contains("Developer"))
            {
                if (ticket.AssignedToUserId == user.Id)
                {
                    return View(ticket);
                }
            }

            return RedirectToAction("Login", "Account");
        }
        public ActionResult Create([Bind(Include = "Id,Title,Description,Created,ProjectId,TicketTypeId,TicketPriorityId,TicketStatusId,OwnerUserId")] Ticket ticket)
        {
            var user = db.Users.Find(User.Identity.GetUserId());
            ProjectsHelper helper = new ProjectsHelper(db);
            TicketHistory ticketHistory = new TicketHistory();



            if (ModelState.IsValid)
            {
                //Ticket Info
                ticket.OwnerUserId = User.Identity.GetUserId();
                ticket.TicketStatusId = 1;
                ticket.Created = DateTimeOffset.Now;
                db.Tickets.Add(ticket);
                ////Ticket History info
                ticketHistory.TicketId = ticket.Id;
                db.TicketHistory.Add(ticketHistory);
                //Saving above to database
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.AssignedToUserId = new SelectList(db.Users, "Id", "DisplayName", ticket.AssignedToUserId);
            ViewBag.OwnerUserId = new SelectList(db.Users, "Id", "DisplayName", ticket.OwnerUserId);
            //if User is assigned to project, then he can make a ticket for that project;
            ViewBag.ProjectId = new SelectList(helper.AssignedProjects(user.Id), "Id", "Title", ticket.ProjectId);
            ViewBag.TicketPriorityId = new SelectList(db.TicketPriority, "Id", "Name", ticket.TicketPriorityId);
            ViewBag.TicketStatusId = new SelectList(db.TicketStatus, "Id", "Name", ticket.TicketStatusId);
            ViewBag.TicketTypeId = new SelectList(db.TicketType, "Id", "Name", ticket.TicketTypeId);

            return View(ticket);
        }
        public ActionResult Create()
        {
            var user = db.Users.Find(User.Identity.GetUserId());
            ProjectsHelper helper = new ProjectsHelper(db);

            ViewBag.AssignedToUserId = new SelectList(db.Users, "Id", "DisplayName");
            ViewBag.OwnerUserId = new SelectList(db.Users, "Id", "DisplayName");

            //if User is assigned to project, then he can make a ticket for that project;
            ViewBag.ProjectId = new SelectList(helper.AssignedProjects(user.Id), "Id", "Title");
            ViewBag.TicketPriorityId = new SelectList(db.TicketPriority, "Id", "Name");
            ViewBag.TicketStatusId = new SelectList(db.TicketStatus, "Id", "Name");
            ViewBag.TicketTypeId = new SelectList(db.TicketType, "Id", "Name");
            return View();
        }