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.effect.ortho.ImageRenderer;
import de.rico.engine.effect.ortho.OverlayImage;
import de.rico.engine.effect.sound.Mp3Sound;

/**
 * This game action can be used to show a full screen image for the
 * specified amount of time.
 * @author Frank Bruns
 *
 * @deprecated Somehow this game action does not get the same result on
 * every graphics hardware. On some machines the screen looks alright, on
 * others it looks distorted and in some cases the screen simply turn black.
 * As long as the problem exists this game action will stay deprecated!
 */
@Deprecated
public class ShowScreen extends AbstractAction
{
	private ImageRenderer imgRenderer = null;
	private OverlayImage image = null;
	private int duration = 0;
	private long cumulatedTime = 0L;
	private Mp3Sound sound = null;
	private boolean soundStarted = false;
	private State backupState = null;


	/**
	 * Creates the game action with the specified file path to the
	 * image file that ought to be displayed on the full game screen.
	 * @param imgFile path to image file
	 * @param duration duration of time to display the image
	 */
	public ShowScreen(String imgFile, int duration)
	{
		this.imgRenderer = new ImageRenderer();
		this.image = new OverlayImage(
				imgFile,
				Camera.getInstance().getViewPort().getWidth(),
				Camera.getInstance().getViewPort().getHeight());

		this.duration = duration;
	}

	/**
	 * Creates the game action with the specified file path to the
	 * image file that ought to be displayed on the full game screen.
	 * You can specify a mp3 sound file for the screen, too. But note that
	 * the screen doesn't wait for the sound file to finish. As soon as
	 * the specified duration time has elapsed the screen disappears
	 * and the sound file gets killed.
	 * @param imgFile path to image file
	 * @param mp3File path to mp3 sound file
	 * @param duration duration of time to display the image
	 */
	public ShowScreen(String imgFile, String mp3File, int duration)
	{
		this(imgFile, duration);
		this.sound = new Mp3Sound(mp3File,false); // don't loop sound
	}

	/* (non-Javadoc)
	 * @see de.rico.adventure.actions.AbstractAction#update(long)
	 */
	@Override
	public void update(long elapsedTime)
	{
		this.cumulatedTime += elapsedTime;

		// save original game state
		if(this.backupState == null)
			this.backupState = StateManager.getInstance().getState();

		StateManager.getInstance().setState(State.SHOW_SCREEN);

		// 	start sound, if not yet started
		if(this.sound != null && this.soundStarted == false)
		{
			this.sound.play();
			this.soundStarted = true;

			// interrupt render thread (this thread) for a short time to allow
			// the sound thread to start safely
			try
			{
				Thread.sleep(100);
			}
			catch (InterruptedException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if(this.cumulatedTime >= this.duration)
		{
			// restore original state
			StateManager.getInstance().setState(this.backupState);
			ActionScheduler.getInstance().removeFirst();
		}

	}

	/* (non-Javadoc)
	 * @see de.rico.adventure.actions.AbstractAction#draw(javax.media.opengl.GL)
	 */
	@Override
	public void draw(GL gl)
	{
		// draw the screen
		this.imgRenderer.drawImage(
				0,
				0,
				Camera.getInstance().getViewPort().getWidth(),
				Camera.getInstance().getViewPort().getHeight(),
				this.image);
	}

	/* (non-Javadoc)
	 * @see de.rico.adventure.actions.AbstractAction#finishUp()
	 */
	@Override
	public void finishUp()
	{
		// restore original state
		StateManager.getInstance().setState(this.backupState);

		if(this.sound != null)
		{
			this.sound.stop();
			this.sound = null;
		}
	}
}
