using System.Text.RegularExpressions;
namespace TINK.Model.Repository.Exception
{
public class NotAtStationException : InvalidResponseException
{
/// COPRI response status regular expression.
public const string RETURNBIKE_FAILURE_STATUS_MESSAGE_UPPERCASE = "(FAILURE 2178: BIKE [0-9]+ OUT OF GEO FENCING\\. )([0-9]+)( METER DISTANCE TO NEXT STATION )([0-9]+)";
/// Prevents invalid use of exception.
private NotAtStationException() : base(typeof(NotAtStationException).Name)
{
}
public static bool IsNotAtStation(string responseState, out NotAtStationException exception)
{
// Check if there are too many bikes requested/ booked.
var match = Regex.Match(
responseState.ToUpper(),
RETURNBIKE_FAILURE_STATUS_MESSAGE_UPPERCASE);
if (match.Groups.Count != 5
|| !int.TryParse(match.Groups[2].ToString(), out int meters)
|| !int.TryParse(match.Groups[4].ToString(), out int stationNr))
{
exception = null;
return false;
}
exception = new NotAtStationException { Distance = meters, StationNr = stationNr };
return true;
}
/// Holds the maximum count of bikes allowed to reserve/ book.
public int Distance { get; private set; }
/// Holds the maximum count of bikes allowed to reserve/ book.
public int StationNr { get; private set; }
}
}