/*********************************************************************
*
* Software License Agreement (BSD License)
*
*
* Author: Kevin Lee
*********************************************************************/
#ifndef _ROBOT_CLEAR_COSTMAP_RECOVERY_H_
#define _ROBOT_CLEAR_COSTMAP_RECOVERY_H_
#include <robot/robot.h>
#include <robot_nav_core/recovery_behavior.h>
#include <robot_costmap_2d/costmap_2d_robot.h>

#include <robot_costmap_2d/costmap_layer.h>

namespace robot_clear_costmap_recovery
{
  /**
   * @class ClearCostmapRecovery
   * @brief A recovery behavior that reverts the navigation stack's costmaps to the static map outside of a user-specified region.
   */
  class ClearCostmapRecovery : public robot_nav_core::RecoveryBehavior {
    public:
      /**
       * @brief  Constructor, make sure to call initialize in addition to actually initialize the object
       * @param
       * @return
       */
      ClearCostmapRecovery();

      /**
       * @brief  Initialization function for the ClearCostmapRecovery recovery behavior
       * @param tf A pointer to a transform listener
       * @param global_costmap A pointer to the global_costmap used by the navigation stack
       * @param local_costmap A pointer to the local_costmap used by the navigation stack
       */
      void initialize(std::string name, tf3::BufferCore* tf,
          robot_costmap_2d::Costmap2DROBOT* global_costmap, robot_costmap_2d::Costmap2DROBOT* local_costmap);

      /** 
       * @brief  Run the ClearCostmapRecovery recovery behavior. Reverts the
       * costmap to the static map outside of a user-specified window and
       * clears unknown space around the robot.
       */
      void runBehavior();

      /**
       * @brief  Export factory function for the ClearCostmapRecovery recovery behavior
       * @return A pointer to the ClearCostmapRecovery recovery behavior
       */
      static std::shared_ptr<ClearCostmapRecovery> create();

    private:
      void clear(robot_costmap_2d::Costmap2DROBOT* costmap);
      void clearMap(boost::shared_ptr<robot_costmap_2d::CostmapLayer> costmap, double pose_x, double pose_y);
      robot_costmap_2d::Costmap2DROBOT* global_costmap_, *local_costmap_;
      std::string name_;
      tf3::BufferCore* tf_;
      bool initialized_;
      bool force_updating_; ///< force costmap update after clearing, so we don't need to wait for update thread
      double reset_distance_;
      bool invert_area_to_clear_;
      std::string affected_maps_; ///< clear only local, global or both costmaps
      std::set<std::string> clearable_layers_; ///< Layer names which will be cleared.
  };
} // namespace robot_clear_costmap_recovery

#endif // _ROBOT_CLEAR_COSTMAP_RECOVERY_H_
