public void FlightMatrixGenerationInteractor_CreateSortedFlightMatrix_SortingAlgo_HappyPath()
        {
            var pilotRegistrations = GenerateValidPilotRegistration(10);

            var flightMatrix = new FlightMatrix
            {
                ContestId = "sdfasdf"
            };

            flightMatrix.Matrix.Add(new FlightMatrixRound
            {
                RoundOrdinal = 0,
                PilotSlots   = new List <FlightMatrixPilotSlot>
                {
                    new FlightMatrixPilotSlot
                    {
                        PilotId     = "sadfxcvcxasdf",
                        FlightGroup = FlightGroup.A
                    }
                }
            });

            this.mockSortingAlgo.Setup(sa => sa.GenerateInitialMatrix(It.IsAny <IEnumerable <PilotRegistration> >(), It.IsAny <int>(), It.IsAny <int>())).Returns(flightMatrix);

            var fmgi   = new FlightMatrixGenInteractor(mockSortingAlgo.Object, mockLogger.Object);
            var result = fmgi.CreateSortedFlightMatrix(pilotRegistrations, 1, 7);

            Assert.IsFalse(result.IsFaulted);
            Assert.IsNull(result.Error);
            Assert.IsNotNull(result.Value);
        }
        public void FlightMatrixGenerationInteractor_CreateSortedFlightMatrix_NullPilotRegistrationParameter()
        {
            var fmgi   = new FlightMatrixGenInteractor(mockSortingAlgo.Object, mockLogger.Object);
            var result = fmgi.CreateSortedFlightMatrix(null, 1, 7);

            Assert.IsTrue(result.IsFaulted);
            Assert.IsNotNull(result.Error);
            Assert.IsNotNull(result.Error.ErrorMessage);
        }
        public void FlightMatrixGenerationInteractor_CreateSortedFlightMatrix_NegativeSuggestionParameter()
        {
            var pilotRegistrations = GenerateValidPilotRegistration(10);
            var fmgi   = new FlightMatrixGenInteractor(mockSortingAlgo.Object, mockLogger.Object);
            var result = fmgi.CreateSortedFlightMatrix(pilotRegistrations, 1, -7);

            Assert.IsTrue(result.IsFaulted);
            Assert.IsNotNull(result.Error);
            Assert.IsNotNull(result.Error.ErrorMessage);
        }
        public void FlightMatrixGenerationInteractor_CreateSortedFlightMatrix_SortingAlgo_ReturnsNull()
        {
            var pilotRegistrations = GenerateValidPilotRegistration(10);

            this.mockSortingAlgo.Setup(sa => sa.GenerateInitialMatrix(It.IsAny <IEnumerable <PilotRegistration> >(), It.IsAny <int>(), It.IsAny <int>())).Returns <FlightMatrix>(null);

            var fmgi   = new FlightMatrixGenInteractor(mockSortingAlgo.Object, mockLogger.Object);
            var result = fmgi.CreateSortedFlightMatrix(pilotRegistrations, 1, 7);

            Assert.IsTrue(result.IsFaulted);
            Assert.IsNotNull(result.Error);
            Assert.IsNotNull(result.Error.ErrorMessage);
        }
        public void FlightMatrixGenerationInteractor_CreateSortedFlightMatrix_SortingAlgo_Exception()
        {
            var pilotRegistrations = GenerateValidPilotRegistration(10);

            this.mockSortingAlgo.Setup(sa => sa.GenerateInitialMatrix(It.IsAny <IEnumerable <PilotRegistration> >(), It.IsAny <int>(), It.IsAny <int>())).Throws(new ArgumentException("test"));

            var fmgi   = new FlightMatrixGenInteractor(mockSortingAlgo.Object, mockLogger.Object);
            var result = fmgi.CreateSortedFlightMatrix(pilotRegistrations, 1, 7);

            Assert.IsTrue(result.IsFaulted);
            Assert.IsNotNull(result.Error);
            Assert.IsInstanceOfType(result.Error.Exception, typeof(ArgumentException));
            Assert.IsNotNull(result.Error.ErrorMessage);
            Assert.AreEqual("test", result.Error.ErrorMessage);
        }
        /// <summary>
        /// Inflates the matrix.
        /// </summary>
        /// <param name="pilotMatrix">The pilot matrix.</param>
        private async Task InflateMatrix(FlightMatrix pilotMatrix)
        {
            var flightMatrixGenerationIntr = new FlightMatrixGenInteractor(App.SortingAlgos.Where(algo => algo.GetUniqueId() == this.contest.SortingAlgoId).Single(), App.Logger);

            // Sort by pilot
            var pilotSortedMatrixResult = await this.flightMatrixQueryIntr.Value.GetPilotSortedFlightMatrix(this.contest.Id);

            if (pilotSortedMatrixResult.IsFaulted)
            {
                base.Alert($"{nameof(FlightMatrixPageViewModel)}:{nameof(InflateMatrix)} - Failed to pilot sort the matrix.");
                return;
            }

            // Get all of the pilots
            var allPilotsResult = await this.pilotQueryIntr.Value.GetAllPilotsAsync();

            if (allPilotsResult.IsFaulted)
            {
                base.Alert($"{nameof(FlightMatrixPageViewModel)}:{nameof(InflateMatrix)} - Failed to get all pilots.");
                return;
            }

            // Clear the Pilots collection on the UI thread...
            await this.Dispatcher.DispatchAsync(new Action(() => this.Pilots.Clear()));

            // Populate the View models
            foreach (var pilotSchedule in pilotSortedMatrixResult.Value)
            {
                var fullPilotObj = allPilotsResult.Value.Where(p => p.Id == pilotSchedule.PilotId).FirstOrDefault();
                if (fullPilotObj == null)
                {
                    base.Throw(new Exception($"Could not find pilotId:{pilotSchedule.PilotId} in the system."));
                }

                // Update the pilots collection on the UI thread.
                await this.Dispatcher.DispatchAsync(new Action(() =>
                {
                    // Create a view model for each pilot schedule
                    this.Pilots.Add(new PilotRoundMatrixListItemViewModel
                    {
                        PilotName = $"{fullPilotObj.FirstName} {fullPilotObj.LastName}",
                        FlightGroups = new ObservableCollection <string>(pilotSchedule.FlightGroupDraw.Select(flightGroup => flightGroup.ToString()))
                    });
                }));
            }

            this.matrix = pilotMatrix;
        }
        /// <summary>
        /// Handles a click on the Generate matrix button.
        /// </summary>
        /// <returns></returns>
        public async Task GenerateMatrix()
        {
            var contestRegistrationsResult = await this.registrationQueryIntr.Value.GetPilotRegistrationsForContest(this.contest.Id);

            if (contestRegistrationsResult.IsFaulted || contestRegistrationsResult.Value == null)
            {
                return;
            }

            try
            {
                var flightMatrixGenerationIntr = new FlightMatrixGenInteractor(
                    App.SortingAlgos.Where(algo => algo.GetUniqueId() == this.contest.SortingAlgoId).Single(),
                    App.Logger);

                var sortedMatrixResult = flightMatrixGenerationIntr.CreateSortedFlightMatrix(
                    contestRegistrationsResult.Value,
                    // Don't include the fly-off rounds
                    this.contest.Rounds.Where(r => !r.Value.IsFlyOffRound).Count(),
                    this.contest.SuggestedNumberOfPilotsPerGroup);

                if (sortedMatrixResult.IsFaulted || sortedMatrixResult.Value == null)
                {
                    return;
                }

                this.matrix = sortedMatrixResult.Value;

                // Assign it to the current contest.
                this.matrix.ContestId = this.contest.Id;

                // Save it
                var flightMatrixSaveResult = await this.flightMatrixCmdIntr.Value.SaveFlightMatrixForContestAsync(this.matrix);

                if (flightMatrixSaveResult.IsFaulted)
                {
                    base.Alert($"{nameof(FlightMatrixPageViewModel)}:{nameof(GenerateMatrix)} - Failed to save the flight matrix.");
                    return;
                }

                await InflateMatrix(sortedMatrixResult.Value);
            }
            catch (Exception ex)
            {
                App.Logger.LogException(ex);
            }
        }