using System;
using Windows.ApplicationModel.DataTransfer;
using Windows.Media.SpeechSynthesis;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using FirstUniversalApp;
using FirstUniversalApp.ViewModels;
#if WINDOWS_PHONE_APP
using Windows.Media.SpeechRecognition;
using Windows.Phone.UI.Input;
#endif

namespace FirstUniversalApp
{
	/// <summary>
	/// An empty page that can be used on its own or navigated to within a Frame.
	/// </summary>
	public sealed partial class DetailsPage : Page
	{
		private DataTransferManager _dataTransferManager;
		private ItemViewModel _selectedItem;

		public DetailsPage()
		{
			this.InitializeComponent();
			this.NavigationCacheMode = NavigationCacheMode.Required;
#if WINDOWS_PHONE_APP
			Back.Visibility = Visibility.Collapsed;
			HardwareButtons.BackPressed += HardwareButtons_BackPressed;
#endif
		}

		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			_selectedItem = e.Parameter as ItemViewModel;
			DataContext = _selectedItem;

			_dataTransferManager = DataTransferManager.GetForCurrentView();
			_dataTransferManager.DataRequested += _dataTransferManager_DataRequested;
		}

		protected override void OnNavigatedFrom(NavigationEventArgs e)
		{
			_dataTransferManager.DataRequested -= _dataTransferManager_DataRequested;
			base.OnNavigatedFrom(e);
		}

#if WINDOWS_PHONE_APP
		private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
		{
			Frame.GoBack();
			e.Handled = true;
		}
#endif
		private void Back_Click(object sender, RoutedEventArgs e)
		{
			Frame.GoBack();
		}

		async void _dataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
		{
			var deferral = args.Request.GetDeferral();

			try
			{
				DataPackage requestData = args.Request.Data;
				requestData.Properties.Title = _selectedItem.Title;
				requestData.Properties.Description = _selectedItem.Author;

				var folder = ApplicationData.Current.LocalFolder;
				var photoFile = await folder.CreateFileAsync("photo.jpg", CreationCollisionOption.ReplaceExisting);
				var downloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();
				var dl = downloader.CreateDownload(_selectedItem.ImageUri, photoFile);
				await dl.StartAsync();
		
				RandomAccessStreamReference streamRef = RandomAccessStreamReference.CreateFromUri(_selectedItem.ImageUri);
				requestData.Properties.Thumbnail = streamRef;
				requestData.SetBitmap(streamRef);
				requestData.SetWebLink(_selectedItem.ImageUri);
				requestData.SetStorageItems(new[] { photoFile });
			}
			finally
			{
				deferral.Complete();
			}
		}

		private void Share_Click(object sender, RoutedEventArgs e)
		{
			DataTransferManager.ShowShareUI();
		}

		private async void Read_Click(object sender, RoutedEventArgs e)
		{
			SpeechSynthesizer ss = new SpeechSynthesizer();
			MediaElement me = new MediaElement();
			SpeechSynthesisStream stream = await ss.SynthesizeTextToStreamAsync(_selectedItem.Title);
			me.SetSource(stream, stream.ContentType);
			me.Play();
		}
	}
}
