// MyScript.cpp : Implementation of CMyScript
#include "stdafx.h"
#include "NativeScriptDemo.h"
#include "MyScript.h"

/////////////////////////////////////////////////////////////////////////////
// CMyScript

CMyScript::CMyScript() : 
	  fieldsAreOk(false)
		
{
  
  // init out buffer
  outValueMax=64;
  outValueCnt = 0;
  outValue = NULL;
  outValue = (BSTR *) calloc(outValueMax,sizeof(BSTR));
  
}

CMyScript::~CMyScript() 
{
			// free the strings , alloctated by getValue
		FreeBSTRs(&outValue,&outValueCnt);

}

// Script methods 


// script instance is created
// we get the pointer to the owning Script node
// inorder to get access to fields (and browser object)

STDMETHODIMP CMyScript::setContainer(Node * container)
{
	script = container;
	if (script) {

		// get any field pointers  needed 

		Field *tmp=NULL;

		// modify here : 
		script->getField( L"serverUrl",&tmp);
		serverUrl = tmp; RELEASE(tmp); 

		script->getField( L"echo",&tmp);
		echo = tmp; RELEASE(tmp); 

		script->getField( L"receive",&tmp);
		receive = tmp; RELEASE(tmp); 



		fieldsAreOk= (serverUrl) && (echo) && (receive);

	} else {
		// important zero pointers to clean up cyclic references
		serverUrl.Release();
		echo.Release();
		receive.Release();

		fieldsAreOk = false;

	}
	return S_OK;
}

STDMETHODIMP CMyScript::loadScriptObject(BSTR urlData)
{
	// 
	return E_NOTIMPL;
}

STDMETHODIMP CMyScript::initialize()
{

	// protect against bad , missing fields 
	if (!fieldsAreOk)
		return E_POINTER;
	
	// test 
	AppendOut(_T("debug:initialize"));

	return S_OK;

}

STDMETHODIMP CMyScript::shutdown()
{
	// cleanup stuff done in initialize 
	// test 
	AppendOut(_T("debug:shutdown"));

	return S_OK;
}

STDMETHODIMP CMyScript::processEvent(
	BSTR name, // name of eventIn function
	INT eventId, // ID, for speed, numbered by definition order (+3) for built-in fields
	EventOut * value,	// the value 
	DOUBLE timeStamp
	)
{
	HRESULT hr=S_OK;

	if (!fieldsAreOk)
		return E_POINTER;
	
	//TRACE("Got event %d \n",eventId);
	if (wcscmp(name,L"send")  == 0) {


		//  the send eventIn function
		CComQIPtr<EventOutMFString> send(value);

		if (!send) // bad, not an MFString 
			return E_POINTER;

		// get the number of strings
		int cnt=0; // 
		hr=send->getSize(&cnt);

		// the array of strings
		BSTR* value=NULL;
		
		value = (BSTR*) calloc(cnt,sizeof(BSTR));

		//query value 
		hr=send->getValue(cnt,value);

		// process the string 
		
		//
		//echo
		//
		VARIANT_BOOL flag;
		hr = echo->getValue(&flag);
		if (flag) {

			//hr = receive->setValue(cnt,value);

			//test low level output 
			USES_CONVERSION ;
			
			for (int i=0; i<cnt;i++) 
			{
				// convert to local string type (TCHAR)

				LPCTSTR t;
				t=W2T(value[i]);
				
				// put in out queue
				AppendOut(t);

			}
		}	


		// free the strings , alloctated by getValue
		FreeBSTRs(&value,&cnt);


	
	}
	else if (wcscmp(name,L"timer")  == 0) {
		#ifdef _DEBUG
			AppendOut(_T("debug:timer"));
		#endif

	}
	else {
		// we don't know what to do with this event
		#ifdef _DEBUG
			AppendOut(_T("debug:unknown eventIn to script"));
		#endif

	}



	return S_OK;
}

STDMETHODIMP CMyScript::eventsProcessed()
{

	// send outgoing strings 
	if (hasOutValue()) {
		receive->setValue(outValueCnt,outValue);
		// free the strings 
		FreeBSTRArray(outValue,outValueCnt);
		outValueCnt=0;

	}
	// could do the work here, depending on which event has been changed
	return S_OK;
}

