// x25router.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "router.h"
#include "routercfg.h"

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


/////////////////////////////////////////////////////////////////////////////
// CRouteDlg dialog


CRouteDlg::CRouteDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRouteDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRouteDlg)
	m_Address = _T("");
	//}}AFX_DATA_INIT
}


void CRouteDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRouteDlg)
	DDX_Control(pDX, IDC_RoutingAliasList, m_RoutingAliasList);
	DDX_Text(pDX, IDC_Address, m_Address);
	DDV_MaxChars(pDX, m_Address, 17);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CRouteDlg, CDialog)
	//{{AFX_MSG_MAP(CRouteDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRouteDlg message handlers


BOOL CRouteDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	// TODO: Add extra initialization here
	CString Alias;
	CAlias Value;
	POSITION pos = m_AliasMap->GetStartPosition();

	while (pos) {
		m_AliasMap->GetNextAssoc(pos, Alias, Value);
		if (Value.m_NumPorts > 0)
			m_RoutingAliasList.InsertString(-1, Alias);
		}

	if (m_RoutingAliasList.SelectString(-1, m_Alias) == CB_ERR)
		m_RoutingAliasList.SetCurSel(0);

	if (m_RoutingAliasList.GetCount() == 0) {
		EndDialog(IDCANCEL);
		AfxMessageBox(IDS_NeedSVCBeforeRoute);
		}

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CRouteDlg::OnOK() 
{
	// TODO: Add extra validation here
	m_RoutingAliasList.GetLBText(m_RoutingAliasList.GetCurSel(), m_Alias);

	CDialog::OnOK();
}



/////////////////////////////////////////////////////////////////////////////
// CX25RouterPage property page

IMPLEMENT_DYNCREATE(CX25RouterPage, CPropertyPage)
CX25RouterPage::CX25RouterPage() : CPropertyPage(CX25RouterPage::IDD)
{
	m_pRouter = NULL;
}

CX25RouterPage::CX25RouterPage(CX25Router* pRouter) : CPropertyPage(CX25RouterPage::IDD)
{
	//{{AFX_DATA_INIT(CX25RouterPage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	ASSERT(pRouter);
	m_pRouter = pRouter;
}

CX25RouterPage::~CX25RouterPage()
{
}

void CX25RouterPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CX25RouterPage)
	DDX_Control(pDX, IDC_RoutesList, m_RoutesList);
	//}}AFX_DATA_MAP
	if (pDX->m_bSaveAndValidate)
		if (m_pRouter->FindHiddenRoutes() == IDCANCEL) {
			pDX->PrepareCtrl(IDC_MoveUp);
			pDX->Fail();
			}
}


BEGIN_MESSAGE_MAP(CX25RouterPage, CPropertyPage)
	//{{AFX_MSG_MAP(CX25RouterPage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CX25RouterPage message handlers

void CX25RouterPage::OnOK() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CPropertyPage::OnOK();
}

BOOL CX25RouterPage::OnInitDialog() 
{
	// SetParams must be called
	ASSERT(m_pRouter);					
	CPropertyPage::OnInitDialog();

	
	CDC *dc = GetDC();
	TEXTMETRIC tm;

	dc->GetTextMetrics(&tm);
	ReleaseDC(dc);
	
	int xunit = LOWORD(GetDialogBaseUnits());
	m_RoutesList.SetTabStops((tm.tmAveCharWidth * 22 * 4) / xunit);

	m_RoutesList.ResetContent();
	for (short i = 0; i < m_pRouter->CountRoutes(); i++)
		m_RoutesList.AddString(m_pRouter->GetRoute(i)->m_Address + _T('\t') + m_pRouter->GetRoute(i)->m_Alias);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}




BOOL CX25RouterPage::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) 
{
	int SelCount = m_RoutesList.GetSelCount();
	int *SelItems = new int[SelCount];
	BOOL success = FALSE;

	m_RoutesList.GetSelItems(SelCount, SelItems);
	if (nCode == BN_CLICKED)
		switch (nID) {
			case IDC_ChangeRoute :
				success = m_pRouter->ChangeRoute(SelCount, SelItems);
				break;
			case IDC_AddRoute :
				success = m_pRouter->AddRoute(SelCount, SelItems);
				break;
			case IDC_RemoveRoute :
				success = m_pRouter->RemoveRoute(SelCount, SelItems);
				SelCount = 0;
				break;
			case IDC_MoveUp :
				if (SelCount > 0) {
					for (short i = 0; i < SelCount; i++)
						if (m_pRouter->MoveUp(SelItems[i]))
							SelItems[i]--;
					success = TRUE;
					}
				break;
			case IDC_MoveDown :
				if (SelCount > 0) {
					for (short i = SelCount-1; i >= 0; i--)
						if (m_pRouter->MoveDown(SelItems[i]))
							SelItems[i]++;
					success = TRUE;
					}
				break;
			}
	else if (nCode == LBN_DBLCLK && nID == IDC_RoutesList)
		success = m_pRouter->ChangeRoute(SelCount, SelItems);

	if (success) {
		m_RoutesList.ResetContent();
		for (short i = 0; i < m_pRouter->CountRoutes(); i++)
			m_RoutesList.AddString(m_pRouter->GetRoute(i)->m_Address + _T('\t') + m_pRouter->GetRoute(i)->m_Alias);
		for (i = 0; i < SelCount; i++)
			if (m_RoutesList.SetSel(SelItems[i]) == LB_ERR)
				break;

		if (i == 0)
			m_RoutesList.SetSel(0);
		}

	delete SelItems;

	
	return CPropertyPage::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}





/*

void AddRoute(const CString& szDevice,const CString& szline,int nsvcports)
{
	theRouter.AddAlias(szDevice,szline.Right(1),szline);
	if (nsvcports > 0)
		theRouter.SetAliasNumPorts(szline,nsvcports);
}

void RemoveRoute(const CString& szDevice,const CString& szline)
{
	theRouter.RemoveRoutingAlias(szDevice,szline.Right(1));
}

void AddRouterPorts(const CString& szAlias,int ports)
{
	theRouter.SetAliasNumPorts(szAlias,ports);
}
*/