//----------------------------------------------------------------------------------
// <copyright file="SCLookup.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 SCLookup, looks up or checks for a storage controller for a given
//  board address.
// </summary>
//
// <owner>Mike Cookson</owner>
//----------------------------------------------------------------------------------

#ifndef __PAYLOADINTERFACELIB_SCLOOKUP_H__
#define __PAYLOADINTERFACELIB_SCLOOKUP_H__

#include "LqrSMACSharedLib.h"
#include "ProxySCDictionary.h"
#include "StorageController.h"

namespace LiquidRobotics {
namespace EventLib {
	class PayloadManagerClient;
}
namespace SMACFramework {
	class ByteArray;
	class IObjectBroker;
	class Logger;
}
namespace PayloadInterface {

class PayloadInterfaceController_Config;

class SHARED_LIB_PUBLIC SCLookup
{
public:

	typedef LiquidRobotics::EventLib::EventTransceiver EventTransceiver;

	SCLookup(LiquidRobotics::SMACFramework::IObjectBroker& _broker,
			LiquidRobotics::EventLib::PayloadManagerClient& _pmclient,
			LiquidRobotics::SMACFramework::Logger& _logger,
			const StorageController::Config& _scconfig,
			const PayloadInterfaceController_Config& _config,
			EventTransceiver& _et);
	void Initialize();
	StorageController& GetSC(uint16_t boardAddress);
	ProxySC::Ptr GetSCPtr(uint16_t boardAddress);
	ProxySCDictionary::ProxySCListPtr GetSCList() const;
	bool HaveSC(uint16_t boardAddress);

protected:

	typedef LiquidRobotics::EventLib::IEventBase IEventBase;

	void AsynchCmdCmpltHandler(EventTransceiver& sender, const IEventBase& receivedEvent);
	virtual void DataSetHandler(EventTransceiver& sender, const IEventBase& receivedEvent);
	virtual void SCCommandComplete(ProxySC& sender, const LiquidRobotics::SMACFramework::ByteArray& data, long transNumber, unsigned destID, bool complete, unsigned resultNum, bool success) = 0;
	virtual void SCNewFileAvailable(ProxySC& sender, const std::string& actualFileName, std::string& remoteFileName) = 0;

private:

	typedef LiquidRobotics::SMACFramework::ScopedConnectionPtr ScopedConnectionPtr;
    typedef std::list<ScopedConnectionPtr> ScopedConnectionPtrList;

	LiquidRobotics::SMACFramework::IObjectBroker& broker;
	LiquidRobotics::EventLib::PayloadManagerClient& pmclient;
	LiquidRobotics::SMACFramework::Logger& logger;
	const StorageController::Config& scconfig;
	const PayloadInterfaceController_Config& config;
	EventTransceiver& et;
	ProxySCDictionary pscDict;
	ScopedConnectionPtr connectionToCmdCmpltEvent;
	ScopedConnectionPtrList connectionToNewFileAvailableEvent;
};

}
}

#endif
