sharee.bike-App/TINKLib/Model/State/StateInfo.cs
2021-05-13 20:03:07 +02:00

161 lines
5.1 KiB
C#

using System;
namespace TINK.Model.State
{
/// <summary>
/// Types of rent states
/// </summary>
public enum InUseStateEnum
{
/// <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 statie 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 m_oInUseState;
/// <summary>
/// Constructs a state info object when state is available.
/// </summary>
/// <param name="p_oDateTimeNowProvider">Provider for current date time to calculate remainig time on demand for state of type reserved.</param>
public StateInfo()
{
m_oInUseState = new StateAvailableInfo();
}
/// <summary>
/// Constructs a state info object when state is requested.
/// </summary>
/// <param name="p_oRequestedAt">Date time when bike was requested</param>
/// <param name="p_strMailAddress">Mail address of user which requested bike.</param>
/// <param name="p_strCode">Booking code.</param>
/// <param name="p_oDateTimeNowProvider">Date time provider to calculate reaining time.</param>
public StateInfo(
Func<DateTime> p_oDateTimeNowProvider,
DateTime p_oRequestedAt,
string p_strMailAddress,
string p_strCode)
{
// Todo: Handle p_oFrom == null here.
// Todo: Handle p_oDuration == null here.
m_oInUseState = new StateRequestedInfo(
p_oDateTimeNowProvider ?? (() => DateTime.Now),
p_oRequestedAt,
p_strMailAddress,
p_strCode);
}
/// <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)
{
// Todo: Handle p_oFrom == null here.
// Todo: Clearify question: What to do if code changes form one value to another? This should never happen.
// Todo: Clearify question: What to do if from time changes form one value to another? This should never happen.
m_oInUseState = new StateOccupiedInfo(
p_oBookedAt,
p_strMailAddress,
p_strCode);
}
/// <summary>
/// Gets the state value of object.
/// </summary>
public InUseStateEnum Value
{
get { return m_oInUseState.Value; }
}
/// <summary>
/// Member for serialization purposes.
/// </summary>
internal BaseState StateInfoObject
{
get { return m_oInUseState; }
}
/// Transforms object to string.
/// </summary>
/// <returns></returns>
public new string ToString()
{
return m_oInUseState.Value.ToString("g");
}
/// <summary>
/// Date of request/ bookeing action.
/// </summary>
public DateTime? From
{
get
{
var l_oNotDisposableInfo = m_oInUseState as INotAvailableState;
return l_oNotDisposableInfo != null ? l_oNotDisposableInfo.From : (DateTime?)null;
}
}
/// <summary>
/// Mail address.
/// </summary>
public string MailAddress
{
get
{
var l_oNotDisposableInfo = m_oInUseState as INotAvailableState;
return l_oNotDisposableInfo?.MailAddress;
}
}
/// <summary>
/// Reservation code.
/// </summary>
public string Code
{
get
{
var l_oNotDisposableInfo = m_oInUseState as INotAvailableState;
return l_oNotDisposableInfo?.Code;
}
}
/// <summary>
/// Tries update
/// </summary>
/// <returns>True if reservation span has not exeeded and state remains reserved, false otherwise.</returns>
/// <todo>Implement logging of time stamps.</todo>
public bool GetIsStillReserved(out TimeSpan? p_oRemainingTime)
{
var l_oReservedInfo = m_oInUseState as StateRequestedInfo;
if (l_oReservedInfo == null)
{
p_oRemainingTime = null;
return false;
}
return l_oReservedInfo.GetIsStillReserved(out p_oRemainingTime);
}
}
}