/**
 *
 */
package de.rico.adventure.actions.custom;

import javax.media.opengl.GL;

import de.rico.adventure.actions.AbstractAction;
import de.rico.adventure.actions.ActionScheduler;
import de.rico.adventure.enums.State;
import de.rico.adventure.structure.StateManager;
import de.rico.engine.camera.Camera;
import de.rico.engine.camera.CameraPerspective;
import de.rico.engine.camera.recorder.CamPlayer;

/**
 * This game action can be used to play a recorded camera flight.
 * @author Frank Bruns
 */
public class PlayCameraFlight extends AbstractAction
{
	private CamPlayer player = null; // camera flight player
	private CameraPerspective tempPerspective = null;
	private int backupPerspId = -1; // original camera perspective id
	private State backupState = null;

	/**
	 * Creates a new camera flight playing action with the
	 * specified recorded camera flight
	 * @param recFile path to recorded camera flight file
	 */
	public PlayCameraFlight(String recFile)
	{
		this.player = new CamPlayer(recFile);

		// create a temporary camera perspective to avoid
		// side effects on the current camera perspective
		this.tempPerspective = new CameraPerspective(0f,0f,0f,0f,0f,0.1f,2000f);
	}

	/* (non-Javadoc)
	 * @see de.rico.adventure.actions.AbstractAction#update(long)
	 */
	@Override
	public void update(long elapsedTime)
	{
		// this should only be called at the first run
		if(this.player.isPlaying() == false)
		{
			// save original game state
			if(this.backupState == null)
				this.backupState = StateManager.getInstance().getState();

			if(this.backupState != State.NO_INPUT_CINEMATIC)
				StateManager.getInstance().setState(State.CINEMATIC);

			// make backup of current camera perspective id to be
			// able to restore the original perspective later on
			this.backupPerspId = Camera.getInstance().getCurrentCamPerspectiveId();

			// add the temporary camera perspective
			Camera.getInstance().addCameraPerspective(this.tempPerspective, -100);
			Camera.getInstance().setToCameraPerspective(-100);

			// start playing
			this.player.start();
		}

		// play the camera flight
		if(this.player.isPlaying() == true)
			this.player.update(elapsedTime);

		// remove the playing action from the scheduler, if
		// the player has finished and remove the temporary
		// camera perspective, too
		if(this.player.isPlaying() == false)
		{
			// reset to original camera perspective
			Camera.getInstance().setToCameraPerspective(this.backupPerspId);

			// reset to original game state
			StateManager.getInstance().setState(this.backupState);
			Camera.getInstance().removeCameraPerspective(-100);
			ActionScheduler.getInstance().removeFirst();
		}
	}

	/* (non-Javadoc)
	 * @see de.rico.adventure.actions.AbstractAction#draw(javax.media.opengl.GL)
	 */
	@Override
	public void draw(GL gl)
	{
		// black bars are gettin draw automatically in CINEMATIC mode
	}

	/* (non-Javadoc)
	 * @see de.rico.adventure.actions.AbstractAction#forceFinish()
	 */
	@Override
	public void finishUp()
	{
		// reset to original camera perspective
		Camera.getInstance().setToCameraPerspective(this.backupPerspId);

		// reset to original game state
		StateManager.getInstance().setState(this.backupState);
		Camera.getInstance().removeCameraPerspective(-100);
	}

}
