public void GetPossibleMeetingsTest()
        {
            //Test parameter null
            Assert.ThrowsException <ArgumentNullException>(() => { MeetingScore.GetPossibleMeetings(null, 0); });
            Event @event = EventTests.GetSimpleEvent(1);

            //Test creation
            List <User> users = new List <User>()
            {
                UserTests.GetSimpleUser(1),
                UserTests.GetSimpleUser(2),
                UserTests.GetSimpleUser(3),
                UserTests.GetSimpleUser(4)
            };

            for (int i = 0; i < 4; i++)
            {
                users[i].Invites.Add(new Invite(@event, users[i], DateTime.Now));
            }
            List <MeetingScore> meetings = MeetingScore.GetPossibleMeetings(users, @event.Id);

            //3+2+1 = 6
            Assert.AreEqual(6, meetings.Count, "The correct amount of meetings wasnt created");
            Assert.AreEqual(3, meetings.Count(m => m.Person1.Id == 3 || m.Person2.Id == 3), "the users arent in the correct amount of meetings");
        }
        public ActionResult CreateList(EventListCreationModel viewModel)
        {
            MeetupModel model = new MeetupModel();

            //Makes sure the user owns the event
            int?infoID = this.UserId();

            viewModel.EventInformation = model.Events.SingleOrDefault(e => e.Id == viewModel.EventId && e.HostUserId == infoID);
            if (viewModel.EventInformation is null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            //Make sure there are enough people
            if (viewModel.EventInformation.Invites.Count < 2)
            {
                return(View("NotEnoughPeople", viewModel));
            }

            //Creates a list of all users in the event
            List <User> UsersInEvent = viewModel.EventInformation.GetUsers().ToList();

            //Clear old meeting list
            foreach (Seance meetingRow in viewModel.EventInformation.Seances)
            {
                model.Meetings.RemoveRange(meetingRow.Meetings);
                model.UserPauses.RemoveRange(meetingRow.UserPauses);
            }
            model.Seances.RemoveRange(viewModel.EventInformation.Seances);

            //Get list of all possible meetings
            List <MeetingScore> possibleMeetings = MeetingScore.GetPossibleMeetings(UsersInEvent, viewModel.EventId);

            //Generate Seances/meetings list
            int meetingsPerSeance = UsersInEvent.Count / 2;

            for (int i = 0; i < viewModel.AmountOfMeetings && possibleMeetings.Count != 0; i++)
            {
                //Create a meeting row (seance)
                List <MeetingScore> possibleMeetingsThisRow = possibleMeetings.Where(m => true).ToList();
                List <User>         usersNotInMeeting       = UsersInEvent.Where(u => true).ToList();
                DateTime            beginningTime           = viewModel.EventInformation.BeginningTime.AddMinutes(viewModel.MinuteInterval * i);
                Seance eventMeetingsRow = new Seance(viewModel.EventInformation, i, beginningTime, beginningTime.AddMinutes(viewModel.MinuteInterval));

                for (int j = 0; j < meetingsPerSeance; j++)
                {
                    if (possibleMeetingsThisRow.Count == 0)
                    {
                        //fill with already used meetings if turned on
                        if (viewModel.ForceFillMeetings && usersNotInMeeting.Count >= 2)
                        {
                            possibleMeetingsThisRow = MeetingScore.GetPossibleMeetings(usersNotInMeeting, viewModel.EventId);
                        }
                        else
                        {
                            //Create list of users not in meetings in this seance
                            foreach (User userNotInMeeting in usersNotInMeeting)
                            {
                                eventMeetingsRow.UserPauses.Add(new UserPause(userNotInMeeting, eventMeetingsRow));
                            }
                            break;
                        }
                    }

                    //add highest scored meeting to the event
                    MeetingScore meetingToAdd = possibleMeetingsThisRow[0];

                    //remove impossible meetings (meetings containing the users who just have been added to the meeting list)
                    eventMeetingsRow.Meetings.Add(new Meeting(meetingToAdd.Person1, meetingToAdd.Person2, eventMeetingsRow));
                    possibleMeetingsThisRow = possibleMeetingsThisRow.Where(m => m.Person1.Id != meetingToAdd.Person1.Id && m.Person2.Id != meetingToAdd.Person2.Id && m.Person1.Id != meetingToAdd.Person2.Id && m.Person2.Id != meetingToAdd.Person1.Id).ToList();
                    usersNotInMeeting       = usersNotInMeeting.Where(u => u.Id != meetingToAdd.Person1.Id && u.Id != meetingToAdd.Person2.Id).ToList();
                    possibleMeetings.Remove(meetingToAdd);
                }

                viewModel.EventInformation.Seances.Add(eventMeetingsRow);
            }

            model.SaveChanges();

            return(RedirectToAction("Page", new { Id = viewModel.EventId }));
        }