public ManualPickViewModel(FileOpenCommands fileCommands, Func <Task <IFlightAcademyClient> > clientFactory,
                                   IDialogService dialogService, SettingsViewModel settingsViewModel, INavigationService navigationService)
        {
            this.settingsViewModel = settingsViewModel;
            FileCommands           = fileCommands;

            video      = fileCommands.OpenVideoCommand.ToProperty(this, x => x.Video);
            flightFile = fileCommands.OpenDataCommand.ToProperty(this, x => x.FlightFile);

            LoadFlightsCommand = ReactiveCommand.CreateFromTask(() => GetFlights(clientFactory), settingsViewModel.IsAccountConfigured);
            summaries          = LoadFlightsCommand
                                 .ToProperty(this, x => x.FlightSummaries);

            MessageBus.Current
            .Listen <Unit>("LoadData")
            .InvokeCommand(LoadFlightsCommand);

            LoadFlightsCommand.ThrownExceptions.MessageOnException(dialogService);

            var manualSimulations = this
                                    .WhenAnyValue(x => x.Video, x => x.FlightFile, (v, f) => new { Video = v, FlightFile = f })
                                    .SelectMany(async x =>
            {
                var readFlight = await x.FlightFile.ReadFlight();
                return(new Simulation(x.Video, readFlight, new PresentationOptions()
                {
                    UnitPack = settingsViewModel.UnitPack,
                    Dashboard = settingsViewModel.VirtualDashboard,
                }));
            })
                                    .FirstOrDefaultAsync();

            PlayFromFileCommand = ReactiveCommand.CreateFromObservable(() => manualSimulations,
                                                                       this.WhenAnyValue(x => x.FlightFile, x => x.Video, (f, v) => f != null && v != null));

            var onlineSimulations = this
                                    .WhenAnyValue(x => x.Video, x => x.SelectedFlightSummary, (v, s) => new { Video = v, FlightSummary = s })
                                    .SelectMany(async x =>
            {
                var readFlight = await(await clientFactory()).GetFlight(x.FlightSummary.Id);
                return(new Simulation(x.Video, readFlight.ToFlight(), new PresentationOptions()
                {
                    UnitPack = settingsViewModel.UnitPack,
                    Dashboard = settingsViewModel.VirtualDashboard,
                }));
            })
                                    .FirstOrDefaultAsync();

            PlayFromOnlineFlightCommand = ReactiveCommand.CreateFromObservable(() => onlineSimulations,
                                                                               this.WhenAnyValue(x => x.SelectedFlightSummary, x => x.Video, (f, v) => f != null && v != null));

            PlayFromFileCommand.Subscribe(simulation => { PlaySimulation(navigationService, simulation); });
            PlayFromOnlineFlightCommand.Subscribe(simulation => { PlaySimulation(navigationService, simulation); });

            isAccountConfigured = settingsViewModel.IsAccountConfigured.ToProperty(this, x => x.IsAccountConfigured);
        }
        public SettingsViewModel(FileOpenCommands commands, IDialogService dialogService, IVirtualDashboardsRepository virtualDashboardsRepository)
        {
            this.virtualDashboardsRepository = virtualDashboardsRepository;
            settings            = new SettingsSaver(this, ApplicationData.Current.RoamingSettings);
            BrowseFolderCommand = commands.BrowseFolderCommand;
            BrowseFolderCommand.Subscribe(x =>
            {
                if (VideoFolderToken != null)
                {
                    StorageApplicationPermissions.FutureAccessList.Clear();
                }

                var token        = StorageApplicationPermissions.FutureAccessList.Add(x);
                VideoFolder      = x.Path;
                VideoFolderToken = token;
            });

            videoFolder         = BrowseFolderCommand.ToProperty(this, x => x.Folder);
            IsAccountConfigured = this.WhenAnyValue(x => x.Username, x => x.Password,
                                                    (u, p) => new[] { u, p }.All(s => !string.IsNullOrEmpty(s)));

            IsVideoFolderFolderConfigured = this.WhenAnyValue(x => x.VideoFolder, x => !string.IsNullOrEmpty(x));

            UnitPack = UnitPacks.FirstOrDefault(pack => pack.Id == StringUnitPack) ?? UnitPacks.First();
            this.WhenAnyValue(x => x.UnitPack).Subscribe(x => StringUnitPack = UnitPack.Id);

            RemoveFolderCommand = ReactiveCommand.Create(() =>
            {
                StorageApplicationPermissions.FutureAccessList.Clear();

                VideoFolder      = null;
                VideoFolderToken = null;
            }, this.WhenAnyValue(x => x.VideoFolderToken, selector: s => s != null));

            UserBasedLogin  = new UserBasedLogin();
            EmailBasedLogin = new EmailBasedLogin(dialogService);

            var usernamesSequence = GetUsernamesSequence();
            var passwordSequence  = GetPasswordsSequence();


            username = usernamesSequence.ToProperty(this, x => x.Username);
            password = passwordSequence.ToProperty(this, x => x.Password);

            dashboardPreview =
                this.WhenAnyValue(x => x.VirtualDashboard, x => x.UnitPack,
                                  (vd, up) => CreatePreviewViewModel(up, vd)).ToProperty(this, x => x.DashboardPreview);
        }