//-----------------------------------------------------------------------------
#include "PrecompiledHeader.h"
#include "polyglot.h"
#include <string>
#include "polyglotloader.h"
#include <cstring>

using namespace std;
//-----------------------------------------------------------------------------


#ifdef S3DX_DLL
	polyglotAPI polyglot ;
#endif

//-----------------------------------------------------------------------------
//  Callbacks
//-----------------------------------------------------------------------------

int Callback_polyglot_init ( int _iInCount, const S3DX::AIVariable *_pIn, S3DX::AIVariable *_pOut )
{
    S3DX_API_PROFILING_START( "polyglot.init" ) ;

    // Input Parameters 
    int iInputCount = 0 ;
    S3DX::AIVariable sXMLpath = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;
    S3DX::AIVariable sScheme  = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;
    S3DX::AIVariable nVersion = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;

    // Output Parameters 
    S3DX::AIVariable nResult ;
	
	// type conversion
	// string path = sXMLpath.GetStringValue();
	const char* path = sXMLpath.GetStringValue();
	const char* sch = sScheme.GetStringValue();
	float vers = nVersion.GetNumberValue();

	int ret = pg_init(path, sch, (int)vers);
	nResult.SetNumberValue((float)ret);

    // Return output Parameters 
    int iReturnCount = 0 ;
    _pOut[iReturnCount++] = nResult ;

    S3DX_API_PROFILING_STOP( ) ;
    return iReturnCount;
}

//-----------------------------------------------------------------------------

int Callback_polyglot_isLanguageAvailable ( int _iInCount, const S3DX::AIVariable *_pIn, S3DX::AIVariable *_pOut )
{
    S3DX_API_PROFILING_START( "polyglot.isLanguageAvailable" ) ;

    // Input Parameters 
    int iInputCount = 0 ;
    S3DX::AIVariable sLangcode = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;

    // Output Parameters 
    S3DX::AIVariable bAvailable ;

	//type conversion
	const char* lc = sLangcode.GetStringValue();

	int ret = pg_isLanguageAvailable(lc);
	bAvailable.SetBooleanValue(ret);

    // Return output Parameters 
    int iReturnCount = 0 ;
    _pOut[iReturnCount++] = bAvailable ;

    S3DX_API_PROFILING_STOP( ) ;
    return iReturnCount;
}

//-----------------------------------------------------------------------------

int Callback_polyglot_translate ( int _iInCount, const S3DX::AIVariable *_pIn, S3DX::AIVariable *_pOut )
{
    S3DX_API_PROFILING_START( "polyglot.translate" ) ;

    // Input Parameters 
    int iInputCount = 0 ;
    S3DX::AIVariable sLangcode     = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;
    S3DX::AIVariable sPolyglotCode = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;
	S3DX::AIVariable bLowercase    = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;

    // Output Parameters 
    S3DX::AIVariable stranslatedString ;

	//type conversion
	const char* lc = sLangcode.GetStringValue();
	const char* pc = sPolyglotCode.GetStringValue();
	bool bl = bLowercase.GetBooleanValue();

	int addr = pg_translate(lc, pc, bl);
	const char* trans = (const char*)addr;
	stranslatedString.SetStringValue(trans);

    // Return output Parameters 
    int iReturnCount = 0 ;
    _pOut[iReturnCount++] = stranslatedString ;

    S3DX_API_PROFILING_STOP( ) ;
    return iReturnCount;
}

//-----------------------------------------------------------------------------

int Callback_polyglot_translateCompound ( int _iInCount, const S3DX::AIVariable *_pIn, S3DX::AIVariable *_pOut )
{
    S3DX_API_PROFILING_START( "polyglot.translateCompound" ) ;

    // Input Parameters 
    int iInputCount = 0 ;
    S3DX::AIVariable sLangcode     = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;
    S3DX::AIVariable sPolyglotCode = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;
    S3DX::AIVariable bLowercase    = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;

    // Output Parameters 
    S3DX::AIVariable stranslatedString ;

	//type conversion
	const char* lc = sLangcode.GetStringValue();
	const char* pc = sPolyglotCode.GetStringValue();
	bool bl = bLowercase.GetBooleanValue();

	int addr = pg_translateCompound(lc, pc, bl);
	const char* trans = (const char*)addr;
	stranslatedString.SetStringValue(trans);

    // Return output Parameters 
    int iReturnCount = 0 ;
    _pOut[iReturnCount++] = stranslatedString ;

    S3DX_API_PROFILING_STOP( ) ;
    return iReturnCount;
}

//-----------------------------------------------------------------------------
//  Constructor / Destructor
//-----------------------------------------------------------------------------

polyglotPackage::polyglotPackage ( )
{
#ifdef S3DX_DLL
    polyglot.pfn_polyglot_init                = Callback_polyglot_init                ;
    polyglot.pfn_polyglot_isLanguageAvailable = Callback_polyglot_isLanguageAvailable ;
    polyglot.pfn_polyglot_translate           = Callback_polyglot_translate           ;
    polyglot.pfn_polyglot_translateCompound   = Callback_polyglot_translateCompound   ;
	
#endif
}

//-----------------------------------------------------------------------------

polyglotPackage::~polyglotPackage ( )
{

}

//-----------------------------------------------------------------------------
//  Functions table
//-----------------------------------------------------------------------------

static S3DX::AIFunction aMyFunctions [ ] =
{
    { "init"               , Callback_polyglot_init               , "nResult"          , "sXMLpath, sScheme, nVersion"         , "init polyglot library"             , 0 }, 
    { "isLanguageAvailable", Callback_polyglot_isLanguageAvailable, "bAvailable"       , "sLangcode"                           , "expects lang/loc strings like deAT", 0 }, 
    { "translate"          , Callback_polyglot_translate          , "stranslatedString", "sLangcode, sPolyglotCode, bLowercase", "translate a singe code"            , 0 }, 
    { "translateCompound"  , Callback_polyglot_translateCompound  , "stranslatedString", "sLangcode, sPolyglotCode, bLowercase", "translate compound strings"        , 0 }    
    //{ NULL, NULL, NULL, NULL, NULL, 0}
} ;

//-----------------------------------------------------------------------------
//  Constants table
//-----------------------------------------------------------------------------

static S3DX::AIConstant aMyConstants [ ] =
{
	{ NULL, 0, NULL, 0}
} ;

//-----------------------------------------------------------------------------
//  Accessors
//-----------------------------------------------------------------------------

const char *polyglotPackage::GetName ( ) const
{
    return "polyglot" ;
}

//-----------------------------------------------------------------------------

S3DX::uint32 polyglotPackage::GetFunctionCount ( ) const
{
	if ( aMyFunctions[0].pName == NULL )
	{
		return 0 ;
	}
	else
	{
		return sizeof( aMyFunctions ) / sizeof( S3DX::AIFunction ) ;
	}
}

//-----------------------------------------------------------------------------

S3DX::uint32 polyglotPackage::GetConstantCount ( ) const
{
	if ( aMyConstants[0].pName == NULL )
	{
		return 0 ;
	}
	else
	{
		return sizeof( aMyConstants ) / sizeof( S3DX::AIConstant ) ;
	}
}

//-----------------------------------------------------------------------------

const S3DX::AIFunction *polyglotPackage::GetFunctionAt ( S3DX::uint32 _iIndex ) const
{
    return &aMyFunctions[ _iIndex ] ;
}

//-----------------------------------------------------------------------------

const S3DX::AIConstant *polyglotPackage::GetConstantAt ( S3DX::uint32 _iIndex ) const
{
    return &aMyConstants[ _iIndex ] ;
}
