//get the users who attend a particular alarm
        public List<User> getAllarmAttendants(string alarmName)
        {
            List<User> attendants = new List<User>();

            using (var db = new HealthModelsDataContext())
            {
                Alarm alarm = db.Alarms.SingleOrDefault(targetAlarm => targetAlarm.name.Equals(alarmName));

                if (alarm != null)
                {
                    List<AlarmAttendance> alarmAttendances = new List<AlarmAttendance>();

                    var attendancesQuery = db.AlarmAttendances.Where(attendace => attendace.alarmId == alarm.id).ToList();
                    alarmAttendances = attendancesQuery;

                    foreach (AlarmAttendance alarmAttendance in alarmAttendances)
                    {
                        User attendant = db.Users.SingleOrDefault(attendantUser => attendantUser.id == alarmAttendance.userId);
                        attendants.Add(attendant);
                    }
                }
            }

            return attendants;
        }
        //to get the users assigned to a particular task
        public List<User> getTaskUsers(string taskName)
        {
            List<User> taskUsers = new List<User>();

            using (var db = new HealthModelsDataContext())
            {
                var taskQuery = db.Tasks.SingleOrDefault(targetTask => targetTask.taskName.Equals(taskName));
                if(taskQuery != null)
                {
                    Task task = taskQuery;
                    int taskId = task.id;

                    var query = db.AssignedTasks.Where(assigned => assigned.taskId == taskId).ToList();
                    List<AssignedTask> assignedTaskUsers = query;

                    foreach(AssignedTask assignedTask in assignedTaskUsers)
                    {
                        int userId = assignedTask.userId;
                        User taskUser = db.Users.SingleOrDefault(user => user.id == userId);
                        taskUsers.Add(taskUser);
                    }
                }
            }

            return taskUsers;
        }
 //delete
 public void deleteAlarmCount(int alarmId)
 {
     using (var db = new HealthModelsDataContext())
     {
         AlarmCount alarmCount = db.AlarmCounts.SingleOrDefault(targetAlarmCount => targetAlarmCount.alarmId == alarmId);
         if(alarmCount != null)
         {
             db.AlarmCounts.DeleteOnSubmit(alarmCount);
             db.SubmitChanges();
         }
     }
 }
        //get SubTasks
        public List<SubTask> getSubtasks()
        {
            List<SubTask> subtask = new List<SubTask>();

            using (var db = new HealthModelsDataContext())
            {
                var query = db.SubTasks.ToList();
                subtask = query;
            }

            return subtask;
        }
        //get SubTask
        public SubTask getSubtask(int id)
        {
            var subtask = new SubTask();

            using (var db = new HealthModelsDataContext())
            {
                var query = db.SubTasks.SingleOrDefault(targetSubtask => targetSubtask.id.Equals(id));
                subtask = query;
            }

            return subtask;
        }
        //get
        public AlarmCount getAlarmCount(int alarmId)
        {
            var alarmCount = new AlarmCount();

            using (var db = new HealthModelsDataContext())
            {
                var query = db.AlarmCounts.SingleOrDefault(targetAlarmCount => targetAlarmCount.alarmId == alarmId);
                alarmCount = query;
            }

            return alarmCount;
        }
        //get tasks
        public List<Task> getTasks()
        {
            List<Task> tasks = new List<Task>();

            using (var db = new HealthModelsDataContext())
            {
                var query = db.Tasks.ToList();
                tasks = query;
            }

            return tasks;
        }
        //get users
        public List<User> getUsers()
        {
            List<User> users = new List<User>();

            using (var db = new HealthModelsDataContext())
            {
                var query = db.Users.ToList();
                users = query;
            }

            return users;
        }
        //get Alarms
        public List<Alarm> getAlarms()
        {
            List<Alarm> alarms = new List<Alarm>();

            using (var db = new HealthModelsDataContext())
            {
                var query = db.Alarms.ToList();
                alarms = query;
            }

            return alarms;
        }
        public AlarmPublisher getAlarmPublisher(int id)
        {
            var publisher = new AlarmPublisher();

            using (var db = new HealthModelsDataContext())
            {
                var query = db.AlarmPublishers.SingleOrDefault(targetPublisher => targetPublisher.id == id);
                publisher = query;
            }

            return publisher;
        }
        public List<AlarmPublisher> getAllAlarmPublishers()
        {
            List<AlarmPublisher> publishers = new List<AlarmPublisher>();

            using (var db = new HealthModelsDataContext())
            {
                var query = db.AlarmPublishers.ToList();
                publishers = query;
            }

            return publishers;
        }
        //status filter
        public List<Task> taskStatusFilter(string status)
        {
            List<Task> filteredTasks = new List<Task>();

            using (var db = new HealthModelsDataContext())
            {
                var query = db.Tasks.Where(targetTask => targetTask.status.Equals(status)).ToList();
                filteredTasks = query;
            }

            return filteredTasks;
        }
        //get task
        public Task getTask(string taskName)
        {
            var task = new Task();

            using (var db = new HealthModelsDataContext())
            {
                var query = db.Tasks.SingleOrDefault(targetTask => targetTask.taskName.Equals(taskName));
                task = query;
            }

            return task;
        }
        //get user
        public User getUser(string username)
        {
            var user = new User();

            using (var db = new HealthModelsDataContext())
            {
                var query = db.Users.SingleOrDefault(targetUser => targetUser.userName.Equals(username));
                user = query;
            }

            return user;
        }
        public Alarm getAlarmById(int id)
        {
            var alarm = new Alarm();

            using (var db = new HealthModelsDataContext())
            {
                var query = db.Alarms.SingleOrDefault(targetAlarm => targetAlarm.id == id);
                alarm = query;
            }

            return alarm;
        }
        //get Alarm
        public Alarm getAlarm(string name)
        {
            var alarm = new Alarm();

            using (var db = new HealthModelsDataContext())
            {
                var query = db.Alarms.SingleOrDefault(targetAlarm => targetAlarm.name.Equals(name));
                alarm = query;
            }

            return alarm;
        }
        //delete alarm
        public void deleteAlarm(string name)
        {
            using (var db = new HealthModelsDataContext())
            {
                Alarm alarm =  db.Alarms.SingleOrDefault(targetAlarm => targetAlarm.name.Equals(name));

                if(alarm!=null)
                {
                    db.Alarms.DeleteOnSubmit(alarm);
                    db.SubmitChanges();
                }
            }
        }
        //delete SubTask
        public void deleteSubTask(int id)
        {
            using (var db = new HealthModelsDataContext())
            {
                SubTask subTask = db.SubTasks.SingleOrDefault(targetSubTask => targetSubTask.id == id);

                if (subTask != null)
                {
                    db.SubTasks.DeleteOnSubmit(subTask);
                    db.SubmitChanges();
                }
            }
        }
        //delete task
        public void deleteTask(string taskName)
        {
            using (var db = new HealthModelsDataContext())
            {
                Task task = db.Tasks.SingleOrDefault(targetTask => targetTask.taskName.Equals(taskName));

                if(task != null)
                {
                    db.Tasks.DeleteOnSubmit(task);
                    db.SubmitChanges();
                }
            }
        }
        public void createPublishedAlarm(int alarmId, int publisherId)
        {
            using (var db = new HealthModelsDataContext())
            {
                var publishedAlarm = new PublishedAlarm();

                publishedAlarm.alarmId = alarmId;
                publishedAlarm.publisherId = publisherId;

                db.PublishedAlarms.InsertOnSubmit(publishedAlarm);
                db.SubmitChanges();
            }
        }
        //increment the count whenever a new user decides to attend a nearby alarm
        public void incrementAlarmCount(int alarmId)
        {
            using (var db = new HealthModelsDataContext())
            {
                AlarmCount alarmCount = db.AlarmCounts.SingleOrDefault(targetAlarmCount => targetAlarmCount.alarmId == alarmId);
                if(alarmCount != null)
                {
                    alarmCount.count += 1;

                    db.SubmitChanges();
                }
            }
        }
        //delete user
        public void deleteUser(string username)
        {
            using (var db = new HealthModelsDataContext())
            {
                User user = db.Users.SingleOrDefault(targetUser => targetUser.userName.Equals(username));

                if(user!=null)
                {
                    db.Users.DeleteOnSubmit(user);
                    db.SubmitChanges();
                }
            }
        }
        public int getPublisherId(int alarmId)
        {
            int publisherId = 0;

            using (var db = new HealthModelsDataContext())
            {
                var query = db.PublishedAlarms.SingleOrDefault(targetPublished => targetPublished.alarmId == alarmId);
                var publishedAlarm = new PublishedAlarm();
                publishedAlarm = query;
                publisherId = publishedAlarm.publisherId;
            }

            return publisherId;
        }
        //create task
        public void createTask(string taskName, string status, string description)
        {
            using (var db = new HealthModelsDataContext())
            {
                var task = new Object_Models.Task();

                task.taskName = taskName;
                task.status = status;
                task.description = description;

                db.Tasks.InsertOnSubmit(task);
                db.SubmitChanges();
            }
        }
        public void createAlarmPublisher(string pacientName, string condition, string location, string password)
        {
            using (var db = new HealthModelsDataContext())
            {
                var publisher = new AlarmPublisher();

                publisher.pacient_name = pacientName;
                publisher.condition = condition;
                publisher.location = location;
                publisher.password = password;

                db.AlarmPublishers.InsertOnSubmit(publisher);
                db.SubmitChanges();
            }
        }
        //create user
        public void createUser(string firstName, string lastName, string username, string password, string role)
        {
            using (var db = new HealthModelsDataContext())
            {
                var user = new User();

                user.firstName = firstName;
                user.lastName = lastName;
                user.userName = username;
                user.password = password;
                user.role = role;

                db.Users.InsertOnSubmit(user);
                db.SubmitChanges();
            }
        }
        //get specific task subtasks
        public List<SubTask> getTaskSubTasks(string taskName)
        {
            List<SubTask> taskSubTasks = new List<SubTask>();

            using (var db = new HealthModelsDataContext())
            {
                Task task = db.Tasks.SingleOrDefault(targetTask => targetTask.taskName.Equals(taskName));
                if (task != null)
                {
                    var query = db.SubTasks.Where(subTask => subTask.taskId == task.id).ToList();
                    taskSubTasks = query;
                }
            }

            return taskSubTasks;
        }
        //create Alarm
        public void createAlarm(string name, string type, string location, string status)
        {
            using (var db = new HealthModelsDataContext())
            {
                var alarm = new Alarm();

                alarm.name = name;
                alarm.type = type;
                alarm.time = DateTime.Now;
                alarm.location = location;
                alarm.status = status;

                db.Alarms.InsertOnSubmit(alarm);
                db.SubmitChanges();
            }
        }
        //create
        public void createAlarmCount(string alarmName)
        {
            using (var db = new HealthModelsDataContext())
            {
                Alarm alarm = db.Alarms.SingleOrDefault(targetAlarm => targetAlarm.name.Equals(alarmName));
                if(alarm!=null)
                {
                    var alarmCount = new AlarmCount();

                    alarmCount.alarmId = alarm.id;
                    alarmCount.count = 1;

                    db.AlarmCounts.InsertOnSubmit(alarmCount);
                    db.SubmitChanges();
                }
            }
        }
        //create SubTask
        public void createSubtask(string taskName, string description, string status)
        {
            using (var db = new HealthModelsDataContext())
            {
                Task task = db.Tasks.SingleOrDefault(targetTask => targetTask.taskName.Equals(taskName));
                if (task != null)
                {
                    var subtask = new SubTask();

                    subtask.taskId = task.id;
                    subtask.description = description;
                    subtask.status = status;

                    db.SubTasks.InsertOnSubmit(subtask);
                    db.SubmitChanges();
                }
            }
        }