using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFManipulationInertia
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
			GetImages();
		}

		private void canvas_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
		{
			touchStatus.Text = "Manipulation starting";
			e.ManipulationContainer = canvas;
		}

		private void canvas_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
		{
			touchStatus.Text = "Manipulation delta";
			FrameworkElement element = e.Source as FrameworkElement;

			if(element == null)
				return;

			Matrix matrix = ((MatrixTransform)element.RenderTransform).Matrix;

			Point center = new Point(element.ActualWidth/2, element.ActualHeight/2);
			center = matrix.Transform(center);

			matrix.ScaleAt(e.DeltaManipulation.Scale.X, e.DeltaManipulation.Scale.Y, center.X, center.Y);

			matrix.RotateAt(e.DeltaManipulation.Rotation, center.X, center.Y);

			matrix.Translate(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);
			((MatrixTransform)element.RenderTransform).Matrix = matrix;

			if(e.IsInertial)
			{
				Rect container = new Rect(((FrameworkElement)e.ManipulationContainer).RenderSize);
				Rect bounds = element.RenderTransform.TransformBounds(new Rect(element.RenderSize));

				if(!container.Contains(bounds))
				{
					e.ReportBoundaryFeedback(e.DeltaManipulation);
					e.Complete();
				}
			}

			e.Handled = true;
		}

		private void canvas_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
		{
			touchStatus.Text = "Manipulation completed";
		}

		private void canvas_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
		{
			inertiaStatus.Text = "Manipulation Starting";
			e.ExpansionBehavior = new InertiaExpansionBehavior()
			{
				InitialVelocity = e.InitialVelocities.ExpansionVelocity,
				// decrease velocity by 0.1 inches every second
				DesiredDeceleration = 0.1 * 96.0 / (1000.0 * 1000.0)
			};

			e.TranslationBehavior = new InertiaTranslationBehavior()
			{
				InitialVelocity = e.InitialVelocities.LinearVelocity,
				// decrease velocity by 10 inches every second
				DesiredDeceleration = 10.0 * 96.0 / (1000.0 * 1000.0)
			};

			e.RotationBehavior = new InertiaRotationBehavior()
			{
				InitialVelocity = e.InitialVelocities.AngularVelocity,
				// slow down by 2 rotations each second
				DesiredDeceleration = 720.0 / (1000.0 * 1000.0)
			};

			e.Handled = true;
		}

		private void GetImages()
		{
			string[] files = Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg");
			foreach(string file in files)
			{
				BitmapImage bi = new BitmapImage(new Uri(file));
				Image image = new Image();
				image.IsManipulationEnabled = true;
				image.Source = bi;
				image.Width = 512;
				image.Height = 512;
				// by default this is Matrix.Identity which is a constant.  Must create a new MatrixTransform
				// so it can be set/edited by the touch stuff
				image.RenderTransform = new MatrixTransform();
				canvas.Children.Add(image);
			}
		}
	}
}
