sharee.bike-App/TINKLib/Model/State/StateInfo.cs

176 lines
4.4 KiB
C#
Raw Normal View History

2023-05-11 17:39:28 +02:00
using System;
2021-05-13 20:03:07 +02:00
namespace TINK.Model.State
{
2022-09-06 16:08:19 +02:00
/// <summary>
/// Types of rent states
/// </summary>
public enum InUseStateEnum
{
/// <summary>
/// Bike was returned but no feedback given.
/// This applies to COPRI locks only because returning of bike is not done using app (Status: Supported locks are ILockIt and COPRI).
/// </summary>
FeedbackPending,
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Bike is not in use. Corresponding COPRI state is "available".
/// </summary>
Disposable,
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Bike is reserved. Corresponding COPRI state is "requested".
/// </summary>
Reserved,
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
2023-05-11 17:39:28 +02:00
/// Bike is booked. Corresponding COPRI state is "occupied".
2022-09-06 16:08:19 +02:00
/// </summary>
Booked
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Manages the state of a bike.
/// </summary>
public class StateInfo : IStateInfo
{
// Holds the current disposable state value.
private readonly BaseState _InUseState;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Constructs a state info object when state is available.
/// </summary>
2023-05-11 17:39:28 +02:00
/// <param name="isFeedbackPending">Specifies whether feedback is pending or not.</param>
2022-09-06 16:08:19 +02:00
public StateInfo(bool isFeedbackPending = false)
{
_InUseState = isFeedbackPending
? (BaseState)new StateFeedbackPendingInfo()
: new StateAvailableInfo();
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Constructs a state info object when state is requested.
/// </summary>
/// <param name="requestedAt">Date time when bike was requested</param>
2023-06-06 12:00:24 +02:00
/// <param name="maxReservationTimeSpan">Time span for which a bike can be reserved.</param>
2022-09-06 16:08:19 +02:00
/// <param name="mailAddress">Mail address of user which requested bike.</param>
/// <param name="code">Booking code.</param>
2023-05-11 17:39:28 +02:00
/// <param name="dateTimeNowProvider">Date time provider to calculate remaining time.</param>
2022-09-06 16:08:19 +02:00
public StateInfo(
Func<DateTime> dateTimeNowProvider,
DateTime requestedAt,
2023-06-06 12:00:24 +02:00
TimeSpan maxReservationTimeSpan,
2022-09-06 16:08:19 +02:00
string mailAddress,
string code)
{
_InUseState = new StateRequestedInfo(
dateTimeNowProvider ?? (() => DateTime.Now),
requestedAt,
2023-06-06 12:00:24 +02:00
maxReservationTimeSpan,
2022-09-06 16:08:19 +02:00
mailAddress,
code);
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Constructs a state info object when state is booked.
/// </summary>
/// <param name="p_oBookedAt">Date time when bike was booked</param>
/// <param name="p_strMailAddress">Mail address of user which booked bike.</param>
/// <param name="p_strCode">Booking code.</param>
public StateInfo(
DateTime p_oBookedAt,
string p_strMailAddress,
string p_strCode)
{
_InUseState = new StateOccupiedInfo(
p_oBookedAt,
p_strMailAddress,
p_strCode);
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Gets the state value of object.
/// </summary>
public InUseStateEnum Value
{
get { return _InUseState.Value; }
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Member for serialization purposes.
/// </summary>
internal BaseState StateInfoObject
{
get { return _InUseState; }
}
/// Transforms object to string.
/// </summary>
/// <returns></returns>
public new string ToString()
{
return _InUseState.Value.ToString("g");
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
2023-06-06 12:00:24 +02:00
/// Date of request/ booking action.
2022-09-06 16:08:19 +02:00
/// </summary>
public DateTime? From
{
get
{
var l_oNotDisposableInfo = _InUseState as INotAvailableState;
return l_oNotDisposableInfo != null ? l_oNotDisposableInfo.From : (DateTime?)null;
}
}
2021-05-13 20:03:07 +02:00
2023-06-06 12:00:24 +02:00
/// <summary>
/// Time span for which a bike can be reserved.
/// </summary>
public TimeSpan? MaxReservationTimeSpan =>
_InUseState is StateRequestedInfo reserved
? reserved.MaxReservationTimeSpan
: (TimeSpan?)null;
2022-09-06 16:08:19 +02:00
/// <summary>
/// Mail address.
/// </summary>
public string MailAddress
{
get
{
var l_oNotDisposableInfo = _InUseState as INotAvailableState;
return l_oNotDisposableInfo?.MailAddress;
}
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Reservation code.
/// </summary>
public string Code
{
get
{
var l_oNotDisposableInfo = _InUseState as INotAvailableState;
return l_oNotDisposableInfo?.Code;
}
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Tries update
/// </summary>
2023-06-06 12:00:24 +02:00
/// <returns>True if reservation span has not exceeded and state remains reserved, false otherwise.</returns>
2022-09-06 16:08:19 +02:00
/// <todo>Implement logging of time stamps.</todo>
public bool GetIsStillReserved(out TimeSpan? p_oRemainingTime)
{
var l_oReservedInfo = _InUseState as StateRequestedInfo;
if (l_oReservedInfo == null)
{
p_oRemainingTime = null;
return false;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
return l_oReservedInfo.GetIsStillReserved(out p_oRemainingTime);
}
}
2021-05-13 20:03:07 +02:00
}