/* "CodeWorker":	a scripting language for parsing and generating text.

Copyright (C) 1996-1997, 1999-2002 Cédric Lemaire

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

To contact the author: codeworker@free.fr
*/

#ifdef WIN32
#pragma warning (disable : 4786)
#endif

#include "UtlException.h"
#include "UtlTrace.h"
#include "ScpStream.h"
#include "UtlTimer.h"

#include "DtaScriptVariable.h"
#include "GrfExecutionContext.h"
#include "CppCompilerEnvironment.h"
#include "GrfCommand.h"

namespace CodeWorker {
	GrfExecutionContext* GrfCommand::_pExecutionContext = 0L;

	GrfCommand::GrfCommand(GrfBlock* pParent) : _pParent(pParent), _sParsingFilePtr(0L), _iFileLocation(-1), _iCounter(0), _pTimer(0L) {}

	GrfCommand::~GrfCommand() {}

	const char* GrfCommand::getFunctionName() const { return 0L; }
	bool GrfCommand::isAPredefinedFunction() const { return (getFunctionName() != 0L); }
	bool GrfCommand::isABNFCommand() const { return false; }

	void GrfCommand::applyRecursively(APPLY_ON_COMMAND_FUNCTION apply) {
		apply(this);
	}

	void GrfCommand::setParsingInformation(const char* sName, ScpStream& stream) {
		if (sName != 0L) {
			_sParsingFilePtr = sName;
			_iFileLocation = stream.getInputLocation();
		}
	}

	void GrfCommand::callBeforeExecutionCBK(DtaScriptVariable& visibility) {
		callRecursiveBeforeExecutionCBK(getCurrentExecutionContext(), visibility);
	}

	void GrfCommand::callRecursiveBeforeExecutionCBK(GrfExecutionContext* pContext, DtaScriptVariable& visibility) {
		if (pContext != 0L) {
			callRecursiveBeforeExecutionCBK(pContext->getLastExecutionContext(), visibility);
			pContext->handleBeforeExecutionCBK(this, visibility);
		}
	}

	void GrfCommand::callAfterExecutionCBK(DtaScriptVariable& visibility) {
		GrfExecutionContext* pContext = getCurrentExecutionContext();
		while (pContext != 0L) {
			pContext->handleAfterExecutionCBK(this, visibility);
			pContext = pContext->getLastExecutionContext();
		}
	}

	void GrfCommand::callAfterExceptionCBK(DtaScriptVariable& visibility, UtlException& exception) {
		GrfExecutionContext* pContext = getCurrentExecutionContext();
		while (pContext != 0L) {
			pContext->handleAfterExceptionCBK(this, visibility, exception);
			pContext = pContext->getLastExecutionContext();
		}
	}

	SEQUENCE_INTERRUPTION_LIST GrfCommand::execute(DtaScriptVariable& visibility) {
		UTLTRACE_STACK_INSTRUCTION(_sParsingFilePtr, _iFileLocation);
		SEQUENCE_INTERRUPTION_LIST result;
		if (_pExecutionContext != 0L) {
			callBeforeExecutionCBK(visibility);
			try {
				result = executeInternal(visibility);
			} catch(UtlException& exception) {
				callAfterExceptionCBK(visibility, exception);
				throw/* UtlException(exception)*/;
			}
			callAfterExecutionCBK(visibility);
		} else {
			result = executeInternal(visibility);
		}
		return result;
	}

	void GrfCommand::clearTimer() {
		if (_pTimer != 0L) _pTimer->clear();
	}

	void GrfCommand::startTimer() {
		if (_pTimer == 0L) _pTimer = new UtlTimer;
		_pTimer->start();
	}

	void GrfCommand::stopTimer() {
		if (_pTimer != 0L) _pTimer->stop();
	}

	long GrfCommand::getTimeInMillis() const {
		if ((_iCounter == 0) || (_pTimer == 0L)) return 0L; // little bug when the timer has never started
		return _pTimer->getTimeInMillis();
	}

	std::string GrfCommand::toString() const {
		return "<unknown command>";
	}

	void GrfCommand::compileCpp(CppCompilerEnvironment& theCompilerEnvironment) const {
		CW_BODY_INDENT << "<unhandled command '" << getFunctionName() << "'>";
		CW_BODY_ENDL;
	}
}
