/*-----------------------------------------------------------------------------
*  Description :  AI - handle acces to field value in Perso structure
*-----------------------------------------------------------------------------
*  Creation date : 23/10/96            Author : Olivier Couvreur
*/
 
#include "AIUseCPA.h"
 
#include "specif/AIOption.h"
#include "AIMacros.h"
#include "AI_Erm.h"
#include "AI_Struc.h"
 
#include "StrIntel.h"
#include "Convert.h"
 
#include "specif/AiTools.h"
 
/*
unsigned char fn_ucSetFieldValue(enum tdeFieldId_ eFieldId, HIE_tdxHandleToSuperObject hSuperObjPerso, struct tdstGetSetParam_ *p_stGetSetParam)
{
    enum tdeVariableType_ eType;
    tdstGetSetParam stTempGetSetParam;
	tdp_fn_cSetFunctionPtr p_fn_cSetFieldFunction;
    
	eType=fn_eGetFieldType(eFieldId);
    
    switch (eType)
    {
    case E_vt_Float:
        {
            M_Full_GetSetParam_Float(&stTempGetSetParam,M_ReturnParam_xValue(p_stGetSetParam));
        }
        break;
    case E_vt_Vector:
        {
            M_Full_GetSetParam_p_stVertex(&stTempGetSetParam,&M_GetSetParam_stVertexValue(p_stGetSetParam));
        }
        break;
    case E_vt_Perso:
        {
            M_Full_GetSetParam_Perso(&stTempGetSetParam,M_GetSetParam_p_stSupObjValue(p_stGetSetParam));
        }
        break;
    default:
        {
            M_Full_GetSetParam_Integer(&stTempGetSetParam,M_ReturnParam_lValue(p_stGetSetParam));
            
#if defined(__DEBUG_AI__)
            {
                unsigned char ucTruncated=(unsigned char) (M_GetSetParam_Type(p_stGetSetParam)==E_vt_Float);
                long lValue=M_GetSetParam_lValue(&stTempGetSetParam);
                
                switch (eType)
                {   
                case E_vt__128To127:
                    if (!ucTruncated) ucTruncated = (unsigned char) ( (lValue<-128) || (lValue>127) );
                    break;
                case E_vt_0To255:
                    if (!ucTruncated) ucTruncated = (unsigned char) ( (lValue<0) || (lValue>255) );
                    break;
                case E_vt__32768To32767:
                    if (!ucTruncated) ucTruncated = (unsigned char) ( (lValue<-32768) || (lValue>32767) );
                    break;
                case E_vt_0To65535:
                    if (!ucTruncated) ucTruncated = (unsigned char) ( (lValue<0) || (lValue>65535) );
                    break;
                case E_vt_PositiveInteger:
                    if (!ucTruncated) ucTruncated = (unsigned char) (lValue<0);
                    break;
                case E_vt_Boolean:
                case E_vt_1bit:
                    if (!ucTruncated) ucTruncated = (unsigned char) ( (lValue<0) || (lValue>1) );
                    break;
                case E_vt_2bit:
                    if (!ucTruncated) ucTruncated = (unsigned char) ( (lValue<0) || (lValue>3) );
                    break;
                case E_vt_3bit:
                    if (!ucTruncated) ucTruncated = (unsigned char) ( (lValue<0) || (lValue>7) );
                    break;
                case E_vt_4bit:
                    if (!ucTruncated) ucTruncated = (unsigned char) ( (lValue<0) || (lValue>15) );
                    break;
                case E_vt_5bit:
                    if (!ucTruncated) ucTruncated = (unsigned char) ( (lValue<0) || (lValue>31) );
                    break;
                case E_vt_6bit:
                    if (!ucTruncated) ucTruncated = (unsigned char) ( (lValue<0) || (lValue>63) );
                    break;
                case E_vt_7bit:
                    if (!ucTruncated) ucTruncated = (unsigned char) ( (lValue<0) || (lValue>127) );
                    break;
                case E_vt_None:
                default:
                    break;
                }
                
                if (ucTruncated)
                {
                    char MyMsg[100];
                    sprintf(MyMsg,"Field id %d from p_stMind %p (%s)\n",eFieldId,hSuperObjPerso,AI_M_szGetPersoName(AI_M_stGetMindOfSuperObj(hSuperObjPerso)) );
                    M_AIWarningErrorAndNeverStopForDebug(E_uwAIWarningTruncateAffectation, C_ucErmNoOpenInfoWindow,MyMsg);
                }
            }
#endif
        }
    }
	p_fn_cSetFieldFunction=fn_p_fn_pGetFieldSetFunctionPtr(eFieldId);
	return(p_fn_cSetFieldFunction(hSuperObjPerso,&stTempGetSetParam));
}
*/
 
/*
unsigned char fn_ucGetFieldValue(enum tdeFieldId_ eFieldId, HIE_tdxHandleToSuperObject hSuperObjPerso, struct tdstGetSetParam_ *p_stGetSetParam)
{
    enum tdeVariableType_ eType;  
	tdp_fn_cGetFunctionPtr p_fn_cGetFieldFunction;
    
    char ret=C_INVALID_SET;
    
    //extremly important
    M_GetSetParam_Clear(p_stGetSetParam);
    
	p_fn_cGetFieldFunction=fn_p_fn_pGetFieldGetFunctionPtr(eFieldId);
	ret=p_fn_cGetFieldFunction(hSuperObjPerso,p_stGetSetParam);
    
	eType=fn_eGetFieldType(eFieldId);
    
    switch (eType)
    {
    case E_vt_Float:
        {
		          MTH_tdxReal xValue=M_GetSetParam_xValue(p_stGetSetParam);
                  M_Full_GetSetParam_Float(p_stGetSetParam,xValue);
        }
        break;
    case E_vt_Vector:
        {
		          MTH3D_tdstVector *p_stVertex= &M_GetSetParam_stVertexValue(p_stGetSetParam); 
                  M_Full_GetSetParam_p_stVertex(p_stGetSetParam,p_stVertex);
        }
        break;
        
    case E_vt_Perso:
        {
		          HIE_tdxHandleToSuperObject hTempSuperObjPerso;
                  hTempSuperObjPerso= M_GetSetParam_p_stSupObjValue(p_stGetSetParam); 
                  M_Full_GetSetParam_Perso(p_stGetSetParam,hTempSuperObjPerso);
        }
        break;
    default:
        {
            long lValue=0;
            // important pour les valeurs signées qui ne remplissent pas la structure GetSetParam
            switch(eType)
            {
            case E_vt__128To127:
                
                lValue=(long) M_GetSetParam_cValue(p_stGetSetParam);
                break;
            case E_vt__32768To32767:
                lValue=(long) M_GetSetParam_wValue(p_stGetSetParam);
                break;
            default:
                lValue=M_GetSetParam_lValue(p_stGetSetParam);
                break;
            }
            M_Full_GetSetParam_Integer(p_stGetSetParam,lValue); 
        }
    }
    return(ret);
}
*/
 
 
/* Set ****************************************/
#if defined(__DEBUG_AI__)
	unsigned char g_ucFieldTruncated=0;
	#define M_INIT_TRUNCATED_FIELD()	g_ucFieldTruncated=0;
 
	#define M_NONE(x,a,b)     0  
	#define M_INF(x,a,b)	  ((x)<(a))
	#define M_SUP(x,a,b)      ((x)>(a))
	#define M_INF_SUP(x,a,b)  ( M_INF(x,a,0) || M_SUP(x,b,0) )
 
	#define M_TEST_TRUNCATED_FIELD(p_stGetSetParam,expression,a,b) \
		g_ucFieldTruncated=fn_ucIsThereFloatIfYesConvertItToInteger(p_stGetSetParam); \
		if (!g_ucFieldTruncated) \
		{ \
			long lValue=M_GetSetParam_lValue(p_stGetSetParam); \
			g_ucFieldTruncated=##expression(lValue,a,b); \
		}
 
	#define M_CHECK_TRUNCATED_FIELD(eFieldId,p_stPerso) \
	    if (g_ucFieldTruncated) \
		{ \
			char MyMsg[200]; \
			sprintf(MyMsg,"Field %d from p_stMind (%s)\n",(eFieldId),AI_M_szGetPersoName(AI_M_stGetMindOfSuperObj(p_stPerso)) ); \
			M_AIWarningErrorAndNeverStopForDebug(E_uwAIWarningTruncateAffectation, C_ucErmNoOpenInfoWindow,MyMsg); \
		}
#else
	#define M_INIT_TRUNCATED_FIELD() 
	#define M_TEST_TRUNCATED_FIELD(p_stGetSetParam,expression,a,b) (void) fn_ucIsThereFloatIfYesConvertItToInteger(p_stGetSetParam);
	#define M_CHECK_TRUNCATED_FIELD(eFieldId,p_stPerso)
#endif
 
void fn_vSetFieldTypeComport(struct tdstGetSetParam_ *p_stGetSetParam)
{
#if defined(__DEBUG_AI__)
	M_VerifyTypeOfParamAction(p_stGetSetParam,E_vt_Comport);
#endif
}
 
void fn_vSetFieldTypeVector(struct tdstGetSetParam_ *p_stGetSetParam)
{
#if defined(__DEBUG_AI__)
	M_VerifyTypeOfParamAction(p_stGetSetParam,E_vt_Vector);
#endif
	p_stGetSetParam=p_stGetSetParam;
}
 
void fn_vSetFieldTypeFloat(struct tdstGetSetParam_ *p_stGetSetParam)
{
  if (!M_IsFloatType(p_stGetSetParam))
  {
	M_Full_GetSetParam_Float(p_stGetSetParam,MTH_M_xLongToReal(M_GetSetParam_lValue(p_stGetSetParam)));
  }
}
 
void fn_vSetFieldTypeInteger(struct tdstGetSetParam_ *p_stGetSetParam)
{
  fn_ucIsThereFloatIfYesConvertItToInteger(p_stGetSetParam);
}
 
void fn_vSetFieldType0To255(struct tdstGetSetParam_ *p_stGetSetParam)
{
  M_TEST_TRUNCATED_FIELD(p_stGetSetParam, M_INF_SUP, 0, 255);
}
 
 
unsigned char fn_ucSetFieldValue(enum tdeFieldId_ eFieldId, HIE_tdxHandleToSuperObject hSuperObjPerso, struct tdstGetSetParam_ *p_stGetSetParam)
{
    enum tdeFieldTypeId_ eFieldType=fn_eGetEngineFieldType(eFieldId);
	tdp_fn_cSetFunctionPtr p_fn_cSetFieldFunction=fn_p_fn_pGetFieldSetFunctionPtr(eFieldId);
    tdp_fn_vSetTypeFunctionPtr p_fn_vSetFieldTypeFunction=fn_p_fn_pGetFieldTypeSetFunctionPtr(eFieldType);
 
	M_INIT_TRUNCATED_FIELD();
 
	/* pre process*/
	p_fn_vSetFieldTypeFunction(p_stGetSetParam);
 
	M_CHECK_TRUNCATED_FIELD(eFieldId,hSuperObjPerso);
 
	/* write field*/
	return(p_fn_cSetFieldFunction(hSuperObjPerso,p_stGetSetParam));
}
 
/* Get ******************/
 
void fn_vGetFieldTypeComport(struct tdstGetSetParam_ *p_stGetSetParam)
{
#if defined(__DEBUG_AI__)
	M_VerifyTypeOfParamAction(p_stGetSetParam,E_vt_Comport);
#endif
}
 
void fn_vGetFieldTypeVector(struct tdstGetSetParam_ *p_stGetSetParam)
{
#if defined(__DEBUG_AI__)
	M_VerifyTypeOfParamAction(p_stGetSetParam,E_vt_Vector);
#endif
	p_stGetSetParam=p_stGetSetParam;
}
 
void fn_vGetFieldTypeFloat(struct tdstGetSetParam_ *p_stGetSetParam)
{
  if (!M_IsFloatType(p_stGetSetParam))
  {
	M_Full_GetSetParam_Float(p_stGetSetParam,MTH_M_xLongToReal(M_GetSetParam_lValue(p_stGetSetParam)));
  }
}
 
void fn_vGetFieldTypeInteger(struct tdstGetSetParam_ *p_stGetSetParam)
{
  if (M_IsFloatType(p_stGetSetParam))
  {
	M_Full_GetSetParam_Integer(p_stGetSetParam,MTH_M_xRealToLong(M_GetSetParam_xValue(p_stGetSetParam)));
  }
}
 
void fn_vGetFieldType0To255(struct tdstGetSetParam_ *p_stGetSetParam)
{
  if (M_IsFloatType(p_stGetSetParam))
  {
	M_Full_GetSetParam_Integer(p_stGetSetParam,MTH_M_xRealToLong(M_GetSetParam_xValue(p_stGetSetParam)));
  }
}
 
unsigned char fn_ucGetFieldValue(enum tdeFieldId_ eFieldId, HIE_tdxHandleToSuperObject hSuperObjPerso, struct tdstGetSetParam_ *p_stGetSetParam)
{
    enum tdeFieldTypeId_ eFieldType=fn_eGetEngineFieldType(eFieldId);
	tdp_fn_cGetFunctionPtr p_fn_cGetFieldFunction=fn_p_fn_pGetFieldGetFunctionPtr(eFieldId);
    tdp_fn_vGetTypeFunctionPtr p_fn_vGetFieldTypeFunction=fn_p_fn_pGetFieldTypeGetFunctionPtr(eFieldType);
    char ret=C_INVALID_SET;
    
    /*extremly important*/
    M_GetSetParam_Clear(p_stGetSetParam);
    
	/* read fields*/
	ret=p_fn_cGetFieldFunction(hSuperObjPerso,p_stGetSetParam);
 
	if (ret==C_VALID_GET)
	{
		/* post process*/
		p_fn_vGetFieldTypeFunction(p_stGetSetParam);
	}
    return(ret);
}
 
 
