using System;
using System.Collections.Generic;
using System.Linq;
using PolyTraderSharp.Models;
namespace PolyTrader.Core.Platforms
{
/// Löst den Adapter einer Plattform auf – der einzige Weg an Handelsverträge (#623).
public interface IPlatformRegistry
{
/// Plattformen, für die ein Adapter registriert ist.
IReadOnlyList Available { get; }
bool IsAvailable(PlatformId platform);
/// Wirft, wenn für die Plattform kein Adapter registriert ist.
IPlatformAdapter Get(PlatformId platform);
bool TryGet(PlatformId platform, out IPlatformAdapter? adapter);
/// Adapter der Plattform, zu der das Konto gehört.
IPlatformAdapter ForAccount(AccountState account);
}
public sealed class PlatformRegistry : IPlatformRegistry
{
private readonly Dictionary _adapters;
public PlatformRegistry(IEnumerable adapters)
{
_adapters = new Dictionary();
foreach (var adapter in adapters ?? throw new ArgumentNullException(nameof(adapters)))
{
if (!_adapters.TryAdd(adapter.Id, adapter))
throw new InvalidOperationException($"Für die Plattform '{adapter.Id}' ist mehr als ein Adapter registriert.");
}
Available = _adapters.Keys.ToList();
}
public IReadOnlyList Available { get; }
public bool IsAvailable(PlatformId platform) => _adapters.ContainsKey(platform);
public IPlatformAdapter Get(PlatformId platform) =>
_adapters.TryGetValue(platform, out var adapter)
? adapter
: throw new InvalidOperationException(
$"Für die Plattform '{platform.DisplayName}' ist in dieser Installation kein Adapter vorhanden.");
public bool TryGet(PlatformId platform, out IPlatformAdapter? adapter) =>
_adapters.TryGetValue(platform, out adapter);
public IPlatformAdapter ForAccount(AccountState account) =>
Get(PlatformId.Parse((account ?? throw new ArgumentNullException(nameof(account))).Platform));
}
}