using System;
using System.Threading.Tasks;
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 CloseCommand = ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.Command.CloseCommand;
using static ShareeBike.ViewModel.Bikes.Bike.BluetoothLock.RequestHandlerFactory;
namespace ShareeBike.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
{
/// Bike is disposable, lock is open and connected to app.
///
/// This state can not be occur because
/// - app does not allow to return bike/ cancel reservation when lock is not closed
/// - as long as app is connected to lock
/// - lock can not be opened manually
/// - no other device can access lock
///
public class DisposableOpen : Base, IRequestHandler, StartReservationCommand.IStartReservationCommandListener, CloseCommand.ICloseCommandListener
{
/// Bike is disposable, lock is open and can be reached via bluetooth.
///
/// This state should never occur because as long as a ILOCKIT is connected it
/// - cannot be closed manually
/// - no other device can access lock
/// - app itself should never event attempt to open a lock which is not rented.
///
/// Provides info about the smart device (phone, tablet, ...)
/// View model to be used for progress report and unlocking/ locking view.
public DisposableOpen(
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 = AppResources.ActionCloseLock; // Close Lock
IsLockitButtonVisible = true; // Show button
_startReservationOrRentalActionViewModel = new StartReservationOrRentalActionViewModel(
selectedBike,
viewUpdateManager,
viewService,
bikesViewModel);
_closeLockActionViewModel = new CloseLockActionViewModel(
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);
///
/// Holds the view model for close action.
///
private readonly CloseLockActionViewModel _closeLockActionViewModel;
///
/// Processes the close lock progress.
///
///
/// Only used for testing.
///
/// Current step to process.
public void ReportStep(CloseCommand.Step step) => _closeLockActionViewModel?.ReportStep(step);
///
/// Processes the close lock state.
///
///
/// Only used for testing.
///
/// State to process.
/// Textual details describing current state.
public async Task ReportStateAsync(CloseCommand.State state, string details) => await _closeLockActionViewModel.ReportStateAsync(state, details);
/// Rents bike by reserving bike and renting bike.
/// Next request handler.
public async Task HandleRequestOption1()
{
await _startReservationOrRentalActionViewModel.StartReservationOrRentalAsync();
return Create(
SelectedBike,
IsConnectedDelegate,
ConnectorFactory,
GeolocationService,
LockService,
ViewUpdateManager,
SmartDevice,
ViewService,
BikesViewModel,
ActiveUser);
}
/// Closes Lock.
/// Next request handler.
public async Task HandleRequestOption2()
{
await _closeLockActionViewModel.CloseLockAsync();
return Create(
SelectedBike,
IsConnectedDelegate,
ConnectorFactory,
GeolocationService,
LockService,
ViewUpdateManager,
SmartDevice,
ViewService,
BikesViewModel,
ActiveUser);
}
}
}