using System;
using ShareeBike.Model.Device;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Repository;
namespace ShareeBike.Model.Connector
{
///
/// Connects app to copri data by getting data from cache.
///
public class ConnectorCache : IConnector
{
/// Constructs a copri connector object to connect to cache.
/// Used for offline scenario to ensure responsiveness of app by preventing hopeless tries to communicate with COPRI.
/// Two letter ISO language name.
/// Holds the session cookie.
/// Mail of user.
/// Holds info about smart device.
/// Is null in production and might be a mock in testing context.
public ConnectorCache(
AppContextInfo appContextInfo,
string uiIsoLangugageName,
string sessionCookie,
string mail,
ISmartDevice smartDevice = null,
ICopriServer server = null)
{
Command = Connector.CreateCommand(
server ?? new CopriProviderMonkeyStore(appContextInfo.MerchantId, uiIsoLangugageName, sessionCookie, smartDevice),
sessionCookie,
mail);
Query = GetQuery(
server ?? new CopriProviderMonkeyStore(appContextInfo.MerchantId, uiIsoLangugageName, sessionCookie, smartDevice),
sessionCookie,
mail);
}
/// Object for querying stations and bikes.
public ICommand Command { get; private set; }
/// Object for querying stations and bikes.
public IQuery Query { get; private set; }
/// True if connector has access to copri server, false if cached values are used.
public bool IsConnected => Command.IsConnected;
/// Gets a command object to perform copri queries.
private static IQuery GetQuery(ICopriServer copri, string sessioncookie, string mail) => string.IsNullOrEmpty(sessioncookie)
? new Query(copri) as IQuery
: new QueryLoggedIn(copri, sessioncookie, mail, () => DateTime.Now);
}
}