//
// Asura_PC_DoubleFusionAdObject.cpp
//
// Created by Andy Eder - 31/06/2006 
//		 (MOD by DWW 27/06/006)
// 
// Copyright (C) 2001 Rebellion, All Rights Reserved.
//

// includes
#include "Core/Asura.h"

#ifdef ASURA_INGAMEADENGINE

#include "AdEngine/Asura_PC_DoubleFusionAdObject.h"
#include "AdEngine/ASURA_PC_DoubleFusionAdEngine.h"
 

////////////////////////////////////////////////////////////////////////////////
///
/// Function      :  Asura_PC_DoubleFusionAdObject (ctor)
/// Creation Date :  31/05/2006
/// Author        :  AJE
///
/// Inputs        :  -
/// Output        :  -
///
/// PreConditions :  -
/// Description   :  
///
////////////////////////////////////////////////////////////////////////////////
Asura_PC_DoubleFusionAdObject::Asura_PC_DoubleFusionAdObject() : 
	m_pxDFObject(0)
   ,m_iID(-1)
{
	
}


////////////////////////////////////////////////////////////////////////////////
///
/// Function      :  Asura_PC_DoubleFusionAdObject (dtor)
/// Creation Date :  31/05/2006
/// Author        :  AJE
///
/// Inputs        :  -
/// Output        :  -
///
/// PreConditions :  -
/// Description   :  base dtor 'unregisters' itself from the DFAdEngine system
///
////////////////////////////////////////////////////////////////////////////////
Asura_PC_DoubleFusionAdObject::~Asura_PC_DoubleFusionAdObject()
{
	if (m_pxDFObject)
	{
		// unregister with AdEngine system and destroy the DFObject
		Destroy();
	}
}

// bool Asura_PC_DoubleFusionAdObject::Initialise(const char* zPlaceHolderFileName, const int iTextureID, const void *pData)
//
// Creation Date:		27/06/2006
// Author:				DWW 
//
// Description: 
//
//
bool Asura_PC_DoubleFusionAdObject::Initialise(const char* zPlaceHolderFileName, const int iTextureID, const void *pData)
{
	// create the DFObject

	if (Asura_PC_DoubleFusionAdEngine::s_xAdEngine.GetIsInitialised())
	{
		DF_RESULT dfResult = Asura_PC_DoubleFusionAdEngine::s_xAdEngine.CreateDFObject(zPlaceHolderFileName, &m_pxDFObject);

		if (m_pxDFObject)
		{

			Asura_PC_DoubleFusionAdEngine::s_xAdEngine.Update(timeGetTime());

			DFImageData	dfImageData;
			DFEventVisibleParams dfVisibleEvent;
				
			m_iTextureID = iTextureID;

			dfVisibleEvent.m_visibleSize = 0;
			
			m_pxDFObject->UpdateOnEvent(DFE_VISIBLE, &dfVisibleEvent);
			m_pxDFObject->GetImageData(&dfImageData);

			// create the texture
			Platform_CreateAdTexture(dfImageData.m_width,dfImageData.m_height,ASURA_AD_BGRA);

			Platform_RenderToTexture(dfImageData.m_pBits, dfImageData.m_width, dfImageData.m_height, dfImageData.m_byteSize, dfImageData.m_bytesPerPixel);

			return true;
		}
		else
		{
			const char* zerror = Asura_PC_DoubleFusionAdEngine::s_xAdEngine.DFErrorCodeToString((DFErrorCode)dfResult);
			
			Asura_Assert(false,zerror);
		}
	}

	return false;
}

// int Asura_PC_DoubleFusionAdObject::Update(const float fFrameTime)
//
// Creation Date:		27/06/2006
// Author:				DWW 
//
// Description: 
//
//
bool Asura_PC_DoubleFusionAdObject::Update(const float fFrameTime)
{
	if (m_pxDFObject)
	{
		DFEventVisibleParams dfVisibleEvent;
		DFImageData	dfImageData;

		// for now set the visibile size to 0
		// The reported size should be the visibile size in pixels of the object (is this needed?)
		dfVisibleEvent.m_visibleSize = 0;

		m_pxDFObject->UpdateOnEvent(DFE_VISIBLE, &dfVisibleEvent);

		// get the new image data
		DF_RESULT dfResult = m_pxDFObject->GetImageData(&dfImageData);

		if (dfResult < 0)
		{
			const char* zerror = Asura_PC_DoubleFusionAdEngine::s_xAdEngine.DFErrorCodeToString((DFErrorCode)dfResult);
			
			Asura_Assert(false,zerror);

			return false;
		}

		//if (dfImageData.m_bFrameChanged)
		{
			// update the texture
			Platform_RenderToTexture(dfImageData.m_pBits, dfImageData.m_width, dfImageData.m_height, dfImageData.m_byteSize, dfImageData.m_bytesPerPixel);
		}

	}

	return 0;
}

// bool Asura_PC_DoubleFusionAdObject::Destroy(void)
//
// Creation Date:		27/06/2006
// Author:				DWW 
//
// Description: 
//
//
bool Asura_PC_DoubleFusionAdObject::Destroy(void)
{
	Asura_PC_DoubleFusionAdEngine::s_xAdEngine.DestroyDFObject(m_pxDFObject);
	m_pxDFObject = 0;
	return true;
}

#endif