// ProtocolSelect.cpp : implementation file
//

#include "stdafx.h"
#include "ProtocolSelect.h"
#include "LineWanConfig.h"

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

/////////////////////////////////////////////////////////////////////////////
// CProtocolSelect property page

IMPLEMENT_DYNCREATE(CProtocolSelect, CPropertyPage)

CProtocolSelect::CProtocolSelect() : CPropertyPage(CProtocolSelect::IDD)
{
	//{{AFX_DATA_INIT(CProtocolSelect)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_bNoWanPage = TRUE;
}

CProtocolSelect::~CProtocolSelect()
{
}

void CProtocolSelect::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CProtocolSelect)
	DDX_Control(pDX, IDC_PROTOCOLLIST, m_ProtocolList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CProtocolSelect, CPropertyPage)
	//{{AFX_MSG_MAP(CProtocolSelect)
	ON_LBN_SELCHANGE(IDC_PROTOCOLLIST, OnSelchangeProtocollist)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProtocolSelect message handlers

void CProtocolSelect::OnSelchangeProtocollist() 
{
	int sel = m_ProtocolList.GetCurSel();
	ASSERT(sel != LB_ERR);
	if (sel != LB_ERR)
	{
		m_nProtocolIndex = sel;
		AddRemoveWanPage();
	}
}

BOOL CProtocolSelect::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	m_ProtocolList.ResetContent();
	ASSERT(m_astrProtocols.GetSize());

	for(int i=0;i<m_astrProtocols.GetSize();i++)
	{
		m_ProtocolList.AddString(m_astrProtocols[i]);
	}
	m_ProtocolList.SetCurSel(m_nProtocolIndex);

	AddRemoveWanPage();
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

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

void CProtocolSelect::SetParams(CStringArray& astrAllowed,CStringArray& astrNoWAN,CStringArray& astrNoWarn,int sel,CPropertyPage* pWanPage)
{
	ASSERT_KINDOF(CPropertyPage,pWanPage);
	m_pPage = pWanPage;
	m_astrProtocols.Copy(astrAllowed);
	m_astrProtocolsNoWAN.Copy(astrNoWAN);
   m_astrProtocolsNoWanPageWarn.Copy(astrNoWarn);
	m_nProtocolIndex = sel;
}

// if the currently selected protocol is within the NoWAN protocols, hide the
// WAN propert page. 
void CProtocolSelect::AddRemoveWanPage()
{
	CString szCurrentProtocol(m_astrProtocols[m_nProtocolIndex]);
	ASSERT(!szCurrentProtocol.IsEmpty());
	BOOL bRemoveWanPage = FALSE;
	for(int i=0;i<m_astrProtocolsNoWAN.GetSize() && !bRemoveWanPage;i++)
	{
		if (szCurrentProtocol == m_astrProtocolsNoWAN[i])
			bRemoveWanPage = TRUE;
	}

	CPropertySheet* pSheet = (CPropertySheet*)GetParent();
	// if we are removing the page
	if (bRemoveWanPage)
	{
		// and the page hasn't been removed
		if (!m_bNoWanPage)
		{
			// remove the page
			m_bNoWanPage = TRUE;
			pSheet->RemovePage(m_pPage);
		}
	}
	else
	{   
		// if the page has been removed
		if (m_bNoWanPage)
		{
			// insert it
			m_bNoWanPage = FALSE;
			pSheet->AddPage(m_pPage);
		}
	}

   // For specific protocols, the WAN Page shouldn't warn about baud rate changes
   if (!m_bNoWanPage)
   {
      ASSERT(NULL != m_pPage);
      // Make sure it's the right class before we touch the accessor
      if (m_pPage->IsKindOf(RUNTIME_CLASS(CWanConfig)))
      {
	      BOOL bWanPageBaudChangeWarning = TRUE;
	      for(int i=0;i<m_astrProtocolsNoWanPageWarn.GetSize() && bWanPageBaudChangeWarning;i++)
	      {
		      if (szCurrentProtocol == m_astrProtocolsNoWanPageWarn[i])
			      bWanPageBaudChangeWarning = FALSE;
	      }

         ((CWanConfig *)m_pPage)->SetBaudChangeWarning(bWanPageBaudChangeWarning);
      }
   }
}
