///////////////////////////////////////////////////////////////////////////////
//
// File   : Asura_FMV.cpp
// Author : Jason Turner
// Date   : 12/02/09
//
// Copyright (C) 2009 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

//Asura main header
#include "Core/Asura.h"

#ifdef ASURA_USE_VIDEO_TO_TEXTURE_SYSTEM

//This files header
#include "Asura_FMV.h"

//Asura Headers
#include "Core/Asura_LocalisedText.h"
#include "Core/Asura_String.h"
#include "Core/Asura_Timers.h"
#include "Core/Asura_UserInput.h"
#include "Debugging/Asura_Assert.h"
#include "FMV/Asura_VideoToTexture.h"
#include "Rendering/Asura_Render.h"
#include "Sound/Asura_Audio_Systems_Manager.h"

bool Asura_FMV::s_bAllowSkip = true;
const float	Asura_FMV::s_fTimeBeforeSkipAllowed = 0.5f;

////////////////////////////////////////////////////////////////////////////////
//  Asura_FMV::Play
////////////////////////////////////////////////////////////////////////////////

bool Asura_FMV::Play(	const char* szFilename,
						bool bLoop /*= false*/,
						Asura_LocalisedText_Language eOverrideLanguage /*= ASURA_LT_L_NUM_LANGUAGES*/)
{
	bool bRet = true;

	Asura_VideoToTexture *pxVideoRenderer = new Asura_VideoToTexture;
	if (!pxVideoRenderer)
	{
		Asura_Assert(pxVideoRenderer, "Unable to allocate Asura_VideoToTexture");
		return false;
	}

	Asura_LocalisedText_Language eLang = ( eOverrideLanguage == ASURA_LT_L_NUM_LANGUAGES ) ? Asura_LocalisedText::GetCurrentLanguage() : eOverrideLanguage;

	pxVideoRenderer->SetPlayFlags(bLoop ? Asura_VideoToTexture::ASURA_FMV_FLAG_LOOP : Asura_VideoToTexture::ASURA_FMV_FLAG_DEFAULT);

	Asura_Assert(!(bLoop && !s_bAllowSkip) ,"FMV set to loop but is unskippable!");

	PrePlay();

	bool bExit = false;
	bool bSkipping = false;

	while (!bExit)
	{
		// We're in a tight loop so call any normal updates here
		if (!Update())
		{
			// we need to stop for some reason
			break;
		}

		// Deal with input.
		{
			Asura_UserInput::Update();

			if(bSkipping)
			{
				// Wait for no input here so whatever happens after the video playback doesn't get unwanted input
				if( !Asura_UserInput::HasAnyKeyBeenPressed() )
				{
					break;
				}
			}
			else if (s_bAllowSkip)
			{
				// need to skip now?
				bool bWantsSkip = Platform_CheckForFMVSkip();

				// Are we past the minimum time?
				if( bWantsSkip && pxVideoRenderer->GetPlayedTimeInSeconds() >= s_fTimeBeforeSkipAllowed )
				{
					bSkipping = true;
				}
			}
		}

		Asura_Render::BeginFrame();
		pxVideoRenderer->Update();

		switch (pxVideoRenderer->GetPlayerState())
		{
			case Asura_VideoToTexture::PLAYER_STATE_INITIALISED:
			{
				// open FMV and render full screen
				pxVideoRenderer->Open(szFilename, ASURA_TEXTURE_NONE, (u_int) eLang);
				pxVideoRenderer->Play();
				break;
			}

			case Asura_VideoToTexture::PLAYER_STATE_PLAYING:
			case Asura_VideoToTexture::PLAYER_STATE_PAUSED:
			{
				pxVideoRenderer->Render();

				break;
			}

			// we've finished playing so exit
			case Asura_VideoToTexture::PLAYER_STATE_COMPLETE:
			case Asura_VideoToTexture::PLAYER_STATE_ERROR:
			{
				bExit = true;
				break;
			}

			default:
				break;
		}

		Asura_Render::EndFrame();
	}

	PostPlay();

	// did we fail?
	bRet = pxVideoRenderer->GetPlayerState() == Asura_VideoToTexture::PLAYER_STATE_ERROR ? false : true;

	delete pxVideoRenderer;

	return bRet;
}

////////////////////////////////////////////////////////////////////////////////
//  Asura_FMV::Update
////////////////////////////////////////////////////////////////////////////////

bool Asura_FMV::Update()
{
	Asura_Timers::HandlePrimaryTimer();
	Asura_Audio_Systems_Manager::Update();

	return Platform_Update();
};

////////////////////////////////////////////////////////////////////////////////
//  Asura_FMV::PrePlay
////////////////////////////////////////////////////////////////////////////////

void Asura_FMV::PrePlay()
{
	Platform_PrePlay();
};

////////////////////////////////////////////////////////////////////////////////
//  Asura_FMV::PostPlay
////////////////////////////////////////////////////////////////////////////////

void Asura_FMV::PostPlay()
{
	Platform_PostPlay();
};

////////////////////////////////////////////////////////////////////////////////
#endif	// ASURA_USE_VIDEO_TO_TEXTURE_SYSTEM
