using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using TINK.Model.User.Account;
namespace TINK.Model.User
{
public delegate void LoginStateChangedDelegate(object p_oSender, EventArgs p_oEventArgs);
///
/// Manages user of the app.
///
public class User : IUser
{
/// Holds account data.
private AccountMutable Account { get; }
///
/// Provides storing functionality.
///
private IStore Store { get; }
/// Holds the id of the device.
public string DeviceId { get; }
/// Loads user name and passwort from account store.
///
/// Object to use for loading and saving user data.
public User(
IStore accountStore,
IAccount account,
string deviceId)
{
Store = accountStore
?? throw new ArgumentException("Can not instantiate user- object. No store functionality available.");
DeviceId = deviceId;
Account = new AccountMutable(account);
}
/// Is fired wheneverlogin state changes.
public event LoginStateChangedDelegate StateChanged;
///
/// Holds a value indicating whether user is logged in or not.
///
public bool IsLoggedIn => Account.GetIsLoggedIn();
///
/// Holds the mail address.
///
public string Mail
{
get { return Account.Mail; }
}
///
/// Gets the sessiong cookie.
///
public string SessionCookie
{
get { return Account.SessionCookie; }
}
///
/// Holds the password.
///
public string Password
{
get { return Account.Pwd; }
}
/// Holds the debug level.
public Permissions DebugLevel
{
get { return Account.DebugLevel; }
}
/// Holds the group of the bike (TINK, Konrad, ...).
public IEnumerable Group { get { return Account.Group; } }
/// Logs in user.
/// Account to use for login.
/// Holds the Id to identify the device.
/// True if connector has access to copri server, false if cached values are used.
public void CheckIsPasswordValid(string mail, string password)
{
if (IsLoggedIn)
{
throw new Exception($"Can not log in user {mail} because user {Account} is already logged in.");
}
// Check if password might be valid before connecting to copri.
var l_oResult = Validator.ValidateMailAndPasswordDelegate(mail, password);
if (!l_oResult.IsValid)
{
// Password is not valid.
throw new ArgumentException(l_oResult.Description[Elements.Account]);
}
}
/// Logs in user.
/// Account to use for login.
/// Holds the Id to identify the device.
/// True if connector has access to copri server, false if cached values are used.
public async Task Login(IAccount account)
{
// Update account instance from copri data.
Account.Copy(account);
// Save data to store.
await Store.Save(Account);
// Nothing to do because state did not change.
StateChanged?.Invoke(this, new EventArgs());
}
/// Logs in user
///
public void Logout()
{
var l_oPreviousState = IsLoggedIn;
Account.Copy(Store.Delete(Account));
if (IsLoggedIn == l_oPreviousState)
{
// Nothing to do because state did not change.
return;
}
StateChanged?.Invoke(this, new EventArgs());
}
///
/// Filters bike groups depending on whether user has access to all groups of bikes.
/// Some user may be "TINK"- user only, some "Konrad" and some may be "TINK" and "Konrad" users.
///
/// Account to filter with.
/// Groups to filter..
/// Filtered bike groups.
public IEnumerable DoFilter(IEnumerable source = null)
{
return Account.DoFilter(source);
}
}
}