using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace TINK.Services
{
/// Container of service objects (locks , geolocation, ...) where one service is active.
/// All service objects must be of different type.
public class ServicesContainerMutable: IEnumerable, INotifyPropertyChanged
{
private readonly Dictionary serviceDict;
public event PropertyChangedEventHandler PropertyChanged;
/// Constructs object on startup of app.
/// Fixed list of service- objects .
/// LocksService name which is activated on startup of app.
public ServicesContainerMutable(
IEnumerable services,
string activeName)
{
serviceDict = services.Distinct().ToDictionary(x => x.GetType().FullName);
if (!serviceDict.ContainsKey(activeName))
{
throw new ArgumentException($"Can not instantiate {typeof(T).Name}- object. Active lock service {activeName} must be contained in [{String.Join(",", serviceDict)}].");
}
Active = serviceDict[activeName];
}
/// Active locks service.
public T Active { get; private set; }
/// Sets a lock service as active locks service by name.
/// Name of the new locks service.
public void SetActive(string active)
{
if (!serviceDict.ContainsKey(active))
{
throw new ArgumentException($"Can not set active lock service {active}. Service must be contained in [{String.Join(",", serviceDict)}].");
}
Active = serviceDict[active];
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Active)));
}
public IEnumerator GetEnumerator()
=> serviceDict.Values.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> serviceDict.Values.GetEnumerator();
}
}