// ClientDlg.h : Declaration of the CClientDlg

#ifndef __CLIENTDLG_H_
#define __CLIENTDLG_H_

#include "resource.h"       // main symbols
#include <atlhost.h>

#include<string>

typedef CComBSTR CString;

/////////////////////////////////////////////////////////////////////////////
// CClientDlg
class CClientDlg : 
	public CAxDialogImpl<CClientDlg>
{
public:
	CClientDlg()
	{
	}

	~CClientDlg()
	{
	}

	enum { IDD = IDD_CLIENTDLG };

BEGIN_MSG_MAP(CClientDlg)
	MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
	COMMAND_ID_HANDLER(IDOK, OnOK)
	COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
END_MSG_MAP()


	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		return 1;  // Let the system set the focus
	}

	LRESULT OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{

	// declare SOAP Client Object
	ISoapClientPtr m_pSoapClient;

	// WSDL location
	_bstr_t WsdlFile="http://www.redcoal.com/xmlserver/soapserver.dll/wsdl/ISoapServer";

try{
	  // create SOAP Client Instance
 	  HRESULT hr = m_pSoapClient.CreateInstance(__uuidof(SoapClient30));
	  if( FAILED(hr) ) {
	  }

	  // initiliase MS SOAP Client
	  m_pSoapClient->MSSoapInit2(
				_variant_t(WsdlFile), 
				_variant_t(_T("")), 
				_T(""), 
				_T(""), 
				_T("")
			);
		
	    BSTR FaultString;

		// if it's failed to initiliase then , find out why by getting fault string
		hr = m_pSoapClient->get_FaultString(&FaultString);
		
		if(FAILED(hr))
		{
		}else{
			CString Msg("");
			Msg += FaultString;
			//CFile mFile("d:\\vcfile.txt",2);
			//mFile.Write(Msg,Msg.GetLength());
			//mFile.Close(); 
		}
		SysFreeString(FaultString);
	
	// now prepare to call the SOAP Method SendTextSMS
	DISPPARAMS params;
	VARIANT arg[9];
	EXCEPINFO excepinfo;
	unsigned int uArgErr;
    WCHAR       *pName    = L"SendTextSMS";
	_variant_t result;
	static DISPID dispid = -1;

	hr=m_pSoapClient->GetIDsOfNames(IID_NULL,&pName,1,LOCALE_SYSTEM_DEFAULT,&dispid);

	CString Msg;
	if( FAILED(hr) ) 
	{
		Msg = L"Cannot get dispatch id";
   		//CFile mFile("d:\\vcfile.txt",2);
		//mFile.Write(Msg,Msg.GetLength());
		//mFile.Close(); 
	}else{
		// Note: you have to pass parameters in reverse order
		::VariantInit(&arg[0]); V_VT(&arg[0])    = VT_INT; V_INT(&arg[0])  = 0;
		::VariantInit(&arg[1]); V_VT(&arg[1])    = VT_BSTR; V_BSTR(&arg[1])  = ::SysAllocString(L"");
		::VariantInit(&arg[2]); V_VT(&arg[2])    = VT_INT; V_INT(&arg[2])  = 0;
		::VariantInit(&arg[3]); V_VT(&arg[3])    = VT_BSTR; V_BSTR(&arg[3])  = ::SysAllocString(L"myName");
		::VariantInit(&arg[4]); V_VT(&arg[4])    = VT_BSTR; V_BSTR(&arg[4])  = ::SysAllocString(L"myEmail");
		::VariantInit(&arg[5]); V_VT(&arg[5])    = VT_BSTR; V_BSTR(&arg[5])  = ::SysAllocString(L"myMessage");
		::VariantInit(&arg[6]); V_VT(&arg[6])    = VT_BSTR; V_BSTR(&arg[6])  = ::SysAllocString(L"61413328444");
		::VariantInit(&arg[7]); V_VT(&arg[7])    = VT_BSTR; V_BSTR(&arg[7])  = ::SysAllocString(L"abc1234");
		::VariantInit(&arg[8]); V_VT(&arg[8])    = VT_BSTR; V_BSTR(&arg[8])  = ::SysAllocString(L"94765-49251-11951-66295");

		params.cArgs = 7;
		params.rgvarg = arg;
		params.cNamedArgs = 0;
		params.rgdispidNamedArgs = NULL;

		memset(&excepinfo, 0, sizeof(excepinfo));
		uArgErr = -1;
		
		// invoke SOAP method ( SendTextSMS )
		hr = m_pSoapClient->Invoke(
				dispid, 
				IID_NULL, 
				LOCALE_SYSTEM_DEFAULT, 
				DISPATCH_METHOD, 
				&params,
				&result, 
				&excepinfo, 
				&uArgErr);

		if(FAILED(hr))
		{
			Msg = L"Invoke failed!!";
			//Msg +=(_bstr_t)m_pSoapClient->FaultString;
   			//CFile mFile("d:\\vcfile.txt",2);
			//mFile.Write(Msg,Msg.GetLength());
			//mFile.Close(); 
		}

		// get the return value (or error response)
		int mtemp=result.intVal;
		// see readme file for description of return values

        Msg=mtemp; 
		//CFile mFile("d:\\vcfile.txt",2);
		//mFile.Write(Msg,Msg.GetLength());
		//mFile.Close(); 
		}
	}catch(_com_error err){
	    DisplayError(err);    
		return false;
	}
   return 0;
   }

	LRESULT OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
        DestroyWindow();
        PostQuitMessage(0);
        return 0;
	}

	void DisplayError(const _com_error& Error)
	{

	CString Message;

	// If the _com_error object contains a description, append it
	// to Message.
	/*
	if( (wchar_t*)Error.Description() != L"" )
	{
		Message += " ";
		Message += Error.Description();
	}*/

	// If the _com_error object specifies a failure HRESULT,
	// other then the generic E_FAIL, get the error message 
	// that goes with that HRESULT and append it to Message. 
	// Also append the HRESULT value formatted as a number.
	if (FAILED(Error.Error()) && (Error.Error() != E_FAIL))
	{

		Message += " ";
		Message += Error.ErrorMessage();

		CString strHR;
		strHR=Error.Error();
		//strHR.Format(" (0x%08lX)", Error.Error());

		Message += strHR;

	}

   	//CFile mFile("d:\\vcfile.txt",2);
	//mFile.Write(Message,Message.GetLength());
//	mFile.Close(); 
	//AfxMessageBox(Message,MB_OK | MB_ICONEXCLAMATION,0);
	}
};


#endif //__CLIENTDLG_H_
