public void RemoveUserFromProject(string userId, int projectId)
        {
            var db = new ApplicationDbContext();
            var project = db.Projects.Single(p => p.Id == projectId);
            var user = db.Users.Single(u => u.Id == userId);
            var helper = new UserRolesHelper(db);
        
            project.Developers.Remove(user);
            db.SaveChanges();

        }
        public ActionResult Create()
        {
            if (!User.IsInRole("Admin"))
            {
                return RedirectToAction("Index", "Home");
            }
            var helper = new UserRolesHelper(db);
            var managers = helper.UsersInRole("ProjectManager").ToList();


            ViewBag.ManagerId = new SelectList(managers, "Id", "Name");
            return View();
        }
        //private ApplicationDbContext db;

        //public ProjectUsersHelper(ApplicationDbContext context)
        //{
        //    this.db = context;
        //}

        //public bool IsDeveloperInProject(string userId, int ProjectId)
        //{
        //    db.Projects.Where(p => p.Id == ProjectId);
        //    var helper = new UserRolesHelper(db);

        //    var devs = db.Users.Where(u => u.Roles.Select("ProjectManager"))

        //    db.Projects.Where(p => p.Developers.Any)
        //    return IsDeveloperInProject();
        //}

        //public string GetUserRole(string userId)
        //{
        //    return userManager.GetRoles(userId).First();
        //}

        public void AddUserToProject(string userId, int projectId)
        {
            var db = new ApplicationDbContext();
            var project = db.Projects.Single(p => p.Id == projectId);
            var user = db.Users.Single(u => u.Id == userId);
            var helper = new UserRolesHelper(db);

            if (helper.IsUserInRole(userId, "ProjectManager") == true){
                project.ManagerId = userId;
                db.Entry(project).State = EntityState.Modified;
                db.SaveChanges();
            }

            if (helper.IsUserInRole(userId, "Developer") == true)
            {
                project.Developers.Add(user);
                db.SaveChanges();
            }
        }
        // GET: Tickets/Create
        public ActionResult Create()
        {
            var helper = new UserRolesHelper(db);
            var user = db.Users.FirstOrDefault(u => u.UserName == User.Identity.Name).Id;
            


            //ViewBag.AuthorId = new SelectList(db.Users, "Id", "FirstName");
            //ViewBag.DeveloperId = new SelectList(db.Users, "Id", "FirstName");
            //ViewBag.PriorityId = new SelectList(db.Priorities, "Id", "Name");
            //ViewBag.ProjectId = new SelectList(db.Projects, "Id", "ManagerId");
            //ViewBag.StatusId = new SelectList(db.Statuses, "Id", "Name");
            ViewBag.TicketTypeId = new SelectList(db.TicketTypes, "Id", "Name");
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name");

            var model = new Ticket
            {
                AuthorId = user
            };
                //ProjectManagerId = project.ManagerId,
                //ProjectManager = project.Manager};

            return View(model);
        }
        public ActionResult Manage(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Ticket ticket = db.Tickets.Find(id);
            if (ticket == null)
            {
                return HttpNotFound();
            }
            var project = ticket.Project;
            var helper = new UserRolesHelper(db);

            var developers = project.Developers.ToList();

            ViewBag.DeveloperId = new SelectList(developers, "Id", "FirstName", ticket.DeveloperId);
            ViewBag.PriorityId = new SelectList(db.Priorities, "Id", "Name", ticket.PriorityId);
            ViewBag.StatusId = new SelectList(db.Statuses, "Id", "Name", ticket.StatusId);
            ViewBag.TicketTypeId = new SelectList(db.TicketTypes, "Id", "Name", ticket.TicketTypeId);
            return View(ticket);
        }
        public ActionResult RemoveUsers(int id)
        {
            var helper = new UserRolesHelper(db);
            var managers = helper.UsersInRole("ProjectManager").ToList();
            var developers = helper.UsersInRole("Developer").ToList();
            var currentproject = db.Projects.Single(p => p.Id == id);

            var project = new ProjectUsersViewModel
            {
                ProjectId = currentproject.Id,
                Name = currentproject.Name,
                Description = currentproject.Description,
                ProjectDevelopers = currentproject.Developers.ToList(),
                ProjectManager = currentproject.Manager,
                CurrentDevelopers = currentproject.Developers.Select(d => d.Id).ToArray(),
                AvailableDevelopers = new SelectList(developers, "Id", "Name"),
            };

            return View(project);
        }
        public ActionResult Edit(int? id)
        {
            if (!User.IsInRole("Admin"))
            {
                return RedirectToAction("Index", "Home");
            }
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Project project = db.Projects.Find(id);
            if (project == null)
            {
                return HttpNotFound();
            }
            var helper = new UserRolesHelper(db);
            var managers = helper.UsersInRole("ProjectManager").ToList();


            ViewBag.ManagerId = new SelectList(managers, "Id", "Name", project.ManagerId);
            return View(project);
        }
 public ActionResult UserRolesChange(UserRolesDetailViewModel model)
 {
     var db = new ApplicationDbContext();
     //var model = new UserRolesDetailViewModel();
     var helper = new UserRolesHelper(db);
     var roleName = db.Roles.Find(model.CurrentRoleId).Name;
     helper.AddUserToRole(model.UserId, roleName);
     db.SaveChanges();
     return RedirectToAction("UserRolesIndex", "Manage");
 }
        public ActionResult UserRolesDetail(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var db = new ApplicationDbContext();
            var helper = new UserRolesHelper(db);

            var user = db.Users.Find(id);

            var model = new UserRolesDetailViewModel
            {
                UserId = id,
                CurrentRoleId = helper.GetUserRole(user.Id),
                AvailableRoles = new SelectList(db.Roles, "Id", "Name")
            };



            return View(model);
        }
        public ActionResult UserRolesIndex(ApplicationUser user)
        {
            var db = new ApplicationDbContext();
            var model = new List<ManageRolesViewModel>();
            var users = db.Users.ToList();
            var helper = new UserRolesHelper(db);

            foreach (var appUser in users)
            {
                model.Add(new ManageRolesViewModel 
                {
                    Id = appUser.Id,
                    FirstName = appUser.FirstName,
                    LastName = appUser.LastName,
                    UserName = appUser.UserName,
                    Role = helper.GetUserRole(appUser.Id)
                });
            }   
            return View(model);
        }