using System;
using System.Linq;
using System.Threading.Tasks;
using TINK.Model.Bikes;
using TINK.Model.Connector.Updater;
using TINK.Model.Services.CopriApi;
using TINK.Repository;
namespace TINK.Model.Connector
{
/// Provides query functionality for a logged in user.
public class QueryLoggedIn : BaseLoggedIn, IQuery
{
/// Copri server.
private readonly ICopriServer server;
/// Constructs a copri query object.
/// Server which implements communication.
public QueryLoggedIn(ICopriServerBase copriServer,
string sessionCookie,
string mail,
Func dateTimeProvider) : base(copriServer, sessionCookie, mail, dateTimeProvider)
{
server = copriServer as ICopriServer;
if (server == null)
{
throw new ArgumentException($"Copri server is not of expected typ. Type detected is {copriServer.GetType()}.");
}
server = copriServer as ICopriServer;
}
/// Gets all stations including postions.
public async Task> GetBikesAndStationsAsync()
{
var stationResponse = await server.GetStationsAsync();
var bikesAvailableResponse = await server.GetBikesAvailableAsync();
var bikesOccupiedResponse = await server.GetBikesOccupiedAsync();
return new Result(
typeof(CopriCallsMonkeyStore),
new StationsAndBikesContainer(
stationResponse.GetStationsAllMutable(),
UpdaterJSON.GetBikesAll(
bikesAvailableResponse?.bikes?.Values,
bikesOccupiedResponse?.bikes_occupied?.Values,
Mail,
DateTimeProvider)),
stationResponse.GetGeneralData());
}
/// Gets bikes occupied and bikes for which feedback is required.
/// Collection of bikes.
public async Task> GetBikesOccupiedAsync()
{
var bikesFeedbackRequired = await server.GetBikesAvailableAsync();
var bikesOccupiedResponse = await server.GetBikesOccupiedAsync();
return new Result(
typeof(CopriCallsMonkeyStore),
UpdaterJSON.GetBikesAll(
bikesFeedbackRequired.bikes?.Values?.Select(bike => bike)?.Where(bike => bike.GetState() == State.InUseStateEnum.FeedbackPending),
bikesOccupiedResponse?.bikes_occupied?.Values,
Mail,
DateTimeProvider),
bikesOccupiedResponse.GetGeneralData());
}
/// Gets bikes available and bikes occupied.
/// Collection of bikes.
public async Task> GetBikesAsync()
{
var bikesAvailableResponse = await server.GetBikesAvailableAsync();
var bikesOccupiedResponse = await server.GetBikesOccupiedAsync();
return new Result(
typeof(CopriCallsMonkeyStore),
UpdaterJSON.GetBikesAll(
bikesAvailableResponse?.bikes?.Values,
bikesOccupiedResponse?.bikes_occupied?.Values,
Mail,
DateTimeProvider),
bikesAvailableResponse.GetGeneralData());
}
}
}