#include "stdafx.h"

#include "ACP_Base.h"
#include "ITF.h"

#include "TAC.h"
#include "IncMEC.h"
#define D_State_Define
#include "IncGAM.h"
#undef D_State_Define
#include "GLI.h"
#include "DPT.h"

#include "TFA.h"    // Family dll include

#include "CPA_Nll.hpp"
#include "_Ainterf.hpp"
#include "EDACstrg.hpp"

#include "ACP_Base.h"
#include "ITF.h"
#include "IncMEC.h"
#define D_State_Define
#include "IncGAM.h"
#undef D_State_Define
#include "GLI.h"

#include "EDACstrg.hpp"
#include "EDACactr.hpp"

#include "edtList.hpp"
#include "CPA_dato.hpp"
#include "CPA_nll.hpp"
#include "x:\Cpa\Main\Inc\_EditID.h"
#include "_AInterf.hpp"

#include "DPT.h"

#include "OZO.h"    // Zones dll
#include "owp.h"    // Waypoint dll


#undef CPA_WANTS_IMPORT
#undef CPA_EXPORT
#define CPA_WANTS_EXPORT
#include "_Actors.hpp"
#undef CPA_WANTS_EXPORT
#define CPA_WANTS_IMPORT


#define  C_ucTypeWay  18

#ifdef CPA_USES_LDT

#include "ldt.h"

#define sTagObjectsList 'bO'
#define sTagZA          'AZ'
#define sTagDefaultZA   'eD'
#define lTagAddName     'NddA'

HREF g_hRefAddENL;
LDT_tdst_Link *g_pLink;


/*****************************************************************
Function name   : CPA_tdoNameList::fn_iCreateCreateNamesList
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 15-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int CPA_tdoNameList::fn_iCreateCreateNamesList( LDT_tdst_Link *pLink ) // create for 'CreateNamesList' section
{
  CPA_Family       *p_oOwnerFamily = (CPA_Family *)LDT_GetTempLong(0);
  CPA_tdoNameList  *p_tdoNameList;
  BOOL              bSaveOnLevel;

  ASSERT( p_oOwnerFamily );
  bSaveOnLevel = p_oOwnerFamily -> GetEditor() -> GetMainWorld() -> GetInterface() -> fn_bIsLoadingWorld();
  p_oOwnerFamily -> GetEditor() -> GetMainWorld() -> GetInterface() -> SetLoadingWorld( TRUE );
  p_tdoNameList = new CPA_tdoNameList(LDT_szGetParam(1), p_oOwnerFamily, TRUE, LDT_szGetSectionName());
  // set
  pLink->pObject = (void*)p_tdoNameList;
  LDT_SetLinkValue(pLink, (unsigned long)bSaveOnLevel);

  return 0;
}


/*****************************************************************
Function name   : CPA_tdoNameList::fn_iLoadCreateNamesList
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 15-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int CPA_tdoNameList::fn_iLoadCreateNamesList( LDT_tdst_Link *pLink ) // load for 'CreateNamesList' section
{
  LDT_tdeParseResult result=ParseResult_BeginSection;

  CPA_tdoNameList  *p_tdoNameList = (CPA_tdoNameList *)pLink->pObject;
  BOOL              bSaveOnLevel = (BOOL)LDT_GetLinkValue(pLink);


  // load subsections
	do{
		result=LDT_GetNextEntry();
		if( result== ParseResult_BeginSection )
				LDT_LoadSection( NULL );
	}while ( result!=ParseResult_EndSection );

  // if no default ZA, select first (if exists)
  if( p_tdoNameList -> m_p_oDefaultZA == NULL )
  {
      CPA_List<CPA_BaseObject> oList;
      long lNbFound = p_tdoNameList -> GetMainWorld() -> fn_lFindObjects( &oList, "", C_szZATypeName, p_tdoNameList );
      if( lNbFound> 0 )
          p_tdoNameList -> m_p_oDefaultZA = oList . GetHead();
  }
  // load level section
  if( p_tdoNameList -> m_p_oLevelSection -> fn_bSectionExists() )
  {
      LDT_SetTempLong(2, (unsigned long)p_tdoNameList);
      LDT_LoadSection( M_MAKECHAR( p_tdoNameList -> m_p_oLevelSection -> GetCompleteSectionName() ) );
  }
  //
  p_tdoNameList -> GetEditor() -> GetMainWorld() -> GetInterface() -> SetLoadingWorld( bSaveOnLevel );

  return 0;
}


/*****************************************************************
Function name   : CPA_tdoNameList::fn_iCreateAddToNamesList
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 15-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int CPA_tdoNameList::fn_iCreateAddToNamesList( LDT_tdst_Link *pLink )  // create for 'AddToNamesList' section
{
  CPA_tdoNameList *p_tdoNameList = (CPA_tdoNameList *)LDT_GetTempLong(2);
  ASSERT( p_tdoNameList );

  pLink->pObject = (void*)p_tdoNameList;

  return 0;
}



/*****************************************************************
Function name   : CPA_tdoNameList::LoadNamesList
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 15-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int CPA_tdoNameList::LoadNamesList( LDT_tdst_Link *pLink ) // load for 'NamesList' section
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
  CPA_tdoNameList *p_tdoNameList = (CPA_tdoNameList *)pLink->pParent->pObject;
  ASSERT( p_tdoNameList );


  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
  	    char *szEntry=LDT_szGetEntryName();
  	    if ((*(long*)szEntry) == lTagAddName)
  	    {
          tdeShareMode eMode = (tdeShareMode) atoi( LDT_szGetParam(2) );
          tdoObjectName *p_oObjName = new tdoObjectName( p_tdoNameList, LDT_szGetParam(1), eMode );
          p_tdoNameList -> AddTail( p_oObjName );
          
  	    }
  	  }
  	}
  }


  return 0;
}



/*****************************************************************
Function name   : CPA_tdoNameList::fn_iLoadAddENL
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 15-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int CPA_tdoNameList::fn_iLoadAddENL( LDT_tdst_Link *pLink ) // load for 'AddENL' subsection
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
  CPA_tdoNameList *p_tdoNameList = (CPA_tdoNameList *)pLink->pParent->pObject;
  ASSERT( p_tdoNameList );

  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
  	    char *szEntry=LDT_szGetEntryName();
  	    switch (*(short*)szEntry)
  	    {

  	      case  sTagObjectsList : /*  */
  	      {
              if (p_tdoNameList -> GetType () != C_szWayNameListType)
                {
                    g_pLink = pLink; // for calling postprocess from EdtListHandler::fn_poCreateEdtList
                    p_tdoNameList -> m_fnp_oCreateNewZoneList( LDT_szGetParam(1) );               

                }

  	      }
  	      break;
  	      case  sTagDefaultZA : /*  */
  	      {
            p_tdoNameList -> m_p_oDefaultZA = p_tdoNameList -> m_fnp_oCreateNewZA( LDT_szGetParam(1) );
  	      }
  	      break;
  	      case  sTagZA : /*  */
  	      {
            if (strlen(szEntry)<3) //ZA
            {
              p_tdoNameList -> m_fnp_oCreateNewZA( LDT_szGetParam(1) );
            }
            else // ZAList
            {
              p_tdoNameList -> m_fnp_oCreateNewZAList( LDT_szGetParam(1) );
            }
  	      }
  	      break;
  	    }
  	  }
  	}
  }


  return 0;
}



/*****************************************************************
Function name   : CPA_tdoNameList::fn_vAddENLPostProcessLDT
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 15-Oct-98
Modified        : 
Return type     : void 
Argument        : HREF hRef
*****************************************************************/
void CPA_tdoNameList::fn_vAddENLPostProcessLDT( HREF hRef )
{

    LDT_tdst_Link *pLink;
    LDT_tdst_Link *pGetFrom;
    int iType;
    short xCount;
    long *pVal;


    while( LDT_GetRefFromTable( hRef, &pLink, &pGetFrom, &iType, &xCount, &pVal )==0 )
    {
        CPA_tdoNameList *p_NameList = (CPA_tdoNameList *)(*pVal);
        void *p_vEngineList = (void*)(*(pVal+1));
        EdtList *p_NewEdtList = (EdtList *)(*(pVal+2));
        CString *p_csEdtListType = (CString *)(*(pVal+3));
        CString csEdtListType = *p_csEdtListType;
        
      if (p_NameList -> GetType () != g_c_csWayNameListType)
      {
        ZDX_tdxHandleToZdxList hZdxList     = (ZDX_tdxHandleToZdxList) p_vEngineList;    // The engine zone list
        int                    iNumberOfZDx = p_NameList -> GetCount ();                 // The number of zones
        CPA_DLLBase          * pZdxDll      = M_GetMainApp () -> fn_p_oGetMainWorld () -> GetObjectDLLWithName (C_szDLLZDxName);
        ASSERT (pZdxDll);
    
        // More names that ZDx objects : we must grow up the ZDx list
        if (fn_uwGetNumberOfGeoObjInList (hZdxList) < iNumberOfZDx)
        {
          while (fn_uwGetNumberOfGeoObjInList (hZdxList) < p_NewEdtList -> GetCount ())
            p_NewEdtList -> RemoveTail ();                                                     // Remove one zdx from the editor list
      
          while (fn_uwGetNumberOfGeoObjInList (hZdxList) < iNumberOfZDx)
          {
            p_NewEdtList -> AddObjectInZoneMotorList (fn_uwGetNumberOfGeoObjInList (hZdxList));    // Add one zdx into the engine list
            p_NewEdtList -> AddTail ((CPA_SaveObject *) NULL);                                     // Add one zdx into the editor list
            hZdxList = (ZDX_tdxHandleToZdxList) p_NewEdtList -> fn_hGetMotorList ();
          }
          p_NewEdtList -> fn_vNotifySave ();
        }
    
        POSITION               PosEdtList   = p_NewEdtList -> GetHeadPosition ();
        POSITION               PosNameList  = p_NameList   -> GetHeadPosition ();
    
        for (int i = 0 ; i < iNumberOfZDx ; i++)    // For each engine zone :
        {
          // We get the name in the list of names
          tdoObjectName * pObjectName = p_NameList -> GetNext (PosNameList);
      
          // We get the engine zone
          ACP_tdxHandleOfObject hGeometricObject = ZDX_fn_hGetGeometricZdxInListAtIndex (i , hZdxList);
          CPA_SaveObject      * pZdxObject;
      
          if (hGeometricObject)
          {
            // We search the section name of the zone and it MUST exist
            char * szZoneSectionName = GLI_p_cSearchValueInLinkTableOfGeometric ((unsigned long) hGeometricObject);
            ASSERT (szZoneSectionName);
        
            // We compute the zone name
            char * szFamilyName = p_NameList -> GetOwner () -> fn_p_szGetName ();
            char szZoneName [SCR_CV_ui_Cfg_MaxLenName];
            SCR_fn_v_RdL0_SplitSectionName (szZoneSectionName , NULL , NULL , szZoneName);
        
            // We ask the Zone dll to give us a ZDx object associated to the engine zone
            tdstCreateZoneMess  stCreateZoneMess;
            stCreateZoneMess . csTypeName             = csEdtListType;
            stCreateZoneMess . hEngineZone            = hGeometricObject;
            stCreateZoneMess . pOwner                 = p_NameList -> GetOwner ();    // Owner = the family
            stCreateZoneMess . csName                 = szZoneName;
            stCreateZoneMess . szDataPath             = fn_szGetLevelsDataPath ();
        
            char szSectionName [SCR_CV_ui_Cfg_MaxLenName];
            if (pObjectName -> m_eGetShareMode () == E_sm_Virge)
            {
              fn_zsGetActualLevelFilename (szSectionName , "");        // szSectionName = "GameData\Level\Cave\Cave"
              char * p_cSlash = strrchr (szSectionName , '\\');
              char * p_cPoint = strrchr (szSectionName , '.');
              *p_cPoint = 0;
              sprintf (szSectionName , "%s\\Families\\%s\\%s%s" , p_cSlash+1 , szFamilyName , szFamilyName , (LPCTSTR) g_csZoneListExtension);
              // szSectionName = "Cave\Families\Ed\Ed.zdx"
            }
            else
            {
              sprintf (szSectionName , "_Common\\Families\\%s\\%s%s" , szFamilyName , szFamilyName ,
                (LPCTSTR) g_csZoneListExtension);              // "_Common\Families\Ed\Ed.zdx"
            }
            stCreateZoneMess . csReferenceSectionName = szSectionName;
        
            pZdxObject = (CPA_SaveObject *) pZdxDll -> OnQueryAction (g_pclInterface , C_uiQueryCreateZDxObject , (LPARAM) & stCreateZoneMess);
          }
          else pZdxObject = NULL;
      
          // And we put the editor zone into the object list
          p_NewEdtList -> SetAt   (PosEdtList , pZdxObject);
          p_NewEdtList -> GetNext (PosEdtList);
        }
      }  
        
        delete p_csEdtListType;
        LDT_FreeRefValues( pVal );
    }

}

#endif //CPA_USES_LDT