using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WPFTouch
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
			Touch.FrameReported += Touch_FrameReported;
		}

		private Dictionary<int, Ellipse> _pointMap = new Dictionary<int,Ellipse>();

		void Touch_FrameReported(object sender, TouchFrameEventArgs e)
		{
			TouchPointCollection tpc = e.GetTouchPoints(TouchSurface);
			Ellipse ellipse;

			foreach(TouchPoint tp in tpc)
			{
				touchStatus.Text = "Touch " + tp.Action;

				switch(tp.Action)
				{
					case TouchAction.Down:
						if(!_pointMap.TryGetValue(tp.TouchDevice.Id, out ellipse))
						{
							ellipse = new Ellipse();
							ellipse.Fill = new SolidColorBrush(Colors.Red);
							ellipse.Stroke = new SolidColorBrush(Colors.Black);
							ellipse.Width = 80;
							ellipse.Height= 80;
							_pointMap.Add(tp.TouchDevice.Id, ellipse);
							TouchSurface.Children.Add(ellipse);
						}
						Canvas.SetLeft(ellipse, tp.Position.X - ellipse.Width/2);
						Canvas.SetTop(ellipse, tp.Position.Y - ellipse.Height/2);
						break;
					case TouchAction.Move:
						if(!_pointMap.TryGetValue(tp.TouchDevice.Id, out ellipse))
							return;
						Canvas.SetLeft(ellipse, tp.Position.X - ellipse.Width/2);
						Canvas.SetTop(ellipse, tp.Position.Y - ellipse.Height/2);
						break;
					case TouchAction.Up:
						if(!_pointMap.TryGetValue(tp.TouchDevice.Id, out ellipse))
							return;
						TouchSurface.Children.Remove(ellipse);
						_pointMap.Remove(tp.TouchDevice.Id);
						break;
				}
			}
		}

	}
}
