/*

ARIA header files for use with ARNL 1.7.1

Copyright(C) 2004, 2005 ActivMedia Robotics, LLC. 
Copyright(C) 2006, 2007, 2008, 2009 MobileRobots Inc.
All rights reserved.

This copy of Aria was relicensed for use with Arnl and the Arnl
license by MobileRobots Inc.  If you wish to download a seperate
distribution of Aria licensed under the GPL or a commercial license go to
http://www.mobilerobots.com/SOFTWARE/aria.html or contact MobileRobots
Inc, at robots@mobilerobots.com or MobileRobots Inc,
10 Columbia Drive, Amherst, NH 03031; 800-639-9481

MobileRobots Inc hereby grants to other individuals or
organizations permission to use this software with Arnl and in
compliance with the Arnl license.  This software may not be
distributed to others except by MobileRobots Inc.

MobileRobots Inc does not make any representations about the
suitability of this software for any purpose.  It is provided "as is"
without express or implied warranty.

*/
#ifndef ARSERVERHANDLERMAPPING_H
#define ARSERVERHANDLERMAPPING_H

#include "Aria.h"
#include "ArServerBase.h"
#include "ArServerHandlerCommands.h"

/// Class that handles the starting, stopping, and status of mapping
class ArServerHandlerMapping
{
public:
  /// Constructor
  AREXPORT ArServerHandlerMapping(ArServerBase *server, ArRobot *robot,
				  ArLaser *laser, 
				  const char *baseDirectory = "",
				  const char *tempDirectory = "",
				  bool useReflectorValues = false,
				  ArLaser *laser2 = NULL, 
				  const char *sickSuffix = NULL,
				  const char *sick2Suffix = NULL);
  /// Deconstructor
  AREXPORT virtual ~ArServerHandlerMapping();
  /// Starts a new map unless ones already made
  AREXPORT void serverMappingStart(ArServerClient *client, 
				   ArNetPacket *packet);
  /// Ends a map
  AREXPORT void serverMappingEnd(ArServerClient *client, 
				 ArNetPacket *packet);
  /// Gets the mapping status
  AREXPORT void serverMappingStatus(ArServerClient *client, 
				    ArNetPacket *packet);
  /// Returns if we're mapping or not
  AREXPORT bool isMapping(void);

  /// Gets the filename we're using (make sure the robot's locked)
  AREXPORT const char *getFileName(void);
  /// Gets the mapname we're using (make sure the robot's locked)
  AREXPORT const char *getMapName(void);
  /// Adds the simple commands to the simple command handler passed in
  AREXPORT void addSimpleCommands(ArServerHandlerCommands *handlerCommands);
  /// Adds a loop start to the log
  AREXPORT void simpleLoopStart(ArArgumentBuilder *arg);
  /// Adds a loop end to the log
  AREXPORT void simpleLoopEnd(ArArgumentBuilder *arg);
  /// Adds a string for adding to the log when we start
  AREXPORT void addStringForStartOfLogs(
	  const char *str, ArListPos::Pos position = ArListPos::LAST);
  /// Removes a string for adding to the log when we start
  AREXPORT void remStringForStartOfLogs(const char *str);
  /// Adds a tag to the logg (has robot pose and stuff) (make sure the
  /// robot's locked)
  AREXPORT void addTagToLog(const char *str);
  /// Adds an info to the log (has no robot pose) (make sure the
  /// robot's locked)
  AREXPORT void addInfoToLog(const char *str);
  /// Adds a new type of location data to the logger
  AREXPORT bool addLocationData(
	  const char *name, 
	  ArRetFunctor2<int, ArTime, ArPose *> *functor);
  /// Adds a callback for the start of mapping
  AREXPORT void addMappingStartCallback(
	  ArFunctor *functor, ArListPos::Pos position = ArListPos::LAST);
  /// Removes a callback for the start of mapping
  AREXPORT void remMappingStartCallback(ArFunctor *functor);
  /// Adds a callback for the end of mapping
  AREXPORT void addMappingEndCallback(
	  ArFunctor *functor, ArListPos::Pos position = ArListPos::LAST);
  /// Removes a callback for the end of mapping
  AREXPORT void remMappingEndCallback(ArFunctor *functor);
  /// Adds a callback to be called before moving from temp dir to base dir
  AREXPORT void addPreMoveCallback(
	  ArFunctor *functor, ArListPos::Pos position = ArListPos::LAST);
  /// Removes a callback to be called before moving from temp dir to base dir
  AREXPORT void remPreMoveCallback(ArFunctor *functor);
  /// Adds a callback to be called after moving from temp dir to base dir
  AREXPORT void addPostMoveCallback(
	  ArFunctor *functor, ArListPos::Pos position = ArListPos::LAST);
  /// Removes a callback to be called after moving from temp dir to base dir
  AREXPORT void remPostMoveCallback(ArFunctor *functor);
  /// The packet handler for starting/stopping scans from the lcd
  AREXPORT bool packetHandler(ArRobotPacket *packet);
  /// Get location data map (mostly for internal things)
  AREXPORT const std::map<std::string, ArRetFunctor2<int, ArTime, ArPose *> *, 
		 ArStrCaseCmpOp> *getLocationDataMap(void);
  /// A method to add the strings for the start of a log just straight to an ArMap
  AREXPORT void addStringsForStartOfLogToMap(ArMap *arMap);
protected:
  ArServerBase *myServer;
  ArRobot *myRobot;
  ArLaser *myLaser;
  ArLaser *myLaser2;
  bool myUseReflectorValues;
  

  std::list<ArFunctor *> myMappingStartCallbacks;
  std::list<ArFunctor *> myMappingEndCallbacks;
  std::list<ArFunctor *> myPreMoveCallbacks;
  std::list<ArFunctor *> myPostMoveCallbacks;
  std::list<std::string> myStringsForStartOfLog;
  ArLaserLogger *myLaserLogger;
  ArLaserLogger *myLaserLogger2;
  std::string myMapName;
  std::string myFileName;
  std::string myFileName2;
  std::string myBaseDirectory;
  std::string myTempDirectory;
  
  std::string mySuffix;
  std::string mySuffix2;

  std::map<std::string, ArRetFunctor2<int, ArTime, ArPose *> *, 
	   ArStrCaseCmpOp> myLocationDataMap;

  ArFunctor2C<ArServerHandlerMapping, ArServerClient *, ArNetPacket *> myMappingStartCB;
  ArFunctor2C<ArServerHandlerMapping, ArServerClient *, ArNetPacket *> myMappingEndCB;
  ArFunctor2C<ArServerHandlerMapping, ArServerClient *, ArNetPacket *> myMappingStatusCB;
  ArServerHandlerCommands *myHandlerCommands;
  ArRetFunctor1C<bool, ArServerHandlerMapping, ArRobotPacket *> myPacketHandlerCB;
  ArFunctor1C<ArServerHandlerMapping, ArArgumentBuilder *> myLoopStartCB;
  ArFunctor1C<ArServerHandlerMapping, ArArgumentBuilder *> myLoopEndCB;
};



#endif // ARSERVERHANDLERMAPPING_H
