using System;
using System.Collections.Generic;
using System.Linq;
using MediaBrowser.Model.Providers;
namespace MediaBrowser.Model.Extensions
{
///
/// Extension methods for .
///
public static class EnumerableExtensions
{
///
/// Orders by requested language in descending order, prioritizing "en" over other non-matches.
///
/// The remote image infos.
/// The requested language for the images.
/// The ordered remote image infos.
public static IEnumerable OrderByLanguageDescending(this IEnumerable remoteImageInfos, string requestedLanguage)
{
if (string.IsNullOrWhiteSpace(requestedLanguage))
{
// Default to English if no requested language is specified.
requestedLanguage = "en";
}
var isRequestedLanguageEn = string.Equals(requestedLanguage, "en", StringComparison.OrdinalIgnoreCase);
return remoteImageInfos.OrderByDescending(i =>
{
if (string.Equals(requestedLanguage, i.Language, StringComparison.OrdinalIgnoreCase))
{
return 3;
}
if (string.IsNullOrEmpty(i.Language))
{
// Assume empty image language is likely to be English.
return isRequestedLanguageEn ? 3 : 2;
}
if (!isRequestedLanguageEn && string.Equals(i.Language, "en", StringComparison.OrdinalIgnoreCase))
{
// Prioritize English over non-requested languages.
return 2;
}
return 0;
})
.ThenByDescending(i => i.CommunityRating ?? 0)
.ThenByDescending(i => i.VoteCount ?? 0);
}
}
}