using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ShareeBike.Model.Bikes;
using ShareeBike.Model.Connector.Updater;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Repository;
using ShareeBike.Repository.Response;
namespace ShareeBike.Model.Connector
{
/// Provides query functionality from cache 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 type. Type detected is {copriServer.GetType()}.");
}
server = copriServer as ICopriServer;
}
/// Gets all stations including positions.
public async Task> GetBikesAndStationsAsync()
{
var stationResponse = await server.GetStationsAsync();
return new Result(
typeof(CopriCallsMonkeyStore),
new StationsAndBikesContainer(
stationResponse.GetStationsAllMutable(),
BikeCollectionFactory.GetBikesAll(
null, // Bikes available are no more of interest because count of available bikes at each given station is was added to station object.
stationResponse.bikes_occupied?.Values ?? new Dictionary().Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache)),
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),
BikeCollectionFactory.GetBikesAll(
bikesFeedbackRequired.bikes?.Values?.Select(bike => bike)?.Where(bike => bike.GetState() == State.InUseStateEnum.FeedbackPending),
bikesOccupiedResponse?.bikes_occupied?.Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache),
bikesOccupiedResponse.GetGeneralData());
}
/// Gets bikes available and bikes occupied.
/// Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.
/// Id of station which is used for filtering bikes. Null if no filtering should be applied.
/// Id of bike which is used for filtering bikes. Null if no filtering should be applied.
/// Collection of bikes.
public async Task> GetBikesAsync(Uri operatorUri = null, string stationId = null, string bikeId = null)
{
var bikesAvailableResponse = await server.GetBikesAvailableAsync(operatorUri, stationId, bikeId);
if (operatorUri?.AbsoluteUri != null)
{
return new Result(
typeof(CopriCallsMonkeyStore),
BikeCollectionFactory.GetBikesAll(
bikesAvailableResponse?.bikes?.Values,
bikesAvailableResponse?.bikes_occupied?.Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache),
bikesAvailableResponse?.GetGeneralData());
}
var bikesOccupiedResponse = await server.GetBikesOccupiedAsync();
return new Result(
typeof(CopriCallsMonkeyStore),
BikeCollectionFactory.GetBikesAll(
bikesAvailableResponse?.bikes?.Values,
bikesOccupiedResponse?.bikes_occupied?.Values,
Mail,
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache),
bikesAvailableResponse?.GetGeneralData());
}
}
}