namespace EEMSServerCore.Managers;

internal class DeviceManager
{
    private readonly ConcurrentDictionary<string, DeviceInfo> DevicesFromConnectionID = [];
    private readonly ConcurrentDictionary<Guid, DeviceInfo> DevicesFromGuid = [];

    public bool LoginDevice(string connectionID, DeviceInfo deviceInfo)
    {
        if (!deviceInfo.Guid.HasValue)
            return false;
        DevicesFromGuid[deviceInfo.Guid.Value] = deviceInfo;
        DevicesFromConnectionID[connectionID] = deviceInfo;
        return true;
    }

    public void RemoveDevice(string connectionID, out DeviceInfo? device)
    {
        DevicesFromConnectionID.TryRemove(connectionID, out device);
        if (device is null || !device.Guid.HasValue)
            return;
        DevicesFromGuid.TryRemove(device.Guid.Value, out _);

    }

    public bool TryGetDevice(string connectionID, out DeviceInfo? device)
    {
        device = default;
        if (string.IsNullOrEmpty(connectionID))
            return false;

        return DevicesFromConnectionID.TryGetValue(connectionID, out device);
    }

    public bool TryGetDevice(Guid guid, out DeviceInfo? device)
    {
        return DevicesFromGuid.TryGetValue(guid, out device);
    }

}
