//------------------------------------------------------------------------------
// <copyright file="VirtualRelayMessageManager.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 VirtualRelayMessageManager class
// </summary>
//
// <owner>Arash Ushani</owner>
//------------------------------------------------------------------------------

#ifndef __VirtualRelayMessageManager_H__
#define __VirtualRelayMessageManager_H__

#include "VirtualRelayCryptoProcessor.h"
#include "VirtualRelayMessage.h"
#include "VirtualRelayMessageManagerSettings.h"
#include "LqrSMACLogger.h"
#include <boost/shared_ptr.hpp>
#include <boost/signals2.hpp>
#include <stdint.h>
#include <queue>

namespace LiquidRobotics {

namespace CryptoLib {
	class CryptoProcessor;
}

namespace SMACFramework {
	class Logger;
}

namespace VirtualRelay {

class VirtualRelayMessageManager
{
public:

	typedef LiquidRobotics::SMACFramework::Logger Logger;
	typedef std::vector<uint8_t> UInt8Vector;
	typedef boost::shared_ptr<UInt8Vector> UInt8VectorPtr;
	typedef boost::signals2::mutex Mutex;
	typedef boost::signals2::scoped_connection ScopedConnection;
	typedef boost::shared_ptr<ScopedConnection> ScopedConnectionPtr;
	typedef boost::signals2::signal<void (const VirtualRelayMessageManager& sender, const UInt8Vector& incomingMessage)> IncomingMessageSignalType;
	typedef boost::shared_ptr<IncomingMessageSignalType> IncomingMessageSignalTypePtr;

	typedef VirtualRelayMessage::VR_SHIPTOSHOREHADER VR_SHIPTOSHOREHADER;
	typedef VirtualRelayMessage::PVR_SHIPTOSHOREHADER PVR_SHIPTOSHOREHADER;
	typedef VirtualRelayMessage::VR_SHORETOSHIPHEADER VR_SHORETOSHIPHEADER;
	typedef VirtualRelayMessage::PVR_SHORETOSHIPHEADER PVR_SHORETOSHIPHEADER;
	typedef VirtualRelayMessage::VR_RELAYSTATUS VR_RELAYSTATUS;
	typedef VirtualRelayMessage::PVR_RELAYSTATUS PVR_RELAYSTATUS;
	typedef VirtualRelayMessage::VR_CHANNELSTATUS VR_CHANNELSTATUS;
	typedef VirtualRelayMessage::PVR_CHANNELSTATUS PVR_CHANNELSTATUS;

	VirtualRelayMessageManager(VirtualRelayMessageManagerSettings& _settings, Logger& _logger);
	~VirtualRelayMessageManager();

	void LoadMessages();
	void DumpMessages();

	void GetOutgoingPacket(uint16_t socketBufferSize, UInt8Vector& outgoingPacket);
	void ProcessIncomingPacket(UInt8Vector& incomingMsg);

	VirtualRelayPacketComparision::Priority HighestPriority();
	UInt8Vector PopMessage();

	ScopedConnectionPtr SubscribeToIncomingMessages(IncomingMessageSignalType::slot_type slot);

	bool HandleOutgoingMessage(UInt8Vector& level2Message);

	bool RequestKeepConnectionActive();

	void SetUniqueID(uint32_t uniqueID);

	bool TelemetryTimeout();

	void ResetTelemetryCount();

	void SetSessionID(uint32_t _sessionID);
	void SetSignalStrength(uint8_t strength);

private:

	std::string Describe();

	void QueueOutgoingLevel2Message(char* msg, int len);
	void QueueOutgoingLevel2Message(UInt8Vector& msg);

	void ProcessMessage(UInt8Vector& message);

	Logger& logger;
	VirtualRelayMessageManagerSettings& settings;
	VirtualRelayCryptoProcessor cryptoProcessor;

	uint8_t signalStrength;
	uint32_t vehicleUniqueID;
	uint32_t sessionID;

	IncomingMessageSignalTypePtr incomingMessageSignalPtr;

	std::priority_queue< UInt8Vector, std::deque<UInt8Vector>, VirtualRelayPacketComparision > outgoingQueue;

	uint16_t lastRelayStatusCRC;

	int telemetryCount;

	bool pendingChannelStatus;

	bool lastSessionEmpty;
};

}
}

#endif
