using System; using System.Threading.Tasks; using Serilog; using ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command; using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock; using ShareeBike.Model.Connector; using ShareeBike.Model.Device; using ShareeBike.Model.User; using ShareeBike.MultilingualResources; using ShareeBike.Services.BluetoothLock; using ShareeBike.Services.Geolocation; using ShareeBike.View; using StartReservationCommand = ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command.StartReservationCommand; using static ShareeBike.ViewModel.Bikes.Bike.BluetoothLock.RequestHandlerFactory; namespace ShareeBike.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler { public class DisposableDisconnected : Base, IRequestHandler, StartReservationCommand.IStartReservationCommandListener { /// Provides info about the smart device (phone, tablet, ...). /// View model to be used for progress report and unlocking/ locking view. public DisposableDisconnected( IBikeInfoMutable selectedBike, Func isConnectedDelegate, Func connectorFactory, IGeolocationService geolocation, ILocksService lockService, Func viewUpdateManager, ISmartDevice smartDevice, IViewService viewService, IBikesViewModel bikesViewModel, IUser activeUser) : base( selectedBike, AppResources.ActionRequestBike, // Reserve / Rent Bike true, // Show button isConnectedDelegate, connectorFactory, geolocation, lockService, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser) { LockitButtonText = GetType().Name; IsLockitButtonVisible = false; _startReservationOrRentalActionViewModel = new StartReservationOrRentalActionViewModel( selectedBike, viewUpdateManager, viewService, bikesViewModel); } /// /// Holds the view model for requesting a bike action. /// private readonly StartReservationOrRentalActionViewModel _startReservationOrRentalActionViewModel; /// /// Processes the reserving a bike progress. /// /// /// Only used for testing. /// /// Current step to process. public void ReportStep(StartReservationCommand.Step step) => _startReservationOrRentalActionViewModel?.ReportStep(step); /// /// Processes the reserving a bike state. /// /// /// Only used for testing. /// /// State to process. /// Textual details describing current state. public async Task ReportStateAsync(StartReservationCommand.State state, string details) => await _startReservationOrRentalActionViewModel.ReportStateAsync(state, details); /// Reserve bike, connect to lock, open lock and rent bike. public async Task HandleRequestOption1() { await _startReservationOrRentalActionViewModel.StartReservationOrRentalAsync(); if (_startReservationOrRentalActionViewModel.ContinueWithOpenLock == false) { return Create( SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser); } var _openLockActionViewModel = new OpenLockActionViewModel( SelectedBike, ViewUpdateManager, ViewService, BikesViewModel); await _openLockActionViewModel.OpenLockAsync(); return Create( SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser); } public async Task HandleRequestOption2() => await UnsupportedRequest(); /// Request is not supported, button should be disabled. /// public async Task UnsupportedRequest() { Log.ForContext().Error("Click of unsupported button click detected."); return await Task.FromResult(this); } } }