using System;
using System.Threading.Tasks;
using ShareeBike.MultilingualResources;
using Serilog;
using DisconnectCommand = ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.Command.DisconnectCommand;
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
namespace ShareeBike.ViewModel.Bikes.Bike
{
///
/// Return bike action.
///
/// Type of owner.
public class DisconnectLockActionViewModel :
DisconnectCommand.IDisconnectCommandListener
{
///
/// View model to be used for progress report and unlocking/ locking view.
///
private IBikesViewModel BikesViewModel { get; set; }
/// Object to start or stop update of view model objects from Copri.
private Func ViewUpdateManager { get; }
/// Bike
private IBikeInfoMutable SelectedBike { get; set; }
///
/// Constructs the object.
///
/// Bike to close.
/// Object to start or stop update of view model objects from Copri.
/// View model to be used for progress report and unlocking/ locking view.
///
public DisconnectLockActionViewModel(
IBikeInfoMutable selectedBike,
Func viewUpdateManager,
IBikesViewModel bikesViewModel)
{
SelectedBike = selectedBike;
ViewUpdateManager = viewUpdateManager;
BikesViewModel = bikesViewModel
?? throw new ArgumentException($"Can not construct {GetType().Name}-object. {nameof(bikesViewModel)} must not be null.");
}
///
/// Processes the disconnect lock progress.
///
/// Current step to process.
public void ReportStep(DisconnectCommand.Step step)
{
switch (step)
{
case DisconnectCommand.Step.DisconnectLock:
BikesViewModel.ActionText = AppResources.ActivityTextDisconnectingLock;
break;
}
}
///
/// Processes the disconnect lock state.
///
/// State to process.
/// Textual details describing current state.
public Task ReportStateAsync(DisconnectCommand.State state, string details)
{
switch (state)
{
case DisconnectCommand.State.GeneralDisconnectError:
BikesViewModel.ActionText = AppResources.ActivityTextErrorDisconnect;
break;
}
return Task.CompletedTask;
}
/// Disconnect lock.
public async Task DisconnectLockAsync()
{
Log.ForContext().Information("User request to end rental of bike {bikeId}.", SelectedBike.Id);
// lock GUI
BikesViewModel.IsIdle = false;
// Stop Updater
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
await ViewUpdateManager().StopAsync();
try
{
#if USELOCALINSTANCE
var command = new DisonnectCommand(SelectedBike, LockService);
await command.Invoke(this);
#else
await SelectedBike.DisconnectAsync(this);
#endif
Log.ForContext().Information("Lock of {bikeId} disconnected successfully.", SelectedBike.Id);
}
catch (Exception exception)
{
Log.ForContext().Information("Lock of bike {bikeId} could not be disconnected. {@exception}", SelectedBike.Id, exception);
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
await ViewUpdateManager().StartAsync();
BikesViewModel.ActionText = string.Empty;
BikesViewModel.IsIdle = true;
}
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
await ViewUpdateManager().StartAsync();
BikesViewModel.ActionText = string.Empty;
BikesViewModel.IsIdle = true;
return;
}
}
}