/*
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2010, Willow Garage, Inc.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of Willow Garage, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: table_objects.cpp 30899 2010-07-16 04:56:51Z rusu $
 *
 */

/**
\author Zoltan-Csaba Marton, Dejan Pangercic
 **/

#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <visualization_msgs/Marker.h>
#include <pcl/point_types.h>

#include <table_objects/GetObjects.h>

ros::ServiceClient client;
ros::Publisher pub;
ros::Publisher pub_marker;
ros::Publisher tabletop_pub;

std::string cop_descriptor_name;
std::string aposteriori_position_name;
std::stringstream ss;
int object_id = 700000;


void callback (sensor_msgs::PointCloud2 cloud)
{
  vision_msgs::cop_answer msg;
  //TODO:
  // call service and publish results on topic "/synthetic_percepts/tabletop_percepts"

  if ()
    {
      ///TODO: You'll need a "for" loop and following inside it
      ss << i;
      cop_descriptor_name = "cop_descriptor" + ss.str();
      aposteriori_position_name = "aposteriori_position" + ss.str();
      vision_msgs::cop_descriptor cop_descriptor_name;
      vision_msgs::aposteriori_position aposteriori_position_name;
      msg.found_poses.push_back(aposteriori_position_name);
      msg.found_poses[i].objectId = i;
      msg.found_poses[i].models.push_back(cop_descriptor_name);	  
      msg.found_poses[i].models[0].type = "ODUFinder";
      msg.found_poses[i].models[0].sem_class = /*TODO: assign object type*/;
      msg.found_poses[i].models[0].object_id = ++object_id;
      msg.found_poses[i].position = 0;
    }
  else
    {
      ROS_ERROR("Failed to call service!");
    }
  tabletop_pub.publish(msg);
}

/* ---[ */
int
  main (int argc, char** argv)
{
  ros::init (argc, argv, "test_table_objects");

  ros::NodeHandle nh;

  //TODO: 
  //instantiate a service client
  //  service name: "get_table_objects"
  //  service type: "table_objects::GetObjects"
  
  
  //TODO:
  //subscribe to the points supported by the plane
  //  topic name: "/extract_objects_indices/output"

  //TODO: 
  //advertise a topic with name "/synthetic_percepts/tabletop_percepts" and the message type "vision_msgs::cop_answer".
  ros::spin ();
  return (0);
}
