public OverviewModel ProjectOverview(int Id)
        {
            OverviewModel model;

            int userId = int.Parse(User.Identity.GetProjectUserId());

            var managerTask = new TaskManager();
            var managerProject = new ProjectUserManager();
            var tasks = managerTask.GetTasksForProject(Id);

            int tasksDone = tasks.Where(t => t.State == managerTask.GetDoneStateId()).Count();
            int tasksActive = tasks.Where(t => t.State == managerTask.GetActiveStateId()).Count();
            int tasksUnassigned = managerTask.GetUnassignedTasks(Id).Count();
            double workHours = managerTask.GetAllWorkTimeForProject(Id)
                .Sum(w => w.EndTime.Subtract(w.StartTime).TotalSeconds);

            var users = managerProject.GetUsersForProject(Id);
            List<string> devs = new List<string>();
            foreach (var u in users)
            {
                devs.Add(u.UserName);
            }

            var project = managerProject.GetProject(Id);

            var projectLeaderName = managerProject.GetProjectLeader(Id).UserName;

            model = new OverviewModel(
                project.Id, project.Name, project.Description, project.Deadline, devs,
                tasksDone, tasksActive, tasksUnassigned,
                (int)(workHours / 3600), projectLeaderName);

            return model;
        }
        private PartialViewResult ShowTasks(int projectId)
        {
            List<TaskListElement> model = new List<TaskListElement>();

            int userId = int.Parse(User.Identity.GetProjectUserId());
            var manager = new TaskManager();

            int deletedId = manager.GetDeletedStateId();

            ViewData["projectId"] = projectId;
            ViewData["deletedId"] = deletedId;
            ViewData["isLeader"] = new ProjectUserManager().IsLeader(userId, projectId);
            ViewData["isDone"] = new ProjectUserManager().GetProject(projectId).Done;

            var tasks = manager.GetTasksForProject(projectId);

            foreach (var t in tasks)
            {
                var name = manager.GetTaskStateName(t.Id);

                var isCommented = manager.IsCommented(t.Id);

                var workHours = manager.GetAllWorkTimeForTask(t.Id)
                    .Sum(w => w.EndTime.Subtract(w.StartTime).TotalSeconds);

                var developers = manager.GetUsersForTask(t.Id);

                List<string> devs = new List<string>();
                foreach (var v in developers)
                {
                    devs.Add(v.UserName);
                }

                TaskListElement element = new TaskListElement(t, name, devs, (int)(workHours / 3600), isCommented);
                model.Add(element);
            }

            return PartialView("_Tasks", model);
        }
        private PartialViewResult ShowOverview(int projectId)
        {
            OverviewModel model;

            int userId = int.Parse(User.Identity.GetProjectUserId());

            var managerTask = new TaskManager();
            var managerProject = new ProjectUserManager();
            var tasks = managerTask.GetTasksForProject(projectId);

            ViewData["isLeader"] = managerProject.IsLeader(userId, projectId);

            int tasksDone = tasks.Where(t => t.State == managerTask.GetDoneStateId()).Count();
            int tasksActive = tasks.Where(t => t.State == managerTask.GetActiveStateId()).Count();
            int tasksUnassigned = managerTask.GetUnassignedTasks(projectId).Count();
            double workHours = managerTask.GetAllWorkTimeForProject(projectId)
                .Sum(w => w.EndTime.Subtract(w.StartTime).TotalSeconds);

            var users = managerProject.GetUsersForProject(projectId);
            List<string> devs = new List<string>();
            foreach (var u in users)
            {
                devs.Add(u.UserName);
            }

            var project = managerProject.GetProject(projectId);

            var projectLeaderName = managerProject.GetProjectLeader(projectId).UserName;

            model = new OverviewModel(
                project, devs,
                tasksDone, tasksActive, tasksUnassigned,
                (int)(workHours / 3600), projectLeaderName);

            return PartialView("_Overview", model);
        }
        private PartialViewResult ShowDevelopers(int projectId)
        {
            List<DeveloperListElement> model = new List<DeveloperListElement>();

            int userId = int.Parse(User.Identity.GetProjectUserId());

            var managerTask = new TaskManager();
            var managerProject = new ProjectUserManager();

            ViewData["projectId"] = projectId;
            ViewData["isLeader"] = managerProject.IsLeader(userId, projectId);
            ViewData["isDone"] = managerProject.GetProject(projectId).Done;

            var tasks = managerTask.GetTasksForProject(projectId);
            var users = managerProject.GetUsersForProject(projectId);

            foreach (var u in users)
            {
                var workHours = managerTask.GetAllWorkTimeForUser(u.Id, projectId)
                    .Sum(w => w.EndTime.Subtract(w.StartTime).TotalSeconds);

                int tasksDone = tasks.Where(t => t.State == managerTask.GetDoneStateId()).Count();
                int tasksAssigned = managerTask.GetAssignedTasks(u.Id, projectId).Count();

                model.Add(new DeveloperListElement(u, (int)(workHours / 3600), tasksDone, tasksAssigned));
            }

            return PartialView("_Developers", model);
        }
        // GET: Projects
        public ActionResult Index()
        {
            int userId = int.Parse(User.Identity.GetProjectUserId());
            var projectUserManager = new ProjectUserManager();
            var taskManager = new TaskManager();

            var projects = projectUserManager.GetProjectsForUser(userId);
            var projectListElements = new List<ProjectListElement>();

            foreach (var p in projects)
            {
                int devs = projectUserManager.GetUsersForProject(p.Id).Count;
                int tasks = taskManager.GetTasksForProject(p.Id).Count;
                var element = new ProjectListElement(p, devs, tasks);
                projectListElements.Add(element);
            }

            if (TempData.ContainsKey("errorMessage")){
                ViewData["errorMessage"] = TempData["errorMessage"];
            }

            return View(new ProjectsViewModel(projectListElements));
        }
        private void BuildTaskDoneWorktimeValues(List<object> xValues, List<object> yValues, DateTime startTime, DateTime endTime,
            int projectId)
        {
            var managerProject = new ProjectUserManager();
            var managerTask = new TaskManager();

            int doneId = managerTask.GetDoneStateId();
            var doneTasks = managerTask.GetTasksForProject(projectId).Where(t => t.State == doneId);
            foreach (var t in doneTasks)
            {
                double seconds = 0.0;

                if (startTime != DateTime.MinValue && endTime != DateTime.MinValue)
                {
                    seconds = managerTask.GetAllWorkTimeForTask(t.Id)
                        .Where(wt => wt.StartTime >= startTime && wt.EndTime <= endTime)
                        .Sum(w => w.EndTime.Subtract(w.StartTime).TotalSeconds);
                }
                else if (startTime != DateTime.MinValue && endTime == DateTime.MinValue)
                {
                    seconds = managerTask.GetAllWorkTimeForTask(t.Id)
                        .Where(wt => wt.StartTime >= startTime)
                        .Sum(w => w.EndTime.Subtract(w.StartTime).TotalSeconds);
                }
                else if (startTime == DateTime.MinValue && endTime != DateTime.MinValue)
                {
                    seconds = managerTask.GetAllWorkTimeForTask(t.Id)
                        .Where(wt => wt.EndTime <= endTime)
                        .Sum(w => w.EndTime.Subtract(w.StartTime).TotalSeconds);
                }
                else
                {
                    seconds = managerTask.GetAllWorkTimeForTask(t.Id)
                        .Sum(w => w.EndTime.Subtract(w.StartTime).TotalSeconds);
                }

                xValues.Add(t.Name);
                yValues.Add(((int)seconds / 3600));
            }
        }
        private void BuildNumberOfFinishedTasksValues(List<object> xValues, List<object> yValues, DateTime startTime, DateTime endTime,
            int projectId)
        {
            var managerProject = new ProjectUserManager();
            var managerTask = new TaskManager();

            int count = 0;
            int doneId = managerTask.GetDoneStateId();
            var doneTasks = managerTask.GetTasksForProject(projectId).Where(t => t.State == doneId);
            foreach (var t in doneTasks)
            {
                var dtst = managerTask.GetDoneTaskStateChanges(t.Id);
                if (dtst.Count == 0)    // invalid test data
                    continue;

                var max = dtst.Max(st => st.Timestamp);
                var finalStateChange = dtst.SingleOrDefault(st => st.Timestamp == max);

                if (startTime != DateTime.MinValue && endTime != DateTime.MinValue)
                {
                    if (finalStateChange.Timestamp >= startTime && finalStateChange.Timestamp <= endTime)
                        count++;
                }
                else if (startTime != DateTime.MinValue && endTime == DateTime.MinValue)
                {
                    if (finalStateChange.Timestamp >= startTime)
                        count++;
                }
                else if (startTime == DateTime.MinValue && endTime != DateTime.MinValue)
                {
                    if (finalStateChange.Timestamp <= endTime)
                        count++;
                }
                else
                {
                    count++;
                }
            }

            xValues.Add("Finished Tasks");
            yValues.Add(count);
        }
        public IEnumerable<TaskListElement> TaskList(int Id)
        {
            int userId = int.Parse(User.Identity.GetProjectUserId());

            List<TaskListElement> res = new List<TaskListElement>();
            var manager = new TaskManager();
            foreach (Task t in manager.GetTasksForProject(Id))
            {
                string stateName = manager.GetTaskStateName(t.State);

                IEnumerable<ProjectUser> devUsers = manager.GetUsersForTask(t.Id);
                List<string> devs = new List<string>();
                foreach (var v in devUsers)
                {
                    devs.Add(v.UserName);
                }

                var workHours = manager.GetAllWorkTimeForTask(t.Id)
                    .Sum(w => w.EndTime.Subtract(w.StartTime).TotalSeconds);

                bool hasComments = manager.IsCommented(t.Id);

                res.Add(new TaskListElement(t.Id,t.Name, stateName, devs, (int)(workHours / 3600), hasComments));
            }

            return res;
        }