//
// Asura_PC_DoubleFusionAdEngine.cpp
//
// Created by Andrew McMaster - 06/04/2006 
//  (Additional work: Andy Eder - 24/05/2006 onwards)
//                 (Additional work: DWW - 27/06/06 onwards)
// 
// Copyright (C) 2001 Rebellion, All Rights Reserved.
//

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

#ifdef ASURA_INGAMEADENGINE

#include "Core/Asura_String.h"

#include "DoubleFusion/DFEngineHeader.h"
#include "AdEngine/Asura_PC_DoubleFusionAdEngine.h"
#include "AdEngine/Asura_PC_DoubleFusionAdObject.h"



// AdEngine statics
Asura_PC_DoubleFusionAdEngine Asura_PC_DoubleFusionAdEngine::s_xAdEngine;
bool Asura_PC_DoubleFusionAdEngine::m_bInitialised = false;
IDFEngine* Asura_PC_DoubleFusionAdEngine::m_pxAdEngine = 0;
Asura_UnicodeChar Asura_PC_DoubleFusionAdEngine::s_zCurrentZone[256];


////////////////////////////////////////////////////////////////////////////////
///
/// Function      :  Asura_PC_DoubleFusionAdEngine ctor
/// Creation Date :  24/05/2006
/// Author        :  AJE
///
/// Inputs        :  -
/// Output        :  -
///
/// PreConditions :  -
/// Description   :  constructor for our class that encapsulates DF's AdEngine
///
////////////////////////////////////////////////////////////////////////////////
Asura_PC_DoubleFusionAdEngine::Asura_PC_DoubleFusionAdEngine()
{
	s_zCurrentZone[0] = 0;
}


////////////////////////////////////////////////////////////////////////////////
///
/// Function      :  Initialise
/// Creation Date :  24/05/2006
/// Author        :  AJE (DWW 27/06/2006)
///
/// Inputs        :  string identifying root directory that AdEngine is to use
/// Output        :  true on successful initialisation, else false
///
/// PreConditions :  -
/// Description   :  initialisation for AdEngine
///
////////////////////////////////////////////////////////////////////////////////
bool Asura_PC_DoubleFusionAdEngine::Initialise(const char* cszProjectName)
{
	if (GetIsInitialised())
	{
		// the DF engine has already been created
		return true;
	}

	// obtain ptr to the DF engine object and start the AdEngine
	m_pxAdEngine = CreateDFEngine();
	
	Asura_Assert(m_pxAdEngine,"CreateDFEngine failed to return back a pointer");

	if (m_pxAdEngine)
	{

		DF_RESULT dfResult = m_pxAdEngine->Start(DF_ENGINE_SDK_VERSION, cszProjectName,NULL, NULL, NULL);

		if (dfResult != 0)
		{	
			Asura_Assert(false,DFErrorCodeToString((DFErrorCode)dfResult));

			SetIsInitialised(false);
			return false;
		} 
		
		s_xAdEngine.SetIsInitialised(true);

	/*	Asura_UnicodeChar xZoneName[8];
		xZoneName[0] = 0;
		Asura_UTF8ToUnicode("MyZone",xZoneName);
		StartZone(xZoneName);*/

		return true;
	} 

	SetIsInitialised(false);
	return false;
}

// void Asura_PC_DoubleFusionAdEngine::Update(float fTime)
//
// Creation Date:		27/06/2006
// Author:				DWW  
//
// Description: 
//
//
void Asura_PC_DoubleFusionAdEngine::Update(float fTime)
{
	if (GetIsInitialised())
	{
		m_pxAdEngine->Update(fTime);
	} // end if
}


////////////////////////////////////////////////////////////////////////////////
///
/// Function      :  Shutdown
/// Creation Date :  24/05/2006
/// Author        :  AJE (DWW 27/06/2006)
///
/// Inputs        :  -
/// Output        :  -
///
/// PreConditions :  -
/// Description   :  shuts down the AdEngine
///
////////////////////////////////////////////////////////////////////////////////
void Asura_PC_DoubleFusionAdEngine::Shutdown()
{
	if (GetIsInitialised())
	{
		LeaveZone();
		m_pxAdEngine->ShutDown();
		m_pxAdEngine = 0;
				
		SetIsInitialised(false);
	}
}


////////////////////////////////////////////////////////////////////////////////
///
/// Function      :  SetIsInitialised
/// Creation Date :  24/05/2006
/// Author        :  AJE
///
/// Inputs        :  boolean value to indicate initialised state of system
/// Output        :  -
///
/// PreConditions :  -
/// Description   :  set whether or not the system has been initialised
///
////////////////////////////////////////////////////////////////////////////////
void Asura_PC_DoubleFusionAdEngine::SetIsInitialised(const bool bIsInitialised)
{
	m_bInitialised = bIsInitialised;
}


////////////////////////////////////////////////////////////////////////////////
///
/// Function      :  GetIsInitialised
/// Creation Date :  24/05/2006
/// Author        :  AJE
///
/// Inputs        :  -
/// Output        :  boolean value indicating initialised state of system
///
/// PreConditions :  -
/// Description   :  returns whether or not the system has been initialised
///
////////////////////////////////////////////////////////////////////////////////
const bool Asura_PC_DoubleFusionAdEngine::GetIsInitialised()
{
	return m_bInitialised;
}

// int Asura_PC_DoubleFusionAdEngine::CreateDFObject(const char* cszObjectID, IDFObject** ppxDFObject)
//
// Creation Date:		27/06/2006
// Author:				DWW 
//
// Description: 
//
//
int Asura_PC_DoubleFusionAdEngine::CreateDFObject(const char* cszObjectID, IDFObject** ppxDFObject)
{
	if (GetIsInitialised())
	{
		DF_RESULT dfResult = m_pxAdEngine->CreateDFObject(cszObjectID, ppxDFObject);

		if (dfResult < 0)
		{
			Asura_Assert(false,DFErrorCodeToString((DFErrorCode)dfResult));
		}
	}

	return 0;
}

// int Asura_PC_DoubleFusionAdEngine::DestroyDFObject(IDFObject* pxDFObject)
//
// Creation Date:		27/06/2006
// Author:				DWW 
//
// Description: 
//
//
int Asura_PC_DoubleFusionAdEngine::DestroyDFObject(IDFObject* pxDFObject)
{
	if (GetIsInitialised())
	{
		return m_pxAdEngine->RemoveDFObject(pxDFObject);
	}

	return 0;
}


////////////////////////////////////////////////////////////////////////////////
///
/// Function      :  DFErrorCodeToString
/// Creation Date :  24/05/2006
/// Author        :  AJE (DWW 04/07/06)
///
/// Inputs        :  DoubleFusion error code (enumeration)
/// Output        :  human-readable string identifying the error
///
/// PreConditions :  
/// Description   :  textual description of a specific error (useful for debugging)
///
////////////////////////////////////////////////////////////////////////////////
const char* Asura_PC_DoubleFusionAdEngine::DFErrorCodeToString(DFErrorCode eErrorCode)
{
	switch (eErrorCode)
	{
		case DF_OK:
			return "No error";
		case DF_ERROR:
			return "ERROR: Unspecified, DFErrorCode: -1";
		case DF_ERROR_NOTSTARTED:
			return "ERROR: DF AdEngine not started, DFErrorCode: -2";
		case DF_ERROR_OPENFILE:
			return "ERROR: Open file error, DFErrorCode: -3";
		case DF_ERROR_WRONG_VERSION:
			return "ERROR: Wrong version, DFErrorCode: -4";
		case DF_ERROR_ALREADY_STARTED:
			return "ERROR: DF AdEngine already started, DFErrorCode: -5";
		case DF_ERROR_INVALID_OBJECT_ID:
			return "ERROR: Invalid object ID, DFErrorCode: -6";
		case DF_ERROR_INVALID_ZONE_ID:
			return "ERROR: Invalid zone ID, DFErrorCode: -7";
		case DF_ERROR_INVALID_ROOT_FOLDER:
			return "ERROR: Invalid root directory, DFErrorCode: -8";
		case DF_ERROR_NOT_IMPLEMENTED:
			return "ERROR: Not implemented, DFErrorCode: -9";
		case DF_ERROR_USER_SHUTDOWN:
			return "ERROR: User shutdown, DFErrorCode: -10";
		case DF_ERROR_LOGIN_COMM:
			return "ERROR: Loging com, DFErrorCode: -11";
		case DF_ERROR_LOGIN_SERVER:
			return "ERROR: Loging into server, DFErrorCode: -12";
		case DF_ERROR_CREATIVE_DL_COMM:
			return "ERROR: creative dl comm, DFErrorCode: -13";
		case DF_ERROR_CREATIVE_DL_SERVER:
			return "ERROR: creative download from server, DFErrorCode: -14";
		case DF_ERROR_TRACKING_COMM:
			return "ERROR: tracking comm, DFErrorCode: -15";
		case DF_ERROR_TRACKING_SERVER:
			return "ERROR: tracking server, DFErrorCode: -16";
		case DF_ERROR_TRACKING_NODATA:
			return "ERROR: tracking no data, DFErrorCode: -17";
		case DF_ERROR_TRACKING_NOSESSION:
			return "ERROR: tracking no session, DFErrorCode: -18";
		case DF_ERROR_OUT_OF_MEMORY:
			return "ERROR: out of memory, DFErrorCode: -19";
		case DF_ERROR_SOUND_ALREADY_INITIALIZED:
			return "ERROR: sound already initalized, DFErrorCode: -20";
		case DF_ERROR_SOUND_UNKNOWN_TYPE:
			return "ERROR: sound unknown type, DFErrorCode: -21";
		case DF_ERROR_INVALID_PROPERTY:
			return "ERROR: sound invalid property, DFErrorCode: -22";
		case DF_ERROR_CANNOT_RELEASE_CREATIVE:
			return "ERROR: sound cannot release creative, DFErrorCode: -23";
		case DF_ERROR_CREATIVE_WAS_RELEASED:
			return "ERROR: creative was released, DFErrorCode: -24";
		case DF_ERROR_EXCEPTION:
			return "ERROR: exception, DFErrorCode: -25";
		case DF_ERROR_ACTION_NOT_SUPPORTED:
			return "ERROR: action not supported, DFErrorCode: -26";


		default:
			return "Double fusion, Unknown error code";
	
	}
}

// void Asura_PC_DoubleFusionAdEngine::StartZone()
//
// Creation Date:		27/06/2006
// Author:				DWW 
//
// Description: 
//
//
void Asura_PC_DoubleFusionAdEngine::StartZone(const Asura_UnicodeChar* pzZoneName)
{
	if (s_xAdEngine.GetIsInitialised())
	{
		// Make sure the same zone is not started twice
		if (wcscmp(s_zCurrentZone,pzZoneName) != 0)
		{

			wcscpy(s_zCurrentZone,pzZoneName);

			char* pchZoneName = new char[wcslen(pzZoneName)+8];

			Asura_UnicodeToUTF8(pzZoneName,pchZoneName);
			DF_RESULT dfResult = m_pxAdEngine->StartZone(pchZoneName);

			delete [] pchZoneName;
			pchZoneName = 0;

			if (dfResult < 0)
			{
				Asura_Assert(false,DFErrorCodeToString((DFErrorCode)dfResult));
			}
		}
	} 
}

// void Asura_PC_DoubleFusionAdEngine::LeaveZone()
//
// Creation Date:		27/06/2006
// Author:				DWW 
//
// Description: 
//
//
void Asura_PC_DoubleFusionAdEngine::LeaveZone()
{
	if (s_xAdEngine.GetIsInitialised())
	{
		char* pchZoneName = new char[wcslen(s_zCurrentZone)+8];

		Asura_UnicodeToUTF8(s_zCurrentZone,pchZoneName);

		m_pxAdEngine->LeaveZone(pchZoneName);

		delete [] pchZoneName;
		pchZoneName = 0;
	}
}

#endif