//
// Asura_Chunk_ModuleReverb.cpp
//
// Created by Richard Rice - 17/03/03
// 
// Copyright (C) 2003 Rebellion, All Rights Reserved.
//


#include "Core/Asura.h"
#include "ChunkSystem/Asura_Chunk_ModuleReverb.h"

#include "ChunkSystem/Asura_Chunk_ModuleLinking.h"//for module index conversion
#include "ChunkSystem/Asura_SaveBlock.h"
#include "ChunkSystem/Asura_Chunk_Macros.h"
#include "Debugging/Asura_Assert.h"
#include "FileAccess/Asura_FileAccess.h"
#include "Rendering/Asura_Module.h"
#include "Sound/Asura_ModuleReverb_System.h"


#define ASURA_CHUNK_MODULEREVERB_VERSION 0

bool Asura_Chunk_ModuleReverb::Process(Asura_Handle iFileHandle) const
{
	Asura_Assert(Version<=ASURA_CHUNK_MODULEREVERB_VERSION,"Unknown chunk version");

	Asura_ModuleReverb_System::SetDefaultReverbType(m_iDefaultReverb);
	if(m_iNumberOfModules)
	{
		//check number of modules with the module index conversion thing
		Asura_ModuleIndex_Conversion::CheckNumberOfModules(m_iNumberOfModules,Asura_Module_System::GetNumberOfModules());
		
		Asura_ModuleReverb_System::AllocateModuleData(Asura_Module_System::GetNumberOfModules());
	
		for (int i=0; i<m_iNumberOfModules; i++)
		{
			//read in the reverb type
			int iReverbType;
			ASURA_CHUNK_FILEEXTRACT(iFileHandle,iReverbType,int);
		
			//convert module index
			const int iModuleIndex = Asura_ModuleIndex_Conversion::ConvertIndex(i);
			
			if (iModuleIndex>=0)
			{
				Asura_ModuleReverb_System::SetModuleReverbType(iModuleIndex, iReverbType);
			}
		}
	}
	return true;
}


#ifdef ASURA_TOOLS
void Asura_Chunk_ModuleReverb::Create(Asura_SaveBlock* pxSaveBlock)
{
	const int iNumberOfModules = Asura_ModuleReverb_System::GetNumberOfModules();

	if (iNumberOfModules)
	{
		//need to save a module names chunk first , for converting indeces upon load
		Asura_Chunk_ModuleNames::Create(pxSaveBlock);
	}
	
	//determine size of chunk
	int iChunkSize = sizeof(Asura_Chunk_ModuleReverb);
	iChunkSize += sizeof(int) * iNumberOfModules;

	//allocate chunk
	Asura_Chunk_ModuleReverb* pxChunk = static_cast<Asura_Chunk_ModuleReverb*>(pxSaveBlock->GetAccessPointer(iChunkSize));

	//and fill it in
	pxChunk->ID = ASURA_CHUNK_MODULEREVERB;
	ASURA_CHUNK_VERIFY(pxChunk->ID, int);
	pxChunk->Size = iChunkSize;
	ASURA_CHUNK_VERIFY(pxChunk->Size, int);
	pxChunk->Version = ASURA_CHUNK_MODULEREVERB_VERSION;
	ASURA_CHUNK_VERIFY(pxChunk->Version, int);
	pxChunk->Flags = 0;
	ASURA_CHUNK_VERIFY(pxChunk->Flags, int);

	pxChunk->m_iNumberOfModules = iNumberOfModules;
	ASURA_CHUNK_VERIFY(pxChunk->m_iNumberOfModules, int);

	pxChunk->m_iDefaultReverb = Asura_ModuleReverb_System::GetDefaultReverbType();
	ASURA_CHUNK_VERIFY(pxChunk->m_iDefaultReverb, int);

	char* pcChunkBuffer = (char*) (pxChunk+1);

	for (int i=0; i<iNumberOfModules; ++i)
	{
		const int iReverbType = Asura_ModuleReverb_System::GetModuleReverbType(i);
		ASURA_CHUNK_FILLANDVERIFY(pcChunkBuffer, iReverbType, int)
	}

	Asura_Assert((int)pcChunkBuffer-(int)pxChunk == iChunkSize,"Incorrect chunk size");
}
#endif // ASURA_TOOLS


