// LogSettingsPage.cpp : implementation file
//

#include "stdafx.h"
#include "wmsoapsvccp.h"
#include "LogSettingsPage.h"
#include "..\soap_source\SOAPAutomationProxy.h"
#include "..\soap_source\bytearrayconv.h"
#include "..\soap_source\WMSOAPWSDLClient.h"

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

/////////////////////////////////////////////////////////////////////////////
// CLogSettingsPage property page

IMPLEMENT_DYNCREATE(CLogSettingsPage, CPropertyPage)

CLogSettingsPage::CLogSettingsPage() : CPropertyPage(CLogSettingsPage::IDD)
{
	//{{AFX_DATA_INIT(CLogSettingsPage)
	m_strServerLogPath = _T("");
	m_strSOAPTraceLogPath = _T("");
	m_strUserAgentLogPath = _T("");
	m_bServerLogEnabled = FALSE;
	m_bUserAgentLogEnabled = FALSE;
	m_bSOAPTraceLogEnabled = FALSE;
	//}}AFX_DATA_INIT
}

CLogSettingsPage::~CLogSettingsPage()
{
}

void CLogSettingsPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLogSettingsPage)
	DDX_Text(pDX, IDC_EDIT_LOG_SERVER, m_strServerLogPath);
	DDX_Text(pDX, IDC_EDIT_LOG_SOAPTRACE, m_strSOAPTraceLogPath);
	DDX_Text(pDX, IDC_EDIT_LOG_USERAGENT, m_strUserAgentLogPath);
	DDX_Check(pDX, IDC_CHECK_SVRLOG_ENABLED, m_bServerLogEnabled);
	DDX_Check(pDX, IDC_CHECK_UALOG_ENABLED, m_bUserAgentLogEnabled);
	DDX_Check(pDX, IDC_CHECK_SOAPLOG_ENABLED, m_bSOAPTraceLogEnabled);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CLogSettingsPage, CPropertyPage)
	//{{AFX_MSG_MAP(CLogSettingsPage)
	ON_EN_CHANGE(IDC_EDIT_LOG_SERVER, OnChangeEditLogServer)
	ON_EN_CHANGE(IDC_EDIT_LOG_SOAPTRACE, OnChangeEditLogSoaptrace)
	ON_EN_CHANGE(IDC_EDIT_LOG_USERAGENT, OnChangeEditLogUseragent)
	ON_BN_CLICKED(IDC_CHECK_SVRLOG_ENABLED, OnCheckSvrLogEnabled)
	ON_BN_CLICKED(IDC_CHECK_UALOG_ENABLED, OnCheckUALogEnabled)
	ON_BN_CLICKED(IDC_CHECK_SOAPLOG_ENABLED, OnCheckSOAPLogEnabled)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLogSettingsPage message handlers

void CLogSettingsPage::OnOK() 
{
	UpdateData(TRUE);
	CWSSCPApp *pApp = (CWSSCPApp *)AfxGetApp();
	if (m_bDirty == TRUE && UpdateLogSettings() == FALSE && pApp->m_bStaleNonce == true)
	{
		// server nonce stale, reauthenticate
		if (UpdateLogSettings() == FALSE)
		{
			AfxMessageBox(_T("Failed to update Logging Settings on Server!"));
		}
	}
	
	CPropertyPage::OnOK();
}

BOOL CLogSettingsPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	CWSSCPApp *pApp = (CWSSCPApp *)AfxGetApp();
	if (QueryLogSettings() == FALSE && pApp->m_bStaleNonce == true)
	{
		// server nonce stale, reauthenticate
		if (QueryLogSettings() == FALSE)
		{
			AfxMessageBox(_T("Failed to obtain Logging Settings from Server!"));
		}
	}	
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CLogSettingsPage::QueryLogSettings()
{
	CWaitCursor wait;
	CWSSCPApp *pApp = (CWSSCPApp *)AfxGetApp();
	// renew the message object
	if (pApp->m_SOAPClient.NewMessage() == false)
	{
		return FALSE;
	}
	// method
	wstring wstrMethodName = L"GetLogSettings";
	// set up call
	_variant_t Args[6];
	wstring Names[6];
	_variant_t varReturn;

	// Doc root path
	Names[0] = L"ServerLogPath";

	Names[1] = L"UserAgentLogPath";

	Names[2] = L"SOAPTraceLogPath";

	Args[3].vt = VT_BOOL;
	Args[3].boolVal = VARIANT_FALSE;
	Names[3] = L"ServerLogEnabled";

	Args[4].vt = VT_BOOL;
	Args[4].boolVal = VARIANT_FALSE;
	Names[4] = L"UserAgentLogEnabled";

	Args[5].vt = VT_BOOL;
	Args[5].boolVal = VARIANT_FALSE;
	Names[5] = L"SOAPTraceLogEnabled";

	int nNumArgs = 6;

	// generate SOAP request message headers
	pApp->BuildRequestHeaders();

	// message buffer strings
	string strRequestXML;
	string strResponseXML;

	// attempt method invocation
	bool bFault = false;
	if (pApp->m_SOAPClient.InvokeSOAPMethod(wstrMethodName,
											Names,
											Args,
											nNumArgs,
											varReturn) == false)
	{
		bFault = true;
	}
	if (pApp->m_bViewBuffers == true)
	{
		MessageBox(pApp->m_SOAPClient.GetXMLOut(), "Request Message", MB_OK);
		MessageBox(pApp->m_SOAPClient.GetXMLIn(), "Response Message", MB_OK);
	}
	// load response message headers
	pApp->GetResponseHeaders();

	if (bFault == true ||
		varReturn.boolVal == VARIANT_FALSE ||
		pApp->m_SOAPClient.IsSOAPFault() == true ||
		pApp->AuthServer() == false)
	{
		return FALSE;
	}
	// extract Server log path
	m_strServerLogPath = Args[0].bstrVal;
	// extract User Agent log path
	m_strUserAgentLogPath = Args[1].bstrVal;
	// extract SOAP Trace log path
	m_strSOAPTraceLogPath = Args[2].bstrVal;
	// flags
	m_bServerLogEnabled = Args[3].boolVal == VARIANT_TRUE ? TRUE : FALSE;
	m_bUserAgentLogEnabled = Args[4].boolVal == VARIANT_TRUE ? TRUE : FALSE;
	m_bSOAPTraceLogEnabled = Args[5].boolVal == VARIANT_TRUE ? TRUE : FALSE;

	UpdateData(FALSE);
	return TRUE;
}

BOOL CLogSettingsPage::UpdateLogSettings()
{
	CWaitCursor wait;
	CWSSCPApp *pApp = (CWSSCPApp *)AfxGetApp();
	// renew the message object
	if (pApp->m_SOAPClient.NewMessage() == false)
	{
		return FALSE;
	}
	// method
	wstring wstrMethodName = L"SetLogSettings";
	// set up call
	_variant_t Args[6];
	wstring Names[6];
	_variant_t varReturn;
	
	// Server log path
	Args[0] = m_strServerLogPath.AllocSysString();
	Names[0] = L"ServerLogPath";
	// User Agent log path
	Args[1] = m_strUserAgentLogPath.AllocSysString();
	Names[1] = L"UserAgentLogPath";
	// SOAP Trace log path
	Args[2] = m_strSOAPTraceLogPath.AllocSysString();
	Names[2] = L"SOAPTraceLogPath";
	// flags
	Args[3] = m_bServerLogEnabled == TRUE ? VARIANT_TRUE : VARIANT_FALSE;
	Names[3] = L"ServerLogEnabled";

	Args[4] = m_bUserAgentLogEnabled == TRUE ? VARIANT_TRUE : VARIANT_FALSE;
	Names[4] = L"UserAgentLogEnabled";

	Args[5] = m_bSOAPTraceLogEnabled == TRUE ? VARIANT_TRUE : VARIANT_FALSE;
	Names[5] = L"SOAPTraceLogEnabled";

	int nNumArgs = 6;

	// generate SOAP request message headers
	pApp->BuildRequestHeaders();

	// message buffer strings
	string strRequestXML;
	string strResponseXML;

	// attempt method invocation
	bool bFault = false;
	if (pApp->m_SOAPClient.InvokeSOAPMethod(wstrMethodName,
											Names,
											Args,
											nNumArgs,
											varReturn) == false)
	{
		bFault = true;
	}
	if (pApp->m_bViewBuffers == true)
	{
		MessageBox(pApp->m_SOAPClient.GetXMLOut(), "Request Message", MB_OK);
		MessageBox(pApp->m_SOAPClient.GetXMLIn(), "Response Message", MB_OK);
	}
	// load response message headers
	pApp->GetResponseHeaders();

	if (bFault == true ||
		varReturn.boolVal == VARIANT_FALSE ||
		pApp->m_SOAPClient.IsSOAPFault() == true ||
		pApp->AuthServer() == false)
	{
		return FALSE;
	}
	m_bDirty = FALSE;
	return TRUE;
}


void CLogSettingsPage::OnChangeEditLogServer() 
{
	SetModified();
	m_bDirty = TRUE;
}

void CLogSettingsPage::OnChangeEditLogSoaptrace() 
{
	SetModified();
	m_bDirty = TRUE;
}

void CLogSettingsPage::OnChangeEditLogUseragent() 
{
	SetModified();
	m_bDirty = TRUE;
}

void CLogSettingsPage::OnCheckSvrLogEnabled() 
{
	SetModified();
	m_bDirty = TRUE;
}

void CLogSettingsPage::OnCheckUALogEnabled() 
{
	SetModified();
	m_bDirty = TRUE;
}

void CLogSettingsPage::OnCheckSOAPLogEnabled() 
{
	SetModified();
	m_bDirty = TRUE;
}
