class CCTVCamera extends GameObject
{
	private var m_mesh : MeshComponent;
	private var m_camera : CameraComponent;
	private var m_isControlled : Bool;
	default m_isControlled = false;
	private var m_cachedPuppetID : EntityID;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		EntityRequestComponentsInterface.RequestComponent( ri, 'cameraMesh', 'MeshComponent', false );
		EntityRequestComponentsInterface.RequestComponent( ri, 'cameraComp', 'CameraComponent', false );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		m_mesh = ( ( MeshComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'cameraMesh' ) ) );
		m_camera = ( ( CameraComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'cameraComp' ) ) );
		m_camera.SetIsHighPriority( true );
	}

	protected function Rotate( deltaYaw : Float )
	{
		var currentRotationMat : Matrix;
		var currentRotationEA : EulerAngles;
		var orientationEA : EulerAngles;
		currentRotationMat = Quaternion.ToMatrix( m_mesh.GetLocalOrientation() );
		currentRotationEA = Matrix.GetRotation( currentRotationMat );
		orientationEA.Pitch = currentRotationEA.Pitch;
		orientationEA.Yaw = currentRotationEA.Yaw + deltaYaw;
		m_mesh.SetLocalOrientation( EulerAngles.ToQuat( orientationEA ) );
	}

	protected function TakeControl( val : Bool )
	{
		m_isControlled = val;
		if( val )
		{
			m_cachedPuppetID = GameInstance.GetPlayerSystem( GetGame() ).GetLocalPlayerMainGameObject().GetEntityID();
			GameInstance.GetPlayerSystem( GetGame() ).LocalPlayerControlExistingObject( GetEntityID() );
			m_camera.Activate( 1.0 );
			RegisterInputListener( this, 'CameraMouseX' );
			RegisterInputListener( this, 'CameraAim' );
			RegisterInputListener( this, 'UI_Exit' );
		}
		else
		{
			UnregisterInputListener( this );
			m_camera.Deactivate( 1.0 );
			GameInstance.GetPlayerSystem( GetGame() ).LocalPlayerControlExistingObject( m_cachedPuppetID );
			m_cachedPuppetID = EntityID();
		}
	}

	protected event OnAreaEnter( trigger : AreaEnteredEvent )
	{
		TakeControl( true );
	}

	protected event OnAreaExit( trigger : AreaExitedEvent ) {}

	protected event OnAction( action : ListenerAction, consumer : ListenerActionConsumer )
	{
		if( ListenerAction.GetName( action ) == 'CameraMouseX' )
		{
			if( m_isControlled )
			{
				Rotate( -( ListenerAction.GetValue( action ) ) * 0.1 );
			}
		}
		if( ListenerAction.GetName( action ) == 'CameraAim' )
		{
			if( ListenerAction.IsButtonJustPressed( action ) )
			{
				m_camera.SetZoom( 2.0 );
			}
			else if( ListenerAction.IsButtonJustReleased( action ) )
			{
				m_camera.SetZoom( 0.0 );
			}
		}
		if( ListenerAction.GetName( action ) == 'UI_Exit' )
		{
			if( ListenerAction.IsButtonJustPressed( action ) )
			{
				TakeControl( false );
			}
		}
	}

}

