//-------------------------------------------------------------------------------------------------
// <copyright file="LWTimerLua.h" company="LiquidRobotics">
//  Copyright (c) Liquid Robotics Corporation, confidential and proprietary.  All rights reserved. 
// </copyright>
//
// <summary>
//	Declarations for LWTimerLua, an implementation of metatable methods exposed to
//	lua as the return value from SMC:NewTimer()  It represents the methods of an
//	interval timer.
// </summary>
//
// <owner>Mike Cookson</owner>
//-------------------------------------------------------------------------------------------------

#ifndef __LWTimerLua_h__
#define __LWTimerLua_h__

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

class LWControllerBase0;

namespace LiquidRobotics {
namespace LuaWrapper {

class SHARED_LIB_PUBLIC LWTimerLua : public boost::noncopyable
{
public:

	typedef LiquidRobotics::SMACFramework::ByteArray ByteArray;
	typedef LiquidRobotics::SMACFramework::LuaInstance LuaInstance;
	typedef LiquidRobotics::SMACFramework::Timer::TimerCollectionBasePtr TimerCollectionBasePtr;
	typedef Luna2<LWTimerLua> LunaT;
	typedef LunaT::RegType RegType;
	typedef std::string string;

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

	void Initialize(LWControllerBase0* _ctrl, TimerCollectionBasePtr _tcol, const string& _luaInstanceName, const string& _timerInstanceName, unsigned _ms);

	static string GetClassName();
	static int GetRegistrations(const RegType*& regBase);
	static void PreRegistration(LuaInstance& L);

	// Lua interface functions for serial port
	int L_GetID(LuaInstance& L);			// GetID <config> <instanceName> (used with callback)

	// C++ interface functions for serial port
	string GetID() const;

protected:

	typedef LiquidRobotics::SMACFramework::Timer Timer;
	typedef boost::shared_ptr<Timer> TimerPtr;
	typedef LiquidRobotics::SMACFramework::TimerWithData<string> TimerWithString;

	void OnTimerExpired(Timer& sender);

private:

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

	LWControllerBase0* pController;
	TimerCollectionBasePtr tcol;
	string luaInstanceName;
	string timerInstanceName;
	TimerPtr timer;
	Timer::ScopedConnectionPtr conn;
};

}
}

#endif
