using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using ShareeBike.Model.Bikes.BikeInfoNS.BikeNS;
using ShareeBike.Model.Bikes.BikeInfoNS.DriveNS;
using ShareeBike.Model.State;
namespace ShareeBike.Model.Bikes.BikeInfoNS.BC
{
[DataContract]
public abstract class BikeInfoMutable : IBikeInfoMutable, INotifyPropertyChanged
{
/// Holds the bike.
private readonly Bike _Bike;
/// Holds the drive of the bike.
private readonly DriveMutable _Drive;
/// Holds the state info of the bike.
private readonly StateInfoMutable _StateInfo;
///
/// Constructs a bike info object.
///
/// True if device is demo device, false otherwise.
/// Provider for current date time to calculate remaining time on demand for state of type reserved.
/// Name of station where bike is located, null if bike is on the road.
/// Holds the uri of the operator or null, in case of single operator setup.
/// Hold tariff description of bike.
/// Bike state info.
protected BikeInfoMutable(
Bike bike,
DriveMutable drive,
DataSource dataSource,
bool isDemo = BikeInfo.DEFAULTVALUEISDEMO,
IEnumerable group = null,
string stationId = null,
string stationName = null,
Uri operatorUri = null,
IRentalDescription tariffDescription = null,
Func dateTimeProvider = null,
IStateInfo stateInfo = null)
{
IsDemo = isDemo;
Group = group;
_Bike = bike;
_Drive = drive;
DataSource = dataSource;
_StateInfo = new StateInfoMutable(dateTimeProvider, stateInfo);
_StateInfo.PropertyChanged += (sender, eventargs) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(eventargs.PropertyName));
StationId = stationId;
StationName = stationName;
OperatorUri = operatorUri;
TariffDescription = tariffDescription;
}
/// Id of station a which bike is located, null otherwise.
[DataMember]
public string StationId { get; private set; }
/// Name of station a which bike is located, null otherwise.
[DataMember]
public string StationName { get; }
/// Holds description about the tariff.
[DataMember]
public IRentalDescription TariffDescription { get; private set; }
///
/// Holds the rent state of the bike.
///
[DataMember]
public StateInfoMutable State
{
get { return _StateInfo; }
}
///
/// Uri of the operator or null, in case of single operator setup.
///
public Uri OperatorUri { get; }
/// Unused member.
IStateInfoMutable IBikeInfoMutable.State => _StateInfo;
public string Id => _Bike.Id;
public bool IsDemo { get; }
/// Returns the group (ShareeBike, Citybike, ...).
public IEnumerable Group { get; }
public WheelType? WheelType => _Bike.WheelType;
public TypeOfBike? TypeOfBike => _Bike.TypeOfBike;
///
/// Gets whether bike is a AA bike (bike must be always returned a the same station) or AB bike (start and end stations can be different stations).
///
public AaRideType? AaRideType => _Bike.AaRideType;
public LockModel LockModel => _Bike.LockModel;
public string Description => _Bike.Description;
public DriveMutable Drive => _Drive;
///
/// Fired whenever property of bike changes.
///
public event PropertyChangedEventHandler PropertyChanged;
private DataSource _DataSource = DataSource.Copri;
/// Gets or sets the information where the data origins from.
public DataSource DataSource
{
get => _DataSource;
set
{
if (_DataSource == value)
return;
_DataSource = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DataSource)));
}
}
/// Loads a bike object from copri server booking_cancel (cancel reservation)/ booking_update (return bike) response.
/// Bike object to load response into.
/// Controls whether notify property changed events are fired or not.
/// Id of the station if bike station changed, null otherwise.
public void Load(
NotifyPropertyChangedLevel notifyLevel,
string stationId = null)
{
State.Load(InUseStateEnum.Disposable, notifyLevel: notifyLevel);
if (stationId == null)
{
// Station did not change.
return;
}
StationId = stationId;
}
///
/// Converts the instance to text.
///
public override string ToString()
{
return $"Id={Id}{(WheelType != null ? $", wheel(s)={WheelType}" : string.Empty)}{(TypeOfBike != null ? $", type={TypeOfBike}" : "")}, demo={IsDemo}, state={State.ToString()}, location={(!string.IsNullOrEmpty(StationId) ? $"Station {StationId}" : "On the road")}.";
}
}
}