// CWizPVCCfg.cpp : implementation file
//

#include "stdafx.h"
#include "WizPVCCfg.h"
#include "service.h"
#include "pvc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CWizPVCCfg property page

IMPLEMENT_DYNCREATE(CWizPVCCfg, CConfigWizPage)

CWizPVCCfg::CWizPVCCfg(CConfigObject const * const pOwner, int const nState) 
{
	CConfigWizPage::Construct(IDD_WIZPVCCFG,0,pOwner,nState);
	m_pOwner = pOwner;
	//{{AFX_DATA_INIT(CWizPVCCfg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_bFirstInit = TRUE;
	m_pChildren = NULL;
}

CWizPVCCfg::~CWizPVCCfg()
{
}

void CWizPVCCfg::DoDataExchange(CDataExchange* pDX)
{
	CConfigWizPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWizPVCCfg)
	DDX_Control(pDX, IDC_PVCLIST, m_PVCList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CWizPVCCfg, CConfigWizPage)
	//{{AFX_MSG_MAP(CWizPVCCfg)
	ON_BN_CLICKED(IDC_AddPVC, OnAddPVC)
	ON_BN_CLICKED(IDC_RemovePVC, OnRemovePVC)
	ON_LBN_DBLCLK(IDC_PVCLIST, OnDblclkPvclist)
	ON_BN_CLICKED(IDC_PropPVC,OnPropPVC)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// any 1 time inits, since OnInitDialog can get called multiple times
void CWizPVCCfg::RefreshValues()
{
	if (!m_bFirstInit)
		return;
}
/////////////////////////////////////////////////////////////////////////////
// CWizPVCCfg message handlers

BOOL CWizPVCCfg::OnInitDialog() 
{
	CConfigWizPage::OnInitDialog();
	RefreshValues();
	m_bFirstInit = FALSE;
	BuildList();
	// 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 CWizPVCCfg::OnAddPVC() 
{
	((CService*)m_pOwner)->AddChild();
	BuildList();
}

void CWizPVCCfg::OnRemovePVC() 
{
	int sel = m_PVCList.GetCurSel();
	if (sel == LB_ERR)
		return;
	INT_PTR index = m_PVCList.GetItemData(sel);
	CConfigObject* pPVC = m_pChildren->GetAt(index);
	ASSERT(pPVC);
	((CService*)m_pOwner)->DeleteChild(pPVC);
	BuildList();	
}

// build a list of PVC entries by their descriptions and put them in the list box.
// the description is guranteed to be unique.
void CWizPVCCfg::BuildList()
{
	m_PVCList.ResetContent();
	for(int i=0;i<m_pChildren->GetSize();i++)
	{
		CPortPVC* pPVC = (CPortPVC*)(m_pChildren->GetAt(i));
		int item = m_PVCList.AddString(pPVC->GetDescription());
		m_PVCList.SetItemData(item,i);
	}
	// select the very first item in the list
	m_PVCList.SetCurSel(0);
	// enable/disable the Properties button if the window has happened 
	BOOL bShowPropBtn = (m_pChildren->GetSize() == 0)?FALSE:TRUE;
	GetDlgItem(IDC_PropPVC)->EnableWindow(bShowPropBtn);
}

// call do-properties
void CWizPVCCfg::OnDblclkPvclist() 
{
	int sel = m_PVCList.GetCurSel();	
	if (sel == LB_ERR)
	{	
		ASSERT(FALSE);
		return;
	}

	INT_PTR index = m_PVCList.GetItemData(sel);
	CConfigObject* pPVC = m_pChildren->GetAt(index);
	if (pPVC->DoProperties())
      BuildList(); // The description may have changed...

}

void CWizPVCCfg::OnPropPVC()
{
	OnDblclkPvclist();
}