using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;


namespace KinPortal {
	public class DrawPortalStrategy : TargetingStrategy {
		private TimeSpan m_leftLoadTime;
		private TimeSpan m_rightLoadTime;

		private bool m_leftActive = false;
		private HandsProjection m_leftProj;
		private bool m_rightActive = false;
		private HandsProjection m_rightProj;


		public void Update(DynamicObject obj, GameTime gameTime) {
			KinPortal.Game game = (KinPortal.Game) obj.Game;

			if(m_leftActive && game.HandsTracker.LeftHand.IsClosed) {
				m_leftLoadTime += gameTime.ElapsedGameTime;
				if(m_leftLoadTime.Milliseconds >= 300.0) {
					obj.Scene.PortalManager.CreatePortal(m_leftProj.HitPoint + (m_leftProj.HitNormal * 0.01f), Geometry.GetRelativeRotation(Vector3.Right, m_leftProj.HitNormal), PortalManager.PortalType.RED);
					m_leftActive = false;
				}
			} else {
				m_leftActive = false;
			}

			if(m_rightActive && game.HandsTracker.RightHand.IsClosed) {
				m_rightLoadTime += gameTime.ElapsedGameTime;
				if(m_rightLoadTime.Milliseconds >= 300.0) {
					obj.Scene.PortalManager.CreatePortal(m_rightProj.HitPoint + (m_rightProj.HitNormal * 0.01f), Geometry.GetRelativeRotation(Vector3.Right, m_rightProj.HitNormal), PortalManager.PortalType.BLUE);
					m_rightActive = false;
				}
			} else {
				m_rightActive = false;
			}
		}

		public void Target(DynamicObject obj, HandsProjection leftHand, HandsProjection rightHand) {
			KinPortal.Game game = (KinPortal.Game) obj.Game;
			Vector3 orgNormal = Vector3.Right;

			if(leftHand.IsProjected && game.HandsTracker.LeftHand.IsClosed == true && game.HandsTracker.LeftHand.PrevIsClosed == false) {
				m_leftActive = true;
				m_leftProj = leftHand;
				m_leftLoadTime = TimeSpan.Zero;
			}

			if(rightHand.IsProjected && game.HandsTracker.RightHand.IsClosed == true && game.HandsTracker.RightHand.PrevIsClosed == false) {
				m_rightActive = true;
				m_rightProj = rightHand;
				m_rightLoadTime = TimeSpan.Zero;
			}
		}
	}
}
