// EdIRCstB.hpp : implementation file
//

#include "stdafx.h"

#include "Defines.hpp"

#ifdef D_ED_IR_ACTIVE

#include "EdIRCstB.hpp"

#include "EdIRIRD.hpp"
#include "EdIRBeEn.hpp"
#include "EdIRStrg.hpp"
#include "EdIRCsNB.hpp"
#include "EdIRCsLs.hpp"

#include "incDPT.h"
#include "incTUT.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//Stefan Dumitrean 17-06-98
#define C_SETTINGS_FILE_NAME  "\\cpa\\exe\\main\\edt_data\\ctcoldim.dat"
//End Stefan Dumitrean 17-06-98

#define C_TYPE_COLUMN_NUMBER	0
#define C_NAME_COLUMN_NUMBER	1
#define C_VALUE_COLUMN_NUMBER	2

int CALLBACK fn_vCompareItems(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	 CPA_EdIR_Constant *pCts1 = (CPA_EdIR_Constant *) lParam1;
	 CPA_EdIR_Constant *pCts2 = (CPA_EdIR_Constant *) lParam2;

	 switch (lParamSort) 
	 {
		 case C_TYPE_COLUMN_NUMBER:
			return pCts1->m_fn_csGetConstantType().Compare(pCts2->m_fn_csGetConstantType());
		 case C_NAME_COLUMN_NUMBER:
			return pCts1->m_fn_csGetConstantName().Compare(pCts2->m_fn_csGetConstantName());
		 case C_VALUE_COLUMN_NUMBER:
			return pCts1->m_fn_csGetConstantValue().Compare(pCts2->m_fn_csGetConstantValue());
	 }

	 return 0;
}

/////////////////////////////////////////////////////////////////////////////
// class CPA_EdIR_ConstantDialog dialog
/////////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(CPA_EdIR_ConstantDialog, CDialog)
	//{{AFX_MSG_MAP(CPA_EdIR_ConstantDialog)
	ON_BN_CLICKED(IDC_BUTTON_NEW_CONSTANT,OnButtonNewConstant)
	ON_BN_CLICKED(IDC_BUTTON_CHANGE_CONSTANT,OnButtonChangeConstant)
	ON_BN_CLICKED(IDC_BUTTON_DELETE_CONSTANT,OnButtonDeleteConstant)
	ON_NOTIFY(LVN_COLUMNCLICK, IDC_CONSTANT_LIST, OnColumnClicked)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/**********************************************************************************/
CPA_EdIR_ConstantDialog::CPA_EdIR_ConstantDialog(CPA_EdIR_ConstantList *pclConstantList,CWnd* pParent /*=NULL*/)
	: CDialog(CPA_EdIR_ConstantDialog::IDD, &g_oBaseFrame)
{
	//{{AFX_DATA_INIT(CPA_EdIR_ConstantDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_pclConstantList=pclConstantList;
}

/**********************************************************************************/
void CPA_EdIR_ConstantDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPA_EdIR_ConstantDialog)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// CPA_EdIR_ConstantDialog message handlers

/**********************************************************************************/
BOOL CPA_EdIR_ConstantDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();

//Stefan Dumitrean 17-06-98
	int		nTest, vColDim[3] = { 50, 200, 50 };
	char	lpszTest[50], vColNames[3][50] = { "Type", "Name", "Value" };
	FILE * p_stFile = fopen( C_SETTINGS_FILE_NAME, "r" );
	if( p_stFile )
	{	
		for( int i = 0; i < 3; i++ )
			if( fscanf( p_stFile, "%s = %d\n", lpszTest, &nTest ) != 2 )
				break;
		if( i == 3 )
		{	
			rewind( p_stFile );
			for( i = 0; i < 3; i++ )
				fscanf( p_stFile, "%s = %d\n", vColNames[i], &vColDim[i] );
		}
		fclose( p_stFile );
	}
	
//End Stefan Dumitrean 17-06-98

	CListCtrl	*pclListControl=(CListCtrl *)GetDlgItem(IDC_CONSTANT_LIST);

//Stefan Dumitrean 17-06-98
	pclListControl->InsertColumn(C_TYPE_COLUMN_NUMBER,vColNames[0],LVCFMT_LEFT,vColDim[0],C_TYPE_COLUMN_NUMBER);
	pclListControl->InsertColumn(C_NAME_COLUMN_NUMBER,vColNames[1],LVCFMT_LEFT,vColDim[1],C_NAME_COLUMN_NUMBER);
	pclListControl->InsertColumn(C_VALUE_COLUMN_NUMBER,vColNames[2],LVCFMT_LEFT,vColDim[2],C_VALUE_COLUMN_NUMBER);
//End Stefan Dumitrean 17-06-98

	POSITION	pos=m_pclConstantList->GetHeadPosition();
	while(pos!=NULL)
	{
		CPA_EdIR_Constant	*pclConstant=m_pclConstantList->GetNext(pos);

		CString	csType=pclConstant->m_fn_csGetConstantType();
		CString	csName=pclConstant->m_fn_csGetConstantName();
		CString	csValue=pclConstant->m_fn_csGetConstantValue();

		m_fn_vAddItem(csType,csName,csValue,(LPARAM)pclConstant);
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

/**********************************************************************************/
void CPA_EdIR_ConstantDialog::OnButtonNewConstant() 
{
	CPA_EdIR_NewConstantDialog	clConstantPresetDlg;

	if(clConstantPresetDlg.DoModal()==IDOK)
	{
		CPA_EdIR_Constant *pclConstant = m_pclConstantList->m_fn_pAdd(clConstantPresetDlg.m_csType,clConstantPresetDlg.m_csName,clConstantPresetDlg.m_csValue);

		m_fn_vAddItem(clConstantPresetDlg.m_csType,clConstantPresetDlg.m_csName,clConstantPresetDlg.m_csValue, (LPARAM)pclConstant);

		m_fn_vNotifySave();
	}
}

/**********************************************************************************/
void CPA_EdIR_ConstantDialog::OnButtonChangeConstant() 
{
	int	iCurSel=m_fn_iGetCurSel();
	if(iCurSel!=-1)
	{
		CListCtrl	*pclListControl=(CListCtrl *)GetDlgItem(IDC_CONSTANT_LIST);

		CString	csType=pclListControl->GetItemText(iCurSel,C_TYPE_COLUMN_NUMBER);
		CString	csName=pclListControl->GetItemText(iCurSel,C_NAME_COLUMN_NUMBER);
		CString	csValue=pclListControl->GetItemText(iCurSel,C_VALUE_COLUMN_NUMBER);

		CPA_EdIR_NewConstantDialog	clConstantPresetDlg(csType,csName,csValue);

		if(clConstantPresetDlg.DoModal()==IDOK)
		{
			m_pclConstantList->m_fn_vRemove(csName);
			CPA_EdIR_Constant *pclConstant = m_pclConstantList->m_fn_pAdd(clConstantPresetDlg.m_csType,clConstantPresetDlg.m_csName,clConstantPresetDlg.m_csValue);

			m_fn_vSetItem(iCurSel,clConstantPresetDlg.m_csType,clConstantPresetDlg.m_csName,clConstantPresetDlg.m_csValue, (LPARAM) pclConstant);

			m_fn_vNotifySave();
		}
	}
}

/**********************************************************************************/
void CPA_EdIR_ConstantDialog::OnButtonDeleteConstant() 
{
	int	iCurSel=m_fn_iGetCurSel();
	if(iCurSel!=-1)
	{
		CListCtrl	*pclListControl=(CListCtrl *)GetDlgItem(IDC_CONSTANT_LIST);

		CString	csName=pclListControl->GetItemText(iCurSel,C_NAME_COLUMN_NUMBER);
		m_pclConstantList->m_fn_vRemove(csName);

		pclListControl->DeleteItem(iCurSel);

		m_fn_vNotifySave();
	}
}

/**********************************************************************************/
void CPA_EdIR_ConstantDialog::m_fn_vNotifySave()
{
	CString	csDefineSectionName=CString(fn_szGetFamiliesDataPath())+"\\"+g_c_csAIDefineFileName;

	SCR_fn_v_SvL1_RegisterNotify(M_MAKECHAR(csDefineSectionName),
															 CPA_EdIR_Brain::m_fn_vSaveEngineAIDefines,
															 m_pclConstantList,
															 SCR_EA_Ntfy_RebuildSection);
}

/**********************************************************************************/
int CPA_EdIR_ConstantDialog::m_fn_iGetCurSel()
{
	CListCtrl	*pclListControl=(CListCtrl *)GetDlgItem(IDC_CONSTANT_LIST);

	for(int iIndex=0;iIndex<pclListControl->GetItemCount();iIndex++)
	{
		if(pclListControl->GetItemState(iIndex,LVIS_SELECTED)==LVIS_SELECTED)
			return iIndex;
	}

	return -1;
}

/**********************************************************************************/
void CPA_EdIR_ConstantDialog::m_fn_vAddItem(CString csType,CString csName,CString csValue,LPARAM lParam)
{
	CListCtrl	*pclListControl=(CListCtrl *)GetDlgItem(IDC_CONSTANT_LIST);

	LV_ITEM	stLvItem;
	stLvItem.mask=LVIF_TEXT;
	stLvItem.iItem=pclListControl->GetItemCount();
	stLvItem.iSubItem=C_TYPE_COLUMN_NUMBER;
	stLvItem.pszText=(char *)LPCTSTR(csType);
	pclListControl->InsertItem(&stLvItem);

	stLvItem.iSubItem=C_NAME_COLUMN_NUMBER;
	stLvItem.pszText=(char *)LPCTSTR(csName);
	pclListControl->SetItem(&stLvItem);

	stLvItem.iSubItem=C_VALUE_COLUMN_NUMBER;
	stLvItem.pszText=(char *)LPCTSTR(csValue);
	pclListControl->SetItem(&stLvItem);

	pclListControl->SetItemData(stLvItem.iItem,lParam);

}

/**********************************************************************************/
void CPA_EdIR_ConstantDialog::m_fn_vSetItem(int nItem,CString csType,CString csName,CString csValue,LPARAM lParam)
{
	CListCtrl	*pclListControl=(CListCtrl *)GetDlgItem(IDC_CONSTANT_LIST);

	pclListControl->SetItemText(nItem,C_TYPE_COLUMN_NUMBER,csType);
	pclListControl->SetItemText(nItem,C_NAME_COLUMN_NUMBER,csName);
	pclListControl->SetItemText(nItem,C_VALUE_COLUMN_NUMBER,csValue);
	pclListControl->SetItemData(nItem,lParam);
}

//Stefan Dumitrean 17-06-98
void CPA_EdIR_ConstantDialog::OnClose() 
{
	m_fn_vSaveSettings();
	CDialog::OnClose();
}

void CPA_EdIR_ConstantDialog::OnOK() 
{
	m_fn_vSaveSettings();		
	CDialog::OnOK();
}


void CPA_EdIR_ConstantDialog::m_fn_vSaveSettings()
{
	CListCtrl	*pclListControl=(CListCtrl *)GetDlgItem(IDC_CONSTANT_LIST);
	FILE * p_stFile = fopen( C_SETTINGS_FILE_NAME, "w" );
	LV_COLUMN stColumn;
	char pszColumnName[50];
	stColumn.mask = LVCF_TEXT | LVCF_WIDTH;
	stColumn.pszText = pszColumnName;
	stColumn.cchTextMax = 50;
	if( p_stFile )
	{	for( int i = 0; i < 3; i++ )
		{
			pclListControl->GetColumn( i, &stColumn );
			fprintf( p_stFile, "%s = %d\n", stColumn.pszText, stColumn.cx );
		}
		fclose( p_stFile );
	}

}

void CPA_EdIR_ConstantDialog::OnColumnClicked (NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNM_LISTVIEW	pNMListView		= (LPNM_LISTVIEW) pNMHDR;
	CListCtrl		*pclListControl	= (CListCtrl *) GetDlgItem(IDC_CONSTANT_LIST);
	int				iNumColumn		= pNMListView->iSubItem;

	pclListControl->SortItems(&fn_vCompareItems, iNumColumn);
}

//End Stefan Dumitrean 17-06-98

#endif //D_ED_IR_ACTIVE

