// Login.cpp : implementation file
//

#include "stdafx.h"
#include "DB_APIVCClient.h"
#include "Login.h"

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

/////////////////////////////////////////////////////////////////////////////
// CLogin dialog


CLogin::CLogin(CWnd* pParent /*=NULL*/)
	: CDialog(CLogin::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLogin)
	m_UserName = _T("");
	m_Password = _T("");
	m_DomainName = _T("");
	//}}AFX_DATA_INIT
}

CLogin ::CLogin(IApplication *pApp): CDialog(CLogin::IDD, NULL)

{
	pApplication =pApp;
}
void CLogin::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLogin)
	DDX_Control(pDX, IDC_USERNAME, m_Cnt_username);
	DDX_Control(pDX, IDC_PASSWD, m_cnt_passwd);
	DDX_Text(pDX, IDC_USERNAME, m_UserName);
	DDX_Text(pDX, IDC_PASSWD, m_Password);
	DDX_Text(pDX, IDC_DOMAIN, m_DomainName);
	//DDX_Control(pDX, IDC_CMBDOMAIN, m_CmbDomain);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CLogin, CDialog)
	//{{AFX_MSG_MAP(CLogin)
	ON_BN_CLICKED(IDOK, OnLoginOk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLogin message handlers

void CLogin::OnLoginOk() 
{
		
	long lUserID;
	BSTR bstrUser;
	BSTR bstrPassword;
	BSTR bstrDomainName;

	//CString sDomainName;

	//int iSel = m_CmbDomain.GetCurSel();
	//if(m_CmbDomain.GetCount() > 0 && iSel<0)
	//	return;
	//m_CmbDomain.GetLBText(iSel, sDomainName);

	try
	{
		UpdateData(TRUE);
		bstrUser = m_UserName.AllocSysString();
		bstrPassword = m_Password.AllocSysString();
		bstrDomainName = m_DomainName.AllocSysString();
		OutputDebugString(m_UserName);
		OutputDebugString(m_DomainName);
		// DIT Bugfix for the Access to Invalid Login on the Services Stop -- Guru
		HRESULT hr = pApplication->Login(bstrUser,bstrPassword,bstrDomainName,&lUserID);
		if (lUserID<=0 || FAILED(hr))
		{
			AfxMessageBox(L"Login failed");
			m_Cnt_username.SetWindowText(L"");
			m_cnt_passwd.SetWindowText(L"");
			m_Cnt_username.SetFocus();
			return;
		}
		else
		{
			AfxMessageBox(L"Login succeeded");
			CDialog::OnOK();
		}
		
		UpdateData(FALSE);
	}
	catch (_com_error &e)
	{
		BSTR bstrError = e.Description();
		
	}
	

}

BOOL CLogin::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	//m_CmbDomain.Clear();

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

void CLogin::LoadDomainNames()
{
	/*
	_variant_t vDomains;
	vDomains.Clear();
	pApplication->GetConfiguredWPDomains(&vDomains);

	if(vDomains.vt == VT_EMPTY)
	{
		AfxMessageBox(_T("No Domains or Logon without Domain Credentials"));
		return;
	}
	
	long uBound =0 ;
	
	COleSafeArray sa(vDomains);
	sa.GetUBound(1,&uBound);
	UpdateData(TRUE);
	BSTR bstrDomain;
	for(long i =0 ;i <= uBound ; i++)
	{
		sa.GetElement(&i,(void*)&bstrDomain);
		CString sDomain = CString(bstrDomain);
		//AfxMessageBox(scard);
		m_CmbDomain.AddString(sDomain);
	}
	m_CmbDomain.SetCurSel(0);
	UpdateData(FALSE);*/
}