using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Net;
using MediaBrowser.UI;
using MediaBrowser.UI.Controls;
using MediaBrowser.UI.Converters;
using MediaBrowser.UI.ViewModels;
using System;
using System.ComponentModel;
using System.Windows;
namespace MediaBrowser.Plugins.DefaultTheme.Controls
{
    /// 
    /// Interaction logic for BaseItemTile.xaml
    /// 
    public partial class BaseItemTile : BaseUserControl
    {
        /// 
        /// Gets the view model.
        /// 
        /// The view model.
        public DtoBaseItemViewModel ViewModel
        {
            get { return DataContext as DtoBaseItemViewModel; }
        }
        /// 
        /// Gets the item.
        /// 
        /// The item.
        private DtoBaseItem Item
        {
            get { return ViewModel.Item; }
        }
        /// 
        /// Initializes a new instance of the  class.
        /// 
        public BaseItemTile()
        {
            InitializeComponent();
            DataContextChanged += BaseItemTile_DataContextChanged;
            Loaded += BaseItemTile_Loaded;
            Unloaded += BaseItemTile_Unloaded;
        }
        /// 
        /// Handles the Unloaded event of the BaseItemTile control.
        /// 
        /// The source of the event.
        /// The  instance containing the event data.
        void BaseItemTile_Unloaded(object sender, RoutedEventArgs e)
        {
            if (ViewModel != null)
            {
                ViewModel.PropertyChanged -= ViewModel_PropertyChanged;
            }
        }
        /// 
        /// Handles the Loaded event of the BaseItemTile control.
        /// 
        /// The source of the event.
        /// The  instance containing the event data.
        void BaseItemTile_Loaded(object sender, RoutedEventArgs e)
        {
            if (ViewModel != null)
            {
                ViewModel.PropertyChanged -= ViewModel_PropertyChanged;
                ViewModel.PropertyChanged += ViewModel_PropertyChanged;
            }
        }
        /// 
        /// Handles the DataContextChanged event of the BaseItemTile control.
        /// 
        /// The source of the event.
        /// The  instance containing the event data.
        void BaseItemTile_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            OnItemChanged();
            if (ViewModel != null)
            {
                ViewModel.PropertyChanged -= ViewModel_PropertyChanged;
                ViewModel.PropertyChanged += ViewModel_PropertyChanged;
            }
        }
        /// 
        /// Handles the PropertyChanged event of the ViewModel control.
        /// 
        /// The source of the event.
        /// The  instance containing the event data.
        void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            ReloadImage();
        }
        /// 
        /// Called when [item changed].
        /// 
        private void OnItemChanged()
        {
            ReloadImage();
            var visibility = Item.HasPrimaryImage && !Item.IsType("Episode") ? Visibility.Collapsed : Visibility.Visible;
            if (Item.IsType("Person") || Item.IsType("IndexFolder"))
            {
                visibility = Visibility.Visible;
            }
            txtName.Visibility = visibility;
            var name = Item.Name;
            if (Item.IndexNumber.HasValue)
            {
                name = Item.IndexNumber + " - " + name;
            }
            txtName.Text = name;
        }
        /// 
        /// Reloads the image.
        /// 
        private async void ReloadImage()
        {
            mainGrid.Height = ViewModel.ParentDisplayPreferences.PrimaryImageHeight;
            mainGrid.Width = ViewModel.ParentDisplayPreferences.PrimaryImageWidth;
            if (Item.HasPrimaryImage)
            {
                var url = ViewModel.GetImageUrl(ViewModel.ParentDisplayPreferences.PrimaryImageType);
                border.Background = null;
                
                try
                {
                    image.Source = await App.Instance.GetRemoteBitmapAsync(url);
                }
                catch (HttpException)
                {
                    SetDefaultImage();
                }
            }
            else
            {
                SetDefaultImage();
            }
        }
        /// 
        /// Sets the default image.
        /// 
        private void SetDefaultImage()
        {
            if (Item.IsAudio || Item.IsType("MusicAlbum") || Item.IsType("MusicArtist"))
            {
                var imageUri = new Uri("../Resources/Images/AudioDefault.png", UriKind.Relative);
                border.Background = MetroTileBackgroundConverter.GetRandomBackground();
                image.Source = App.Instance.GetBitmapImage(imageUri);
            }
            else
            {
                var imageUri = new Uri("../Resources/Images/VideoDefault.png", UriKind.Relative);
                border.Background = MetroTileBackgroundConverter.GetRandomBackground();
                image.Source = App.Instance.GetBitmapImage(imageUri);
            }
        }
    }
}