//-------------------------------------------------------------------------------------------------
// <copyright file="LWInvokeCmd.h" company="LiquidRobotics">
//  Copyright (c) Liquid Robotics Corporation, confidential and proprietary.  All rights reserved. 
// </copyright>
//
// <summary>
//	Declarations for LWInvokeCmd, a command class that is created from a SendCommand
//	invocation.  Commands are first queued up on a queue and run consecutively on the
//	controller's command thread.  LWInvokeCmd passes a method invocation to the
//	appropriate lua instance and waits for completion.
// </summary>
//
// <owner>Mike Cookson</owner>
//-------------------------------------------------------------------------------------------------

#ifndef __LWInvokeCmd_h__
#define __LWInvokeCmd_h__

#include "LqrSMACSemaphore.h"
#include "LWAsynchScriptContextLua.h"
#include "LWConcreteCmd.h"
#include <boost/shared_ptr.hpp>

namespace LiquidRobotics {

namespace SMACFramework {
	class CmdLineParser;
	class Logger;
	class LuaInstance;
}

namespace LuaWrapper {

class SHARED_LIB_PUBLIC LWInvokeCmd : public LWConcreteCmd
{
public:

	typedef LWConcreteCmd::CmdBasePtr CmdPtr;
	typedef LiquidRobotics::SMACFramework::CmdLineParser CmdLineParser;
	typedef LiquidRobotics::SMACFramework::Logger Logger;
	typedef LiquidRobotics::SMACFramework::LuaInstance LuaInstance;
	typedef boost::shared_ptr<LuaInstance> LuaInstancePtr;
	typedef LWControllerBase0::ParsingContext ParsingContext;
	typedef std::vector<string> StringArray;

	LWInvokeCmd(TCon& _ctrl, const string& _cmd, const string& _instanceName, const string& _functionName, const StringArray& _args, Logger& _logger);
	virtual ~LWInvokeCmd();

	// this method is called by the background thread to execute the command
	virtual void Execute();

	// parse the command
	static CmdPtr Parse(const string& arg1, const string& arg2, LWControllerBase0& ctrl, const CmdLineParser& parser, ParsingContext& context, Logger& logger);

private:

	typedef LiquidRobotics::SMACFramework::Semaphore Semaphore;

	void OnAsynchNotify(LWAsynchScriptContextLua& sender, const LWAsynchScriptContextLua::Event& evt);

	LWControllerBase0& ctrl;
	Logger& logger;
	Semaphore sem;
	bool isAsynch;
	bool isComplete;
	string cmd;
	string instanceName;
	string functionName;
	StringArray args;
	LWAsynchScriptContextLua::ScopedConnectionPtr cb;
};

}
}

#endif
