using System;
using System.Collections.Generic;
namespace MediaBrowser.Model.Entities
{
    /// 
    /// Since BaseItem and DTOBaseItem both have ProviderIds, this interface helps avoid code repition by using extension methods
    /// 
    public interface IHasProviderIds
    {
        /// 
        /// Gets or sets the provider ids.
        /// 
        /// The provider ids.
        Dictionary ProviderIds { get; set; }
    }
    /// 
    /// Class ProviderIdsExtensions
    /// 
    public static class ProviderIdsExtensions
    {
        /// 
        /// Determines whether [has provider identifier] [the specified instance].
        /// 
        /// The instance.
        /// The provider.
        /// true if [has provider identifier] [the specified instance]; otherwise, false.
        public static bool HasProviderId(this IHasProviderIds instance, MetadataProviders provider)
        {
            return !string.IsNullOrEmpty(instance.GetProviderId(provider.ToString()));
        }
        
        /// 
        /// Gets a provider id
        /// 
        /// The instance.
        /// The provider.
        /// System.String.
        public static string GetProviderId(this IHasProviderIds instance, MetadataProviders provider)
        {
            return instance.GetProviderId(provider.ToString());
        }
        /// 
        /// Gets a provider id
        /// 
        /// The instance.
        /// The name.
        /// System.String.
        public static string GetProviderId(this IHasProviderIds instance, string name)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (instance.ProviderIds == null)
            {
                return null;
            }
            string id;
            instance.ProviderIds.TryGetValue(name, out id);
            return id;
        }
        /// 
        /// Sets a provider id
        /// 
        /// The instance.
        /// The name.
        /// The value.
        public static void SetProviderId(this IHasProviderIds instance, string name, string value)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            
            // If it's null remove the key from the dictionary
            if (string.IsNullOrEmpty(value))
            {
                if (instance.ProviderIds != null)
                {
                    if (instance.ProviderIds.ContainsKey(name))
                    {
                        instance.ProviderIds.Remove(name);
                    }
                }
            }
            else
            {
                // Ensure it exists
                if (instance.ProviderIds == null)
                {
                    instance.ProviderIds = new Dictionary(StringComparer.OrdinalIgnoreCase);
                }
                instance.ProviderIds[name] = value;
            }
        }
        /// 
        /// Sets a provider id
        /// 
        /// The instance.
        /// The provider.
        /// The value.
        public static void SetProviderId(this IHasProviderIds instance, MetadataProviders provider, string value)
        {
            instance.SetProviderId(provider.ToString(), value);
        }
    }
}