//
// Asura_ServerEntity_FMVTrigger.cpp
//
// Created by Richard Rice - 12/6/2003
// 
// Copyright (C) 2003 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"
#include "Entity/Triggers/Asura_ServerEntity_FMVTrigger.h"

#include "ChunkSystem/Asura_Chunk_Stream.h"
#include "Entity/Asura_Entity_Classification.h"

#ifdef ASURA_TOOLS
#include "Tools_System.h"
#endif

#define ASURA_SERVERENTITY_FMVTRIGGER_VERSION 0

Asura_ServerEntity_FMVTrigger::Asura_ServerEntity_FMVTrigger()
:Asura_ServerEntity_Trigger(true)
{
	m_uFlags = 0;
	Classification = AsuraEntityClass_FMVTrigger;
}

Asura_ServerEntity_FMVTrigger::Asura_ServerEntity_FMVTrigger(Asura_Guid uGuid)
:Asura_ServerEntity_Trigger(true)
{
	m_uFlags = 0;
	Classification = AsuraEntityClass_FMVTrigger;
}

Asura_ServerEntity_FMVTrigger::Asura_ServerEntity_FMVTrigger(Asura_Guid uGuid,const char*& pcChunkBuffer)
:Asura_ServerEntity_Trigger(uGuid,pcChunkBuffer)
{
	Classification = AsuraEntityClass_FMVTrigger;

	int iVersion = ASURA_SERVERENTITY_FMVTRIGGER_VERSION;
	ASURA_CHUNK_EXTRACT( pcChunkBuffer, iVersion, int);
	Asura_Assert(iVersion<=ASURA_SERVERENTITY_FMVTRIGGER_VERSION,"Unknown chunk version");

	ASURA_CHUNK_EXTRACT( pcChunkBuffer, m_uFlags, unsigned int);
	ASURA_CHUNK_EXTRACT_STRING( pcChunkBuffer, m_xFMVName);
}

void Asura_ServerEntity_FMVTrigger::CreateFromChunkBuffer(Asura_Guid uGuid, const char*& pcChunkBuffer)
{
#ifdef TEST_ENTITY_CHUNK_STREAM
	TestStream<Asura_ServerEntity_FMVTrigger>(uGuid, pcChunkBuffer);
#endif
	new Asura_ServerEntity_FMVTrigger(uGuid, pcChunkBuffer);
}

void Asura_ServerEntity_FMVTrigger::OnTrigger()
{
	//play the FMV
    PlatformProject_PlayFMV();

	//send any messages
	Asura_ServerEntity_Trigger::OnTrigger();
}

int Asura_ServerEntity_FMVTrigger::GetChunkBufferSize()
{
	if(m_xFMVName.IsBlank()) m_xFMVName = "";

	int iChunkSize = Asura_ServerEntity_Trigger::GetChunkBufferSize() + sizeof(int)+sizeof(unsigned int);
	ASURA_CHUNK_SIZE_STRING(iChunkSize,m_xFMVName);
		

	return iChunkSize;
}

void Asura_ServerEntity_FMVTrigger::FillInChunkBuffer(char*& pcChunkBuffer)
{
	/*
	This entity type can be used in template files , so any changes to FillInChunkBuffer may
	require ApplyChangesForTemplateInstance() to be updated as well.
	Please see the comment at the top of Tools/EntityTemplate/Tools_EntityTemplate_System.h before 
	making any changes.
	RAR 29/04/2004
	*/
	//fill in base class
	Asura_ServerEntity_Trigger::FillInChunkBuffer(pcChunkBuffer);

	// check that when we're in tools, the FMV hasn't already set the "has been triggered" flag
#ifdef ASURA_TOOLS
	if (Tools_System::IsActive())
	{
		Asura_Assert( !m_bHasBeenTriggered, "this FMV will never play in-game");
	}
#endif

	int iVersion = ASURA_SERVERENTITY_FMVTRIGGER_VERSION;
	ASURA_CHUNK_FILLANDVERIFY( pcChunkBuffer, iVersion, int);
	ASURA_CHUNK_BITFIELD_FILLANDVERIFY( pcChunkBuffer, m_uFlags, unsigned int);
	ASURA_CHUNK_FILL_STRING( pcChunkBuffer, m_xFMVName);
}

void Asura_ServerEntity_FMVTrigger::WriteToChunkStream(Asura_Chunk_Stream& xStream) const
{
	//Wrong order - should do version number first

	// Parent...
	Asura_ServerEntity_Trigger::WriteToChunkStream(xStream);

	// Version...
	int iVersion = ASURA_SERVERENTITY_FMVTRIGGER_VERSION;
	xStream << iVersion;

	// Child...
	Asura_WriteToStream_Bitfield_Wrapper<u_int> xFlagsWrapper(m_uFlags);
	xFlagsWrapper.Add(m_bPC, 1);
	xFlagsWrapper.Add(m_bPS2, 1);
	xFlagsWrapper.Add(m_bXBox, 1);
	xFlagsWrapper.Add(m_bGC, 1);
	xFlagsWrapper.Add(m_bPSP, 1);
	xFlagsWrapper.Add(m_bPS3, 1);
	xFlagsWrapper.Add(m_bXBox360, 1);
	xFlagsWrapper.Add(m_bWii, 1);
	xFlagsWrapper.Add(m_bPlayAtStart, 1);
	xFlagsWrapper.Add(m_bHasBeenTriggered, 1);
	xStream << xFlagsWrapper;
	xStream << m_xFMVName;
}

void Asura_ServerEntity_FMVTrigger::ReadFromChunkStream(Asura_Chunk_Stream& xStream)
{
	//Wrong order - should do version number first

	// Parent...
	Asura_ServerEntity_Trigger::ReadFromChunkStream(xStream);

	// Version...
	int iVersion = 0;
	xStream >> iVersion;
	Asura_Assert(iVersion<=ASURA_SERVERENTITY_FMVTRIGGER_VERSION,"Unknown chunk version");

	// Child...
	xStream >> m_uFlags;
	xStream >> m_xFMVName;
}

// This will play the FMV if it is the first first update and the FMV is triggered to play at the start of the level.
void Asura_ServerEntity_FMVTrigger::Update()
{
	if( IsDestroyed() ) { return; }

	if (IsActive())
	{
		// Lets see if this FMV wants to be played straight away, and if it already has.
		if (m_bPlayAtStart && !m_bHasBeenTriggered)
		{
			OnTrigger();
			m_bHasBeenTriggered = true;
		}
	}
	Asura_ServerEntity_Trigger::Update();
}
