using System.Windows;
using System.Windows.Controls;
using Microsoft.Kinect;

namespace GettingConnectedWithKinect
{
	public class KinectHelpers
	{
		public static void SetEllipsePosition(FrameworkElement ellipse, Joint joint, double width, double height)
		{
			var scaledJoint = ScaleTo(joint, (int)width, (int)height, .5f, .5f);

			Canvas.SetLeft(ellipse, scaledJoint.Position.X);
			Canvas.SetTop(ellipse, scaledJoint.Position.Y);            
		}

		private static Joint ScaleTo(Joint joint, int width, int height, float skeletonMaxX, float skeletonMaxY)
		{
			SkeletonPoint pos = new SkeletonPoint
			{
				X = Scale(width, skeletonMaxX, joint.Position.X),
				Y = Scale(height, skeletonMaxY, -joint.Position.Y),
				Z = joint.Position.Z,
			};

			Joint j = new Joint
			{
				TrackingState = joint.TrackingState,
				Position = pos
			};

			return j;
		}

		private static float Scale(int maxPixel, float maxSkeleton, float position)
		{
			float value = ((((maxPixel / maxSkeleton) / 2) * position) + (maxPixel/2));
			if(value > maxPixel)
				return maxPixel;
			if(value < 0)
				return 0;
			return value;
		}

	}
}
