/*=========================================================================
* 
* SECTobj.cpp :  Implementation of Sector Object  
* 
*
* Version			1.0
* Creation date		10/06/99
* Revision date
*
* Shaitan
*=======================================================================*/
#include "stdafx.h"
#include "acp_base.h"

#include "TBKobj.hpp"

#include "DPT.h"
#include "incGAM.h"

#include "_EditID.h"

/*=============================================================================
	Globals
=============================================================================*/


//#################################################################################
// CONSTRUCTOR & DESTRUCTOR
//#################################################################################

/*===========================================================================
*		Description:	Constructor
*		Creation date:	10/06/99
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*=========================================================================*/
TBK_BankObject::TBK_BankObject(TBK_BanksInterface *pInterface, TBK_tdeBankType eType, CString csName, BOOL bExistingBank)
	: CPA_SaveObject (pInterface, C_szBankTypeName,	E_ss_Responsible, NULL, TRUE, fn_szGetLevelsDataPath(), TBK_BankObject::TBK_fn_eCallBackSaveBank)
{
	char szSectionName[SCR_CV_ui_Cfg_MaxLenName];
	char szFileName[SCR_CV_ui_Cfg_MaxLenName];

	// section object
	SetSectionData(this); 	
	// section name
	sprintf(szFileName, "%s\\%s.bnk", fn_p_szGetLevelName(), fn_p_szGetLevelName());
	SCR_fn_v_RdL0_ComputeSectionName(szSectionName, szFileName, (eType == TBK_E_bt_Common) ? C_CommonSectionName : C_BankSectionName, " ");
	SetReferencedSectionName(szSectionName);

	// name
	if (fn_eRename(csName ) != E_mc_None)
		SetDefaultValidName();
	fn_vUpdateSectionName();

	// existence
	SetExistingSection(bExistingBank);

	// init parameters
	m_eBankType = eType;
	m_lBankIndex = (m_eBankType == TBK_E_bt_Common) ? 0 : pInterface->m_fn_lAddANewBank();

	// save bank
	if (!bExistingBank && (m_eBankType != TBK_E_bt_Common))
		fn_vNotifySave();
};
	

/*===========================================================================
*		Description:	Destructor
*		Creation date:	10/06/99
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*=========================================================================*/
TBK_BankObject::~TBK_BankObject()
{
}
	

//#################################################################################
// SECTORS
//#################################################################################

/*===========================================================================
*		Description:	check if a sector belong to the bank
*		Creation date:	11/06/99
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*=========================================================================*/
BOOL TBK_BankObject::m_fn_bIsSectorInBank (CPA_SuperObject *pSector)
{
	return (m_oListOfSector.Find(pSector) != NULL);
}

/*===========================================================================
*		Description:	add a sector to the bank
*		Creation date:	11/06/99
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*=========================================================================*/
BOOL TBK_BankObject::m_fn_bAddSectorToBank (CPA_SuperObject *pSector)
{
	// check if the sector is already in the list
	if (m_fn_bIsSectorInBank(pSector))
		return FALSE;

	// add sector to the list
	m_oListOfSector.AddTail(pSector);
	// update sector color
	m_fn_pGetBanksInterface()->m_fn_vSetSectorColor(pSector, (tdeLocalColor) m_lBankIndex);

	// notify bank for saving
	if (!m_fn_pGetBanksInterface()->GetInterface()->fn_bIsLoadingWorld())
		fn_vNotifySave();

	return TRUE;
}

/*===========================================================================
*		Description:	remove a sector from the bank
*		Creation date:	11/06/99
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*=========================================================================*/
BOOL TBK_BankObject::m_fn_bRemoveSectorFromBank (CPA_SuperObject *pSector)
{
	// check if the sector is really in the list
	POSITION pos = m_oListOfSector.Find(pSector);
	if (!pos)
		return FALSE;

	// remove sector from the list
	m_oListOfSector.RemoveAt(pos);

	// notify bank for saving
	fn_vNotifySave();

	return TRUE;
}

/*===========================================================================
*		Description:	remove a sector from the bank
*		Creation date:	11/06/99
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*=========================================================================*/
BOOL TBK_BankObject::m_fn_bTransferAllSectors (TBK_BankObject *pNewBank)
{
	// empty list ?
	if (!m_oListOfSector.GetCount())
		return FALSE;

	// transfer all list
	while (m_oListOfSector.GetCount())
	{
		CPA_SuperObject *pSector = m_oListOfSector.RemoveTail();
		pNewBank->m_fn_bAddSectorToBank(pSector);
	}
	// notify bank for saving
	fn_vNotifySave();

	return TRUE;
}

/*===========================================================================
*		Description:	get the next sector (for tree)
*		Creation date:	11/06/99
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*=========================================================================*/
CPA_SuperObject * TBK_BankObject::m_fn_pGetNextSector (CPA_SuperObject *pSector)
{
	// empty list ?
	if (!m_oListOfSector.GetCount())
		return NULL;

	// first sector ?
	if (!pSector)
		return m_oListOfSector.GetHead();

	// next sector
	return m_oListOfSector.GetNextElement(pSector);
}


/*===========================================================================
*		Description:	reinit the sector list
*		Creation date:	16/06/99
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*=========================================================================*/
void TBK_BankObject::m_fn_vReinitSectorList (void)
{
	m_oListOfSector.RemoveAll();
}


/*===========================================================================
*		Description:	update colors for the sectors of the bank
*		Creation date:	16/06/99
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*=========================================================================*/
void TBK_BankObject::m_fn_vReinitSectorColors (BOOL bColor)
{
	POSITION pos = m_oListOfSector.GetHeadPosition();
	while (pos)
	{
		CPA_SuperObject *pSector = m_oListOfSector.GetNext(pos);
		m_fn_pGetBanksInterface()->m_fn_vSetSectorColor(pSector, bColor ? (tdeLocalColor) m_lBankIndex : E_lc_NoColor);
	}
}

	
//#################################################################################
// SAVES
//#################################################################################

/*===========================================================================
*		Description:	callback to save the bank
*		Creation date:	
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*=========================================================================*/
void TBK_BankObject::TBK_fn_eCallBackSaveBank (SCR_tdst_File_Description *p_stFile, char *szSectionName, void *p_vData, SCR_tde_Ntfy_Action eAction)
{
	// get sector
	TBK_BankObject	*pBankObject = (TBK_BankObject *) p_vData;
	CPA_SuperObject *pSector;
	POSITION		pos;
	char			szNewSectionName[SCR_CV_ui_Cfg_MaxLenName];

	switch (eAction)
    {
		case SCR_EA_Ntfy_AddSection:
			// go to end of file 
			SCR_fn_v_SvL1_ToEndSection(p_stFile);
			SCR_M_SvL0_SaveBlankLine (p_stFile);

		case SCR_EA_Ntfy_RebuildSection:

			// save section name
			SCR_fn_v_RdL0_ComputeSectionName(szNewSectionName, NULL, (pBankObject->m_fn_eGetBankType() == TBK_E_bt_Common) ? C_CommonSectionName : C_BankSectionName, (char *) (LPCSTR)pBankObject->GetName());
			SCR_M_SvL0_SaveBeginSection(p_stFile, szNewSectionName, SCR_CC_C_Cfg_EOL);

			// save list of sectors
			pos = pBankObject->m_oListOfSector.GetHeadPosition();
			while (pos)
			{
				SCR_M_SvL0_SaveEntry(p_stFile, C_BankSectorEntry, SCR_CC_C_Cfg_NoChar);

				pSector = pBankObject->m_oListOfSector.GetNext(pos);
				SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, pSector->GetReferencedSectionName());

			}

			// end section
			SCR_M_SvL0_SaveEndSection (p_stFile, SCR_CC_C_Cfg_EOL);
			if (eAction == SCR_EA_Ntfy_AddSection)
				SCR_M_SvL0_SaveBlankLine (p_stFile);

			// reinit existence
			pBankObject->fn_vSectionSaved();
			break;
		
		case SCR_EA_Ntfy_DeleteSection:
			// update existence
			pBankObject->fn_vSectionDeleted();
			break;
	}
}
