using MediaBrowser.Common.Logging;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;
using MediaBrowser.UI.Pages;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace MediaBrowser.UI.ViewModels
{
///
/// Class DtoBaseItemViewModel
///
public class DtoBaseItemViewModel : BaseViewModel
{
///
/// The _average primary image aspect ratio
///
private double _averagePrimaryImageAspectRatio;
///
/// Gets the aspect ratio that should be used if displaying the primary image
///
/// The average primary image aspect ratio.
public double AveragePrimaryImageAspectRatio
{
get { return _averagePrimaryImageAspectRatio; }
set
{
_averagePrimaryImageAspectRatio = value;
OnPropertyChanged("AveragePrimaryImageAspectRatio");
}
}
///
/// The _parent display preferences
///
private DisplayPreferences _parentDisplayPreferences;
///
/// Gets of sets the current DisplayPreferences
///
/// The parent display preferences.
public DisplayPreferences ParentDisplayPreferences
{
get { return _parentDisplayPreferences; }
set
{
_parentDisplayPreferences = value;
NotifyDisplayPreferencesChanged();
}
}
///
/// The _item
///
private DtoBaseItem _item;
///
/// Gets or sets the item.
///
/// The item.
public DtoBaseItem Item
{
get { return _item; }
set
{
_item = value;
OnPropertyChanged("Item");
}
}
///
/// Notifies the display preferences changed.
///
public void NotifyDisplayPreferencesChanged()
{
OnPropertyChanged("DisplayPreferences");
}
///
/// Gets an image url that can be used to download an image from the api
///
/// The type of image requested
/// The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.
/// System.String.
public string GetImageUrl(ImageType imageType, int? imageIndex = null)
{
var height = ParentDisplayPreferences.PrimaryImageHeight;
var averageAspectRatio = BaseFolderPage.GetAspectRatio(imageType, AveragePrimaryImageAspectRatio);
var width = height * averageAspectRatio;
var imageOptions = new ImageOptions
{
ImageType = imageType,
ImageIndex = imageIndex,
Height = height
};
if (imageType == ImageType.Primary)
{
var currentAspectRatio = imageType == ImageType.Primary ? Item.PrimaryImageAspectRatio ?? width / height : width / height;
// Preserve the exact AR if it deviates from the average significantly
var preserveExactAspectRatio = Math.Abs(currentAspectRatio - averageAspectRatio) > .10;
if (!preserveExactAspectRatio)
{
imageOptions.Width = Convert.ToInt32(width);
}
}
return App.Instance.ApiClient.GetImageUrl(Item, imageOptions);
}
///
/// Gets the average primary image aspect ratio.
///
/// The items.
/// System.Double.
/// items
public static double GetAveragePrimaryImageAspectRatio(IEnumerable items)
{
if (items == null)
{
throw new ArgumentNullException("items");
}
double total = 0;
var count = 0;
foreach (var child in items)
{
var ratio = child.PrimaryImageAspectRatio ?? 0;
if (ratio.Equals(0))
{
continue;
}
total += ratio;
count++;
}
return count == 0 ? 1 : total / count;
}
///
/// Gets the observable items.
///
/// The items.
/// ObservableCollection{DtoBaseItemViewModel}.
public static ObservableCollection GetObservableItems(DtoBaseItem[] items)
{
return GetObservableItems(items, GetAveragePrimaryImageAspectRatio(items));
}
///
/// Gets the observable items.
///
/// The items.
/// The average primary image aspect ratio.
/// The parent display preferences.
/// ObservableCollection{DtoBaseItemViewModel}.
/// items
public static ObservableCollection GetObservableItems(IEnumerable items, double averagePrimaryImageAspectRatio, DisplayPreferences parentDisplayPreferences = null)
{
if (items == null)
{
throw new ArgumentNullException("items");
}
return new ObservableCollection(items.Select(i => new DtoBaseItemViewModel
{
Item = i,
ParentDisplayPreferences = parentDisplayPreferences,
AveragePrimaryImageAspectRatio = averagePrimaryImageAspectRatio
}));
}
}
}