using System; using TINK.Model.Connector; using TINK.Services.Geolocation; using TINK.View; using TINK.ViewModel.Bikes.Bike.CopriLock.RequestHandler; using TINK.Model.User; using Serilog; using TINK.Model.Device; using TINK.Model.Bike.CopriLock; using TINK.Model.Bikes.Bike.CopriLock; namespace TINK.ViewModel.Bikes.Bike.CopriLock { public static class RequestHandlerFactory { /// Creates a request handler. /// /// /// /// /// /// Provides info about the smart device (phone, tablet, ...) /// /// View model to be used for progress report and unlocking/ locking view. /// Request handler. public static BluetoothLock.IRequestHandler Create( Model.Bikes.Bike.BC.IBikeInfoMutable selectedBike, Func isConnectedDelegate, Func connectorFactory, IGeolocation geolocation, Func viewUpdateManager, ISmartDevice smartDevice, IViewService viewService, IBikesViewModel bikesViewModel, IUser activeUser) { if (!(selectedBike is BikeInfoMutable selectedBluetoothLockBike)) return null; switch (selectedBluetoothLockBike.State.Value) { case Model.State.InUseStateEnum.Disposable: // Bike is reserved, selecte action depending on lock state. switch (selectedBluetoothLockBike.LockInfo.State) { case LockingState.Closed: // Unexepected state detected. // This state is unexpected because connection is closed // - when reservation is canceled or // - when bike is returned. Log.Error("Unexpected state {BookingState}/ {LockingState} detected.", selectedBluetoothLockBike.State.Value, selectedBluetoothLockBike.LockInfo.State); return new DisposableClosed( selectedBluetoothLockBike, isConnectedDelegate, connectorFactory, geolocation, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser); default: return new DisposabledAway( selectedBluetoothLockBike, isConnectedDelegate, connectorFactory, geolocation, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser); } case Model.State.InUseStateEnum.Reserved: // Bike is reserved, selecte action depending on lock state. switch (selectedBluetoothLockBike.LockInfo.State) { case LockingState.Closed: // Lock could not be opened after reserving bike. return new ReservedClosed( selectedBluetoothLockBike, isConnectedDelegate, connectorFactory, geolocation, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser); default: return new ReservedClosed( selectedBluetoothLockBike, isConnectedDelegate, connectorFactory, geolocation, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser); } case Model.State.InUseStateEnum.Booked: // Bike is booked, selecte action depending on lock state. switch (selectedBluetoothLockBike.LockInfo.State) { case LockingState.Closed: // Ride was paused. return new BookedClosed( selectedBluetoothLockBike, isConnectedDelegate, connectorFactory, geolocation, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser); case LockingState.Open: // User wants to return bike/ pause ride. return new BookedOpen( selectedBluetoothLockBike, isConnectedDelegate, connectorFactory, geolocation, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser); default: return new BookedDefault( selectedBluetoothLockBike, isConnectedDelegate, connectorFactory, geolocation, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser); } default: // Unexpected copri state detected. Log.Error("Unexpected locking {BookingState}/ {LockingState} detected.", selectedBluetoothLockBike.State.Value, selectedBluetoothLockBike.LockInfo.State); return new DisposabledAway( selectedBluetoothLockBike, isConnectedDelegate, connectorFactory, geolocation, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser); } } } }