// HwWizProtocolPropPage.cpp : implementation file
//

#include "stdafx.h"
#include "digiinf.h"
#include "DriverServices.h"
#include "ProtocolDisplayNameMap.h"
#include "HwWizProtocolPropPage.h"

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

/////////////////////////////////////////////////////////////////////////////
// CHwWizProtocolPropPage property page

IMPLEMENT_DYNCREATE(CHwWizProtocolPropPage, CPropertyPageEx)

CHwWizProtocolPropPage::CHwWizProtocolPropPage() 
   : CPropertyPageEx(CHwWizProtocolPropPage::IDD, 0, IDS_PROTOCOL_HEADER, IDS_PROTOCOL_SUB_HEADER)
{
	//{{AFX_DATA_INIT(CHwWizProtocolPropPage)
	m_ProtocolString = _T("");
	//}}AFX_DATA_INIT
   m_nProtocolIndex = NO_PROTOCOL_INDEX;
}

CHwWizProtocolPropPage::~CHwWizProtocolPropPage()
{

}

void CHwWizProtocolPropPage::SetPropPageParams(HDEVINFO const DeviceInfoSet,
                                               PSP_DEVINFO_DATA const DeviceInfoData,
                                               CStringArray const & astrProtocolChoices,
                                               CString AdapterIdentityString)
{
   // NT5 class installer provided data
   ASSERT(DeviceInfoSet != NULL);
   ASSERT(DeviceInfoData != NULL);
   m_PropPageParams.DeviceInfoSet = DeviceInfoSet;
   m_PropPageParams.DeviceInfoData = DeviceInfoData;

   // Set up the strings that will be put in the combo box
   m_astrChoices.RemoveAll();
   AffirmThrow(astrProtocolChoices.GetSize());
   m_astrChoices.Copy(astrProtocolChoices);

   // Set up the prop sheet page's styles for use when the page is created
   CString Title;
   m_psp.dwFlags |= PSP_USETITLE;   // Always use a dynamic title
   Title.Format(IDS_PROTOCOL_SHEET_TITLE_FORMAT, AdapterIdentityString);
   SetTitle(Title);

   // Turn the wiz help button off, we don't have any help for this now
   m_psp.dwFlags &= ~PSP_HASHELP;
   // Turn on the wiz page header
   m_psp.dwFlags &= ~PSP_HIDEHEADER;
   m_psp.dwFlags |= PSP_USEHEADERSUBTITLE;
   m_psp.dwFlags |= PSP_USEHEADERTITLE;

   // OK, now for the kludge, fill in what we have and then set the superset members
   memcpy(&m_kludge_psp, &m_psp, sizeof m_psp);
   m_kludge_psp.dwSize = sizeof(PROPSHEETPAGE);
   m_kludge_psp.pszHeaderTitle = MAKEINTRESOURCE(IDS_PROTOCOL_HEADER);
   m_kludge_psp.pszHeaderSubTitle = MAKEINTRESOURCE(IDS_PROTOCOL_SUB_HEADER);
}

void CHwWizProtocolPropPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPageEx::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHwWizProtocolPropPage)
	DDX_Control(pDX, IDC_STATIC_PROTOCOL, m_wPhoneProtocol);
	DDX_Control(pDX, IDC_STATIC_PROTOCOL_TEXT, m_wProtocolInstruction);
	DDX_Control(pDX, IDC_COMBO_PROTOCOL, m_ComboProtocol);
	DDX_CBString(pDX, IDC_COMBO_PROTOCOL, m_ProtocolString);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CHwWizProtocolPropPage, CPropertyPageEx)
	//{{AFX_MSG_MAP(CHwWizProtocolPropPage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHwWizProtocolPropPage message handlers

BOOL CHwWizProtocolPropPage::OnInitDialog() 
{
	CPropertyPageEx::OnInitDialog();

   // Fill the combo box with the specified selections and instruction text
   try
   {
      CString IdsString;
      IdsString.LoadString(IDS_PROTOCOL_INSTRUCTION);
      m_wProtocolInstruction.SetWindowText(IdsString);
      IdsString.LoadString(IDS_PHONE_LINE_PROTOCOL);
      m_wPhoneProtocol.SetWindowText(IdsString);

      CProtocolDisplayNameMap mapProtocolDisplayName;
      AffirmThrow(m_astrChoices.GetSize() > 0);
      m_ComboProtocol.ResetContent();
      for (int i = 0; i < m_astrChoices.GetSize(); i++)
      {
         // NOTE: NOTE: Make sure the indexes match up
         AffirmThrow(m_ComboProtocol.AddString(mapProtocolDisplayName[m_astrChoices[i]]) == i);
         TRACE("CHwWizProtocolPropPage::OnInitDialog(): Added: %s, display: %s\n",m_astrChoices[i], mapProtocolDisplayName[m_astrChoices[i]]);
      }
      // The first one is the default...
      m_ComboProtocol.SetCurSel(0);
      m_ComboProtocol.SetFocus();
   }
   catch (CAffirmException ae)
   {
   }

   return FALSE;   // We set focus to the Protocol Combo...
}

LRESULT CHwWizProtocolPropPage::OnWizardNext() 
{
   TRACE("CHwWizProtocolPropPage::OnWizardNext()\n");

   BOOL fRestart = TRUE;
   CString strProtocol;

   // Get the user's selection
   UpdateData(TRUE);
   int nProtocolIndex = m_ComboProtocol.GetCurSel();

   try
   {
      // In case we come back, don't redo the same settings...
      if (m_nProtocolIndex != nProtocolIndex)
      {
         TRACE("CHwWizProtocolPropPage::OnWizardNext(): Protocol changed! (or new config)\n");

         // NOTE: NOTE: The indexes _must_ match up...
         AffirmThrow(nProtocolIndex < m_astrChoices.GetSize());
         strProtocol = m_astrChoices[nProtocolIndex];
         TRACE("CHwWizProtocolPropPage::OnWizardNext(): Setting protocol %s\n", strProtocol);
      
         // The MS ISDN Wiz should only run if we're one of the ISDN protocols...
         if (0 == strProtocol.Compare(DXB_REGVAL_PROTOCOL_ENUM_PRI))
         {
            fRestart = FALSE; // ISDN wiz will restart...
         }

         // Actually commit to the settings....
         if (NO_PROTOCOL_INDEX != m_nProtocolIndex)
         {
            // We've been here before, remove the params we wrote
            RemoveAdapterParams(m_PropPageParams.DeviceInfoSet, m_PropPageParams.DeviceInfoData);
         }

         // Write just enough for the adapter object to figure out the rest
         WriteAdapterLineProtocol(  m_PropPageParams.DeviceInfoSet,
                                    m_PropPageParams.DeviceInfoData,
                                    strProtocol);
         WriteAdapterParams(m_PropPageParams.DeviceInfoSet, m_PropPageParams.DeviceInfoData);

         // fRestart implies that we're the last page and have no way of restarting later,
         // This is the case when we are not handing off to the ISDN Wiz
         if (fRestart)
         {
            // restart the driver
            TRACE("CHwWizProtocolPropPage:: About to restart Dxb\n");
            SP_PROPCHANGE_PARAMS pcp = { 0 };
            pcp.ClassInstallHeader.cbSize = sizeof pcp.ClassInstallHeader;
            pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
            pcp.StateChange = DICS_PROPCHANGE;
            pcp.Scope = DICS_FLAG_GLOBAL;

            if (SetupDiSetClassInstallParams(m_PropPageParams.DeviceInfoSet,
                                             m_PropPageParams.DeviceInfoData,
                                             &pcp.ClassInstallHeader,
                                             sizeof pcp))
            {
               SetupDiCallClassInstaller( DIF_PROPERTYCHANGE,
                                          m_PropPageParams.DeviceInfoSet,
                                          m_PropPageParams.DeviceInfoData);
            }
         }

         m_nProtocolIndex = nProtocolIndex;
      }
   }
   catch (CAffirmException ae)
   {
   }

   return CPropertyPageEx::OnWizardNext();
}

BOOL CHwWizProtocolPropPage::OnSetActive() 
{

   CPropertySheet* psheet = (CPropertySheet*) GetParent();
   // Only enable [Next], [Back] has no meaning for us
   psheet->SetWizardButtons(PSWIZB_NEXT);
   	
	return CPropertyPageEx::OnSetActive();
}

BOOL CHwWizProtocolPropPage::OnWizardFinish() 
{
   TRACE("************ CHwWizProtocolPropPage::OnWizardFinish() ************\n");
   return CPropertyPageEx::OnWizardFinish();
}
