//-------------------------------------------------------------------------------------------------
// <copyright file="LWOpenLuaInternalCmd.cpp" company="LiquidRobotics">
//  Copyright (c) Liquid Robotics Corporation, confidential and proprietary.  All rights reserved. 
// </copyright>
//
// <summary>
//	Implementation of LWOpenLuaInternalCmd, a delegate class that carries a command
//	create a new lua instance and is queued up on the lua instance specific thread.
//	When it runs, it calls back to the controller to complete instance creation.
// </summary>
//
// <owner>Mike Cookson</owner>
//-------------------------------------------------------------------------------------------------

#include "LWController.h"
#include "LWOpenLuaInternalCmd.h"
#include "LqrSMACLogger.h"
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace LuaWrapper {

LWOpenLuaInternalCmd::LWOpenLuaInternalCmd(TCon& _ctrl, const string& _cmd, const string& _instanceName, const string& _fileName, int _traceOpt)
: LWConcreteCmd(_ctrl)
, ctrl(_ctrl)
, cmd(_cmd)
, instanceName(_instanceName)
, fileName(_fileName)
, traceOpt(_traceOpt)
{
}

LWOpenLuaInternalCmd::~LWOpenLuaInternalCmd()
{
}

// this method is called by the background thread to execute the command
void LWOpenLuaInternalCmd::Execute()
{

	try
	{
		SignalStarted();
		dynamic_cast<LWController&>(ctrl).OpenLuaInternal(instanceName, fileName, traceOpt);
		SetException(TEx::NoException, "");
	}
	catch(exception& ex)
	{
		const int exNum = TEx::ExceptionToNumber(ex);

		stringstream msg;
		msg << "caught exception " << typeid(ex).name() << " '" << ex.what() << "'";

		SetException(exNum, msg.str());
	}
	catch(...)
	{
		TEx::NotReady exc("");
		const int exNum = TEx::ExceptionToNumber(exc);

		SetException(exNum, "caught unknown exception");
	}

	if ((exType == TEx::NoException) && !cmdResponse.length())
		cmdResponse = "OK";

}

// get the output of the command
string LWOpenLuaInternalCmd::GetResult()
{
	DoThrow();
	return cmdResponse;
}


}
}
