/*
*********************************************************************
*
*	The Essence of OLE
*
*	Source code sample
*
*	Copyright (c) 1996, Rolling Thunder Computing, Ipswich MA
*
*	e-mail: essence@rollthunder.com
*	CompuServe: 73540,3430
*	Web: http://www.rollthunder.com
*
*	File Name: CELIZA.CPP
*
*	Purpose: Implmentation of IEliza interface for local server
*
*********************************************************************
*/

#include <windows.h>

#include "celiza.h"

extern "C" void Eliza (TCHAR *InStr, TCHAR *OutStr) ;

CEliza::CEliza (void)
{
    m_RefCount = 1 ;
    return;
}

extern int LockCount, ObjectCount ;


CEliza::~CEliza(void)
{
	ObjectCount -- ;

	if (LockCount == 0 && ObjectCount == 0)
	{	
		PostQuitMessage (0) ;
	}
}



STDMETHODIMP CEliza::QueryInterface(REFIID riid, LPVOID *ppv)
{
    if (riid == IID_IUnknown || riid == IID_ITypeLibMarshaledEliza)
	{
        *ppv = (LPVOID)this;
		AddRef( ) ;
		return NOERROR;
	}
	else
	{
    	*ppv = NULL ;
    	return E_NOINTERFACE ;
	}
        
}


STDMETHODIMP_(ULONG) CEliza::AddRef(void)
{
    return ++m_RefCount;
}


STDMETHODIMP_(ULONG) CEliza::Release(void)
{
	m_RefCount -- ;

    if (m_RefCount == 0)
	{
        delete this;
		return 0 ;
	}

    return m_RefCount ;
}


STDMETHODIMP CEliza::Complain (BSTR In, BSTR *pOut)
{
	if (In != 0x00)
	{
//		MessageBox (NULL, In, L"This complaint", 0) ;

 		TCHAR out [256] ;
		wsprintf (out, TEXT("Client: %s%c%c"), In, 0x0D, 0x0A) ;
		SendMessage (m_hEdit, EM_SETSEL, (WPARAM) -1, (LPARAM)-1) ;
		SendMessage (m_hEdit, EM_REPLACESEL, 0, (LPARAM) out) ;

		TCHAR stringOut [256] ;
		stringOut [0] = 0x00 ;

		Eliza (In, stringOut) ;
		SysReAllocString (pOut, stringOut) ;

//		MessageBox (NULL, stringOut, L"This response", 0) ;

		SendMessage (m_hEdit, EM_SETSEL, (WPARAM) -1, (LPARAM)-1) ;
		SendMessage (m_hEdit, EM_REPLACESEL, 0, (LPARAM) stringOut) ;
	}

	return NOERROR ;
}
