using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using MasterDetailsWithRIAServices.Web.Services;

namespace MasterDetailsWithRIAServices
{
	public class ArtistIdToAlbumCollectionConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			DomainDataSource albumsDataSource = new DomainDataSource()
			{
				DomainContext = new ChinookContext(),			// Our context
				AutoLoad = false,								// We will call Load a little bit later
				QueryName = "GetAlbumsForArtistIdQuery",		// This is the master-details query from ChinookService.cs
				LoadSize = 1,									// Slow things down artificially to see the asynchronous loading in action
				LoadInterval = new TimeSpan(0, 0, 0, 0, 100)	// Slow things down artificially to see the asynchronous loading in action
			};

			int artistId = (int)value;
			// I want only the albums for this particular artist.
			albumsDataSource.QueryParameters.Add(new Parameter { ParameterName = "artistId", Value = artistId });

			albumsDataSource.Load();

			// albumsDataSource.DataView implements INotifyCollection and that is
			// how the details grid is populated asynchronously.
			return albumsDataSource.DataView;
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}
