sharee.bike-App/TINKLib/Model/State/StateInfo.cs
2023-06-06 12:05:48 +02:00

175 lines
4.4 KiB
C#

using System;
namespace TINK.Model.State
{
/// <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,
/// <summary>
/// Bike is not in use. Corresponding COPRI state is "available".
/// </summary>
Disposable,
/// <summary>
/// Bike is reserved. Corresponding COPRI state is "requested".
/// </summary>
Reserved,
/// <summary>
/// Bike is booked. Corresponding COPRI state is "occupied".
/// </summary>
Booked
}
/// <summary>
/// Manages the state of a bike.
/// </summary>
public class StateInfo : IStateInfo
{
// Holds the current disposable state value.
private readonly BaseState _InUseState;
/// <summary>
/// Constructs a state info object when state is available.
/// </summary>
/// <param name="isFeedbackPending">Specifies whether feedback is pending or not.</param>
public StateInfo(bool isFeedbackPending = false)
{
_InUseState = isFeedbackPending
? (BaseState)new StateFeedbackPendingInfo()
: new StateAvailableInfo();
}
/// <summary>
/// Constructs a state info object when state is requested.
/// </summary>
/// <param name="requestedAt">Date time when bike was requested</param>
/// <param name="maxReservationTimeSpan">Time span for which a bike can be reserved.</param>
/// <param name="mailAddress">Mail address of user which requested bike.</param>
/// <param name="code">Booking code.</param>
/// <param name="dateTimeNowProvider">Date time provider to calculate remaining time.</param>
public StateInfo(
Func<DateTime> dateTimeNowProvider,
DateTime requestedAt,
TimeSpan maxReservationTimeSpan,
string mailAddress,
string code)
{
_InUseState = new StateRequestedInfo(
dateTimeNowProvider ?? (() => DateTime.Now),
requestedAt,
maxReservationTimeSpan,
mailAddress,
code);
}
/// <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);
}
/// <summary>
/// Gets the state value of object.
/// </summary>
public InUseStateEnum Value
{
get { return _InUseState.Value; }
}
/// <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");
}
/// <summary>
/// Date of request/ booking action.
/// </summary>
public DateTime? From
{
get
{
var l_oNotDisposableInfo = _InUseState as INotAvailableState;
return l_oNotDisposableInfo != null ? l_oNotDisposableInfo.From : (DateTime?)null;
}
}
/// <summary>
/// Time span for which a bike can be reserved.
/// </summary>
public TimeSpan? MaxReservationTimeSpan =>
_InUseState is StateRequestedInfo reserved
? reserved.MaxReservationTimeSpan
: (TimeSpan?)null;
/// <summary>
/// Mail address.
/// </summary>
public string MailAddress
{
get
{
var l_oNotDisposableInfo = _InUseState as INotAvailableState;
return l_oNotDisposableInfo?.MailAddress;
}
}
/// <summary>
/// Reservation code.
/// </summary>
public string Code
{
get
{
var l_oNotDisposableInfo = _InUseState as INotAvailableState;
return l_oNotDisposableInfo?.Code;
}
}
/// <summary>
/// Tries update
/// </summary>
/// <returns>True if reservation span has not exceeded and state remains reserved, false otherwise.</returns>
/// <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;
}
return l_oReservedInfo.GetIsStillReserved(out p_oRemainingTime);
}
}
}