//-------------------------------------------------------------------------------------------------
// <copyright file="LWControllerLua.h" company="LiquidRobotics">
//  Copyright (c) Liquid Robotics Corporation, confidential and proprietary.  All rights reserved. 
// </copyright>
//
// <summary>
//	Declarations for LWControllerLua, an implementation of metatable methods exposed
//	to lua as the global "SMC" variable.  It represents the services provided to
//	the script by the lua wrapper.
// </summary>
//
// <owner>Mike Cookson</owner>
//-------------------------------------------------------------------------------------------------

#ifndef __LWControllerLua_h__
#define __LWControllerLua_h__

#include "LqrSMACLuaInstance.h"
#include "LqrSMACSharedLib.h"
#include "LqrSMACTimerCollection.h"
#include "Luna2.h"
#include <boost/shared_ptr.hpp>

namespace LiquidRobotics {

namespace SMACFramework {
	class IObjectBroker;
	class Logger;
}

namespace LuaWrapper {

class LWControllerBase0;

class SHARED_LIB_PUBLIC LWControllerLua : public boost::noncopyable
{
public:

	typedef LiquidRobotics::SMACFramework::IObjectBroker IObjectBroker;
	typedef LiquidRobotics::SMACFramework::LuaInstance LuaInstance;
	typedef LiquidRobotics::SMACFramework::Logger Logger;
	typedef LiquidRobotics::SMACFramework::TimerCollection<void*> TimerCollection;
	typedef std::string string;

	LWControllerLua(LuaInstance& _L);
	virtual ~LWControllerLua();

	void SetController(const string& _instanceName, LWControllerBase0* _pController, Logger* _pLogger, IObjectBroker* _pBroker);

	static string GetClassName();
	static int GetRegistrations(const Luna2<LWControllerLua>::RegType*& regBase);
	static void PreRegistration(LuaInstance& L);

	int BAnd(LuaInstance& L);
	int BOr(LuaInstance& L);
	int BXor(LuaInstance& L);
	int GetLocation(LuaInstance& L);			// returns nil if there is no setting yet
	int Log(LuaInstance& L);
	int NewSerialPort(LuaInstance& L);
	int NewTimer(LuaInstance& L);
	int PublishDataSet(LuaInstance& L);
	int PublishEvent(LuaInstance& L);			// PublishEvent(event-spec)
	int SendCommand(LuaInstance& L);			// SendCommand(serverSpec,var,[synchronous=true]):{asynch,result or txn or nil}
	int SubscribeEvents(LuaInstance& L);		// SubscribeEvents({..eventName..} or {} or nil)
	int SystemUptime(LuaInstance& L);

private:

	static const Luna2<LWControllerLua>::RegType registrations[];
	static const size_t registrationCount;

	typedef LiquidRobotics::SMACFramework::TimerCollectionBasePtr TimerCollectionBasePtr;

	string instanceName;
	TimerCollectionBasePtr tcol;
	LWControllerBase0* pController;
	Logger* pLogger;
	IObjectBroker* pBroker;
};

}
}

#endif
