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 readonly AccountMutable m_oAccount;
///
/// Provides storing functionality.
///
private IStore m_oStore;
/// 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)
{
m_oStore = accountStore
?? throw new ArgumentException("Can not instantiate user- object. No store functionality available.");
DeviceId = deviceId;
m_oAccount = 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 {
get
{
return m_oAccount.GetIsLoggedIn();
}
}
///
/// Holds the mail address.
///
public string Mail
{
get { return m_oAccount.Mail; }
}
///
/// Gets the sessiong cookie.
///
public string SessionCookie
{
get { return m_oAccount.SessionCookie; }
}
///
/// Holds the password.
///
public string Password
{
get { return m_oAccount.Pwd; }
}
/// Holds the debug level.
public Permissions DebugLevel
{
get { return m_oAccount.DebugLevel; }
}
/// Holds the group of the bike (TINK, Konrad, ...).
public IEnumerable Group { get { return m_oAccount.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 {m_oAccount} 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.
m_oAccount.Copy(account);
// Save data to store.
await m_oStore.Save(m_oAccount);
// Nothing to do because state did not change.
StateChanged?.Invoke(this, new EventArgs());
}
/// Logs in user
///
public void Logout()
{
var l_oPreviousState = IsLoggedIn;
m_oAccount.Copy(m_oStore.Delete(m_oAccount));
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 p_oSource = null)
{
return m_oAccount.DoFilter(p_oSource);
}
}
}