2023-06-06 12:00:24 +02:00
using System ;
2022-08-30 15:42:25 +02:00
using Serilog ;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model.Bikes.BikeInfoNS.CopriLock ;
using ShareeBike.Model.Connector ;
using ShareeBike.Model.Device ;
using ShareeBike.Model.User ;
using ShareeBike.View ;
using ShareeBike.ViewModel.Bikes.Bike.CopriLock.RequestHandler ;
2022-04-25 22:15:15 +02:00
2024-04-09 12:53:23 +02:00
namespace ShareeBike.ViewModel.Bikes.Bike.CopriLock
2022-04-25 22:15:15 +02:00
{
2022-09-06 16:08:19 +02:00
public static class RequestHandlerFactory
{
/// <summary> Creates a request handler.</summary>
/// <param name="selectedBike"></param>
/// <param name="isConnectedDelegate"></param>
/// <param name="connectorFactory"></param>
/// <param name="bikeRemoveDelegate"></param>
/// <param name="viewUpdateManager"></param>
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...)</param>
/// <param name="viewService"></param>
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
/// <returns>Request handler.</returns>
public static BluetoothLock . IRequestHandler Create (
Model . Bikes . BikeInfoNS . BC . IBikeInfoMutable selectedBike ,
Func < bool > isConnectedDelegate ,
Func < bool , IConnector > connectorFactory ,
Func < IPollingUpdateTaskManager > viewUpdateManager ,
ISmartDevice smartDevice ,
IViewService viewService ,
IBikesViewModel bikesViewModel ,
IUser activeUser )
{
if ( ! ( selectedBike is IBikeInfoMutable selectedCopriLock ) )
{
Log . Error ( "Bike of unexpected type {type} detected." , selectedBike ? . GetType ( ) ) ;
return null ;
}
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
switch ( selectedCopriLock . State . Value )
{
// User has rented this bike before. Bike is now available but feedback was not yet given.
case Model . State . InUseStateEnum . FeedbackPending :
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
return new FeedbackPending (
selectedCopriLock ,
isConnectedDelegate ,
connectorFactory ,
viewUpdateManager ,
smartDevice ,
viewService ,
bikesViewModel ,
activeUser ) ;
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
case Model . State . InUseStateEnum . Disposable :
2022-04-25 22:15:15 +02:00
2023-06-06 12:00:24 +02:00
// Bike is reserved, selected action depending on lock state.
2022-09-06 16:08:19 +02:00
return new DisposableClosed (
selectedCopriLock ,
isConnectedDelegate ,
connectorFactory ,
viewUpdateManager ,
smartDevice ,
viewService ,
bikesViewModel ,
activeUser ) ;
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
case Model . State . InUseStateEnum . Reserved :
// Lock could not be opened after reserving bike.
return new ReservedClosed (
selectedCopriLock ,
isConnectedDelegate ,
connectorFactory ,
viewUpdateManager ,
smartDevice ,
viewService ,
bikesViewModel ,
activeUser ) ;
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
case Model . State . InUseStateEnum . Booked :
2022-04-25 22:15:15 +02:00
2023-06-06 12:00:24 +02:00
// Bike is booked, selected action depending on lock state.
2022-09-06 16:08:19 +02:00
var lockState = selectedCopriLock . LockInfo . State ;
switch ( lockState )
{
case LockingState . Closed :
// Frame lock is closed. User paused ride and closed lock.
return new BookedClosed (
selectedCopriLock ,
isConnectedDelegate ,
connectorFactory ,
viewUpdateManager ,
smartDevice ,
viewService ,
bikesViewModel ,
activeUser ) ;
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
default :
if ( lockState ! = LockingState . Open )
Log . Error ( "Unexpected locking {LockingState} of a rented bike with copri lock bike detected." , selectedCopriLock . LockInfo . State ) ;
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
return new BookedOpen (
selectedCopriLock ,
isConnectedDelegate ,
connectorFactory ,
viewUpdateManager ,
smartDevice ,
viewService ,
bikesViewModel ,
activeUser ) ;
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
}
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
default :
// Unexpected copri state detected.
Log . Error ( "Unexpected locking {BookingState}/ {LockingState} of a copri lock bike detected." , selectedCopriLock . State . Value , selectedCopriLock . LockInfo . State ) ;
return new DisposableClosed (
selectedCopriLock ,
isConnectedDelegate ,
connectorFactory ,
viewUpdateManager ,
smartDevice ,
viewService ,
bikesViewModel ,
activeUser ) ;
}
}
}
2022-04-25 22:15:15 +02:00
}