/*
 * This file is part of the Owl Positioning System (OwlPS) project.
 * It is subject to the copyright notice and license terms in the
 * COPYRIGHT.t2t file found in the top-level directory of this
 * distribution and at
 *   https://code.lm7.fr/mcy/owlps/src/master/COPYRIGHT.t2t
 * No part of the OwlPS Project, including this file, may be copied,
 * modified, propagated, or distributed except according to the terms
 * contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
 * distributed along with this file, either separately or by replacing
 * this notice by the COPYRIGHT.t2t file's contents.
 */


#include "waypoint.hh"
#include "building.hh"

#include <sstream>

using namespace std ;



/* *** Constructors *** */


/**
 * @param _b A pointer to the (first) Building to add to #buildings.
 * If it is null, #buildings will remain empty.
 * @param _x X coordinate.
 * @param _y Y coordinate.
 * @param _z Z coordinate.
 */
Waypoint::Waypoint(Building *const _b,
                   const float _x, const float _y, const float _z):
  Point3D(_x, _y, _z)
{
  if (_b)
    buildings.insert(_b) ;
}


/**
 * @param _b A pointer to the (first) Building to add to #buildings.
 * If it is null, #buildings will remain empty.
 * @param p Coordinates of the Waypoint.
 */
Waypoint::Waypoint(Building *const _b, const Point3D &p): Point3D(p)
{
  if (_b)
    buildings.insert(_b) ;
}


/**
 * Clears #buildings, but does not deallocate the values pointed by
 * the elements into it.
 */
Waypoint::~Waypoint()
{
  buildings.clear() ;
}



/* *** Operators *** */


Waypoint& Waypoint::operator=(const Waypoint &wp)
{
  if (this == &wp)
    return *this ;

  this->Point3D::operator=(wp) ;
  buildings = wp.buildings ;

  return *this ;
}


bool Waypoint::operator==(const Waypoint &wp) const
{
  if (this == &wp)
    return true ;

  return
    this->Point3D::operator==(wp) &&
    buildings == wp.buildings ;
}


Waypoint::operator std::string() const
{
  ostringstream csv ;
  csv << static_cast<Point3D>(*this) ;

  for (auto i = buildings.begin() ; i != buildings.end() ; ++i)
    csv << ';' << (*i)->get_name() ;

  return csv.str() ;
}



ostream &operator<<(ostream &os, const Waypoint &wp)
{
  // Coordinates
  os << static_cast<Point3D>(wp) ;

  // List of buildings
  if (wp.buildings.empty())
    os << '\n' << "Belongs to no building!" ;
  else
    for (auto i = wp.buildings.begin() ; i != wp.buildings.end() ; ++i)
      os << '\n' << **i ;

  return os ;
}
