using System; using Serilog; using System.Threading.Tasks; using TINK.Model.Connector; using TINK.Model.State; using TINK.View; using TINK.Repository.Exception; using TINK.Services.Geolocation; using TINK.MultilingualResources; using TINK.Model.User; using TINK.Model.Device; using TINK.Model.Bikes.Bike.CopriLock; namespace TINK.ViewModel.Bikes.Bike.CopriLock.RequestHandler { using IRequestHandler = BluetoothLock.IRequestHandler; public class DisposabledAway : Base, IRequestHandler { /// Provides info about the smart device (phone, tablet, ...). /// View model to be used for progress report and unlocking/ locking view. public DisposabledAway( IBikeInfoMutable selectedBike, Func isConnectedDelegate, Func connectorFactory, IGeolocation geolocation, Func viewUpdateManager, ISmartDevice smartDevice, IViewService viewService, IBikesViewModel bikesViewModel, IUser activeUser) : base( selectedBike, AppResources.ActionRequest, // Copri text: "Rad reservieren" true, // Show copri button to enable reserving and opening isConnectedDelegate, connectorFactory, geolocation, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser) { LockitButtonText = GetType().Name; IsLockitButtonVisible = false; // If bike is not reserved/ booked app can not connect to lock } /// Gets the bike state. public override InUseStateEnum State => InUseStateEnum.Disposable; /// Reserve bike and connect to lock. public async Task HandleRequestOption1() => await Reserve(); public async Task HandleRequestOption2() => await UnsupportedRequest(); /// Reserve bike and connect to lock. public async Task Reserve() { BikesViewModel.IsIdle = false; // Ask whether to really reserve bike? var alertResult = await ViewService.DisplayAlert( string.Empty, string.Format(AppResources.QuestionReserveBike, SelectedBike.GetFullDisplayName(), StateRequestedInfo.MaximumReserveTime.Minutes), AppResources.MessageAnswerYes, AppResources.MessageAnswerNo); if (alertResult == false) { // User aborted booking process Log.ForContext().Information("User selected availalbe bike {bike} in order to reserve but action was canceled.", SelectedBike); BikesViewModel.IsIdle = true; return this; } // Lock list to avoid multiple taps while copri action is pending. Log.ForContext().Information("Request to book and open lock for bike {bike} detected.", SelectedBike); BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease; // Stop polling before requesting bike. await ViewUpdateManager().StopUpdatePeridically(); BikesViewModel.ActionText = AppResources.ActivityTextReservingBike; IsConnected = IsConnectedDelegate(); try { await ConnectorFactory(IsConnected).Command.DoReserve(SelectedBike); } catch (Exception exception) { BikesViewModel.ActionText = string.Empty; if (exception is BookingDeclinedException) { // Too many bikes booked. Log.ForContext().Information("Request declined because maximum count of bikes {l_oException.MaxBikesCount} already requested/ booked.", (exception as BookingDeclinedException).MaxBikesCount); await ViewService.DisplayAlert( AppResources.MessageTitleHint, string.Format(AppResources.MessageReservationBikeErrorTooManyReservationsRentals, SelectedBike.Id, (exception as BookingDeclinedException).MaxBikesCount), AppResources.MessageAnswerOk); } else if (exception is WebConnectFailureException) { // Copri server is not reachable. Log.ForContext().Information("User selected availalbe bike {bike} but reserving failed (Copri server not reachable).", SelectedBike); await ViewService.DisplayAlert( "Verbingungsfehler beim Reservieren des Rads!", string.Format("{0}\r\n{1}", exception.Message, WebConnectFailureException.GetHintToPossibleExceptionsReasons), "OK"); } else { Log.ForContext().Error("User selected availalbe bike {bike} but reserving failed. {@l_oException}", SelectedBike, exception); await ViewService.DisplayAlert( "Fehler beim Reservieren des Rads!", exception.Message, "OK"); } // Restart polling again. BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater; await ViewUpdateManager().StartUpdateAyncPeridically(); BikesViewModel.ActionText = string.Empty; BikesViewModel.IsIdle = true; return this; } Log.ForContext().Information("User reserved bike {bike} successfully.", SelectedBike); BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater; await ViewUpdateManager().StartUpdateAyncPeridically(); // Restart polling again. BikesViewModel.ActionText = string.Empty; BikesViewModel.IsIdle = true; // Unlock GUI return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser); } /// Requst 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); } } }