//------------------------------------------------------------------------------
// <copyright file="ConnectionManagerProgram.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
// 	Declaration of ConnectionManagerProgram class
// </summary>
//
// <owner>Arash Ushani</owner>
//------------------------------------------------------------------------------

#ifndef __ConnectionManagerProgram_H__
#define __ConnectionManagerProgram_H__

#include "ConnectionManagerChannel.h"
#include "ConnectionManagerProgramSettings.h"
#include "EventTransceiver.h"
#include "TelemetrySettingsChangedEvent.h"
#include "ChannelRequestEvent.h"
#include "ChannelStateEvent.h"
#include "LqrSMACIdl.h"
#include "LqrSMACSharedLib.h"
#include "LqrSMACTimerCollection.h"
#include "LqrSMACFileSet.h"
#include "PayloadManagerClient.h"
#include <boost/shared_ptr.hpp>
#include <boost/signals2.hpp>
#include <boost/asio.hpp>

#define MAJOR	1
#define MINOR	0
#define BUILD	108

namespace LiquidRobotics {

namespace EventLib {
	class IEventBase;
}

namespace SMACFramework {
	class IObjectBroker;
	class ISerialPort;
	class Logger;
	class PayloadDescription;
}

namespace ConnectionManager {

class ConnectionManagerSettings;

class SHARED_LIB_PUBLIC ConnectionManagerProgram
: public LiquidRobotics::EventLib::EventTransceiver
, public LiquidRobotics::SMACFramework::ICommandableBase
{
public:

	typedef LiquidRobotics::SMACFramework::IObjectBroker IObjectBroker;
	typedef LiquidRobotics::EventLib::IEventBase IEventBase;
	typedef LiquidRobotics::EventLib::TelemetrySettingsChangedEvent TelemetrySettingsChangedEvent;
	typedef LiquidRobotics::EventLib::ChannelRequestEvent ChannelRequestEvent;
	typedef LiquidRobotics::EventLib::ChannelStateEvent ChannelStateEvent;
	typedef LiquidRobotics::SMACFramework::Logger Logger;
	typedef LiquidRobotics::SMACFramework::TimerCollection<void*> TimerCollection;
	typedef LiquidRobotics::EventLib::PayloadManagerClient PayloadManagerClient;
	typedef LiquidRobotics::SMACFramework::PayloadDescription Desc;
	typedef boost::signals2::scoped_connection ScopedConnection;
	typedef boost::shared_ptr<ScopedConnection> ScopedConnectionPtr;
	typedef boost::shared_ptr<ConnectionManagerChannel> ChannelPtr;
	typedef unsigned char byte;

	static const string CMD_REQUEST;
	static const string CMD_CANCELREQUEST;
	static const string CMD_GETSTATE;

	ConnectionManagerProgram(IObjectBroker& _broker, const ConnectionManagerProgramSettings& _settings, Logger::Ptr _logger);
	virtual ~ConnectionManagerProgram();

	virtual void Initialize(Object_ptr _pServer, PayloadManagerClient::Ptr _pmClient);

	// IPingable operations
    virtual ::CORBA::Double Ping();

    // IPayloadDevice
	virtual ::LiquidRobotics::SMAC::EnumerationData* GetEnumerationData();
	virtual void PowerOff(::LiquidRobotics::SMAC::PowerOffTimeType offDelay);
	virtual void PowerOn();
	virtual ::LiquidRobotics::SMAC::BoardIDType boardID();
	virtual ::LiquidRobotics::SMAC::PayloadDescription* description();
	virtual ::LiquidRobotics::SMAC::EPayloadEnableState enableState();
	virtual ::LiquidRobotics::SMAC::PowerOffTimeType requestOffTime();
	virtual ::LiquidRobotics::SMAC::PowerOnTimeType requestOnTime();
	virtual ::LiquidRobotics::SMAC::ComponentVersion* softwareVersion();
	virtual ::LiquidRobotics::SMAC::TaskIDType taskID();

    // ICommandable
    virtual void SendCommand(const ::CORBA::Any& command, const char* requester, const char* transactionID, ::CORBA::Any_OUT_arg result);

private:

	string ProcessCommand(const string& command);

	void ActiveChannelRequest(int protocolRevision);
	void CancelChannelRequest(int protocolRevision);

	void SignalStrengthHandler(ConnectionManagerChannel& sender, int signalStrength);

	void ChannelRequestEventHandler(EventTransceiver& sender, const IEventBase& receivedEvent);
	void ChannelRequestEventHandler_Asynch(const ChannelRequestEvent& receivedEvent);

	void FireChannelStateEvent(int channel, bool state);
	void FireSignalStrengthEvent(int channel, int strength);

	ScopedConnectionPtr connectionToRequestConnectionEvent;
	ScopedConnectionPtr connectionToCancelRequestEvent;

	IObjectBroker& broker;
	const ConnectionManagerProgramSettings& settings;
	Logger::Ptr logger;
	TimerCollection timerCollection;
	PayloadManagerClient::Ptr pmClient;

	std::map<int, ChannelPtr> channels;
	std::map<int, ScopedConnectionPtr> signalStrengthConnections;

	std::map<byte, int> currentRequests;
};

}
}

#endif
