using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace MediaBrowser.Model.Entities
{
///
/// Class ProviderIdsExtensions.
///
public static class ProviderIdsExtensions
{
///
/// Case insensitive dictionary of string representation.
///
private static readonly Dictionary _metadataProviderEnumDictionary =
Enum.GetValues()
.ToDictionary(
enumValue => enumValue.ToString(),
enumValue => enumValue.ToString(),
StringComparer.OrdinalIgnoreCase);
///
/// Checks if this instance has an id for the given provider.
///
/// The instance.
/// The of the provider name.
/// true if a provider id with the given name was found; otherwise false.
public static bool HasProviderId(this IHasProviderIds instance, string name)
{
ArgumentNullException.ThrowIfNull(instance);
return instance.TryGetProviderId(name, out _);
}
///
/// Checks if this instance has an id for the given provider.
///
/// The instance.
/// The provider.
/// true if a provider id with the given name was found; otherwise false.
public static bool HasProviderId(this IHasProviderIds instance, MetadataProvider provider)
{
return instance.HasProviderId(provider.ToString());
}
///
/// Gets a provider id.
///
/// The instance.
/// The name.
/// The provider id.
/// true if a provider id with the given name was found; otherwise false.
public static bool TryGetProviderId(this IHasProviderIds instance, string name, [NotNullWhen(true)] out string? id)
{
ArgumentNullException.ThrowIfNull(instance);
if (instance.ProviderIds == null)
{
id = null;
return false;
}
var foundProviderId = instance.ProviderIds.TryGetValue(name, out id);
// This occurs when searching with Identify (and possibly in other places)
if (string.IsNullOrEmpty(id))
{
id = null;
foundProviderId = false;
}
return foundProviderId;
}
///
/// Gets a provider id.
///
/// The instance.
/// The provider.
/// The provider id.
/// true if a provider id with the given name was found; otherwise false.
public static bool TryGetProviderId(this IHasProviderIds instance, MetadataProvider provider, [NotNullWhen(true)] out string? id)
{
return instance.TryGetProviderId(provider.ToString(), out id);
}
///
/// Gets a provider id.
///
/// The instance.
/// The name.
/// System.String.
public static string? GetProviderId(this IHasProviderIds instance, string name)
{
instance.TryGetProviderId(name, out string? id);
return id;
}
///
/// Gets a provider id.
///
/// The instance.
/// The provider.
/// System.String.
public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider)
{
return instance.GetProviderId(provider.ToString());
}
///
/// Sets a provider id.
///
/// The instance.
/// The name.
/// The value.
public static void SetProviderId(this IHasProviderIds instance, string name, string? value)
{
ArgumentNullException.ThrowIfNull(instance);
// If it's null remove the key from the dictionary
if (string.IsNullOrEmpty(value))
{
instance.ProviderIds?.Remove(name);
}
else
{
// Ensure it exists
instance.ProviderIds ??= new Dictionary(StringComparer.OrdinalIgnoreCase);
// Match on internal MetadataProvider enum string values before adding arbitrary providers
if (_metadataProviderEnumDictionary.TryGetValue(name, out var enumValue))
{
instance.ProviderIds[enumValue] = value;
}
else
{
instance.ProviderIds[name] = value;
}
}
}
///
/// Sets a provider id.
///
/// The instance.
/// The provider.
/// The value.
public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value)
{
instance.SetProviderId(provider.ToString(), value);
}
}
}