#include <Rcpp.h>
// Copyright (c) 2007  GeometryFactory (France).  All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1/Straight_skeleton_2/include/CGAL/Straight_skeleton_2/IO/print.h $
// $Id: include/CGAL/Straight_skeleton_2/IO/print.h b26b07a1242 $
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s)     : Fernando Cacciola

#ifndef CGAL_SLS_IO_PRINT_H
#define CGAL_SLS_IO_PRINT_H

#include <CGAL/license/Straight_skeleton_2.h>


#include <CGAL/Point_2.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <CGAL/Straight_skeleton_2.h>

namespace CGAL {
namespace Straight_skeletons_2 {
namespace IO {

template<class K>
void print_point ( CGAL::Point_2<K> const& p )
{
  Rcpp::Rcout << "(" << p.x() << "," << p.y() << ")" ;
}

template<class HDS_V>
void print_vertex ( HDS_V const& v )
{
  if(v->has_infinite_time())
    Rcpp::Rcout << "F" << v->id() << " " ;
  else
    Rcpp::Rcout << "N" << v->id() << " " ;
}

template<class K, class C>
void print_polygon ( CGAL::Polygon_2<K,C> const& poly )
{
  typedef CGAL::Polygon_2<K,C> Polygon ;

  Rcpp::Rcout << "Polygon with " << poly.size() << " vertices" << std::endl ;

  for( typename Polygon::Vertex_const_iterator vi = poly.vertices_begin() ; vi != poly.vertices_end() ; ++ vi )
  {
    print_point(*vi); Rcpp::Rcout << std::endl ;
  }
}

template<class K, class C>
void print_polygons ( std::vector< std::shared_ptr< CGAL::Polygon_2<K,C> > > const& polies )
{
  typedef std::vector< std::shared_ptr< CGAL::Polygon_2<K,C> > > PolygonVector ;

  Rcpp::Rcout << "Polygon list with " << polies.size() << " polygons" << std::endl ;

  for( typename PolygonVector::const_iterator pi = polies.begin() ; pi != polies.end() ; ++ pi )
    print_polygon(**pi);
}

template<class K, class C>
void print_polygon_with_holes ( CGAL::Polygon_with_holes_2<K,C> const& polywh )
{
  typedef CGAL::Polygon_with_holes_2<K,C> PolygonWithHoles ;

  Rcpp::Rcout << "Polygon_with_holes having " << polywh.number_of_holes() << " holes" << std::endl ;

  print_polygon(polywh.outer_boundary());

  for( typename PolygonWithHoles::Hole_const_iterator hi = polywh.holes_begin() ; hi != polywh.holes_end() ; ++ hi )
    print_polygon(*hi);
}

template<class K, class C>
void print_polygons_with_holes ( std::vector< std::shared_ptr< CGAL::Polygon_with_holes_2<K,C> > > const& polies )
{

  typedef std::vector< std::shared_ptr< CGAL::Polygon_with_holes_2<K,C> > > PolygonWithHolesVector ;

  Rcpp::Rcout << "Polygon_with_holes list with " << polies.size() << " element" << std::endl ;

  for( typename PolygonWithHolesVector::const_iterator pi = polies.begin() ; pi != polies.end() ; ++ pi )
    print_polygon_with_holes(**pi);
}


template<class K>
void print_straight_skeleton( CGAL::Straight_skeleton_2<K> const& ss )
{
  typedef CGAL::Straight_skeleton_2<K> Ss ;

  typedef typename Ss::Vertex_const_handle     Vertex_const_handle ;
  typedef typename Ss::Halfedge_const_handle   Halfedge_const_handle ;
  typedef typename Ss::Halfedge_const_iterator Halfedge_const_iterator ;

  Halfedge_const_handle null_halfedge ;
  Vertex_const_handle   null_vertex ;

  Rcpp::Rcout << "Straight skeleton with " << ss.size_of_vertices()
            << " vertices, " << ss.size_of_halfedges()
            << " halfedges and " << ss.size_of_faces()
            << " faces" << std::endl ;

  Rcpp::Rcout << "Faces " << std::endl;
  for ( Halfedge_const_iterator h = ss.halfedges_begin(); h != ss.halfedges_end(); ++h )
  {
    if(h->is_inner_bisector() || h->is_bisector())
      continue;

    Halfedge_const_iterator end = h;
    for(;;)
    {
      if(h->is_inner_bisector())
        Rcpp::Rcout << "IBH" << h->id() << " " << std::flush ;
      else if(h->is_bisector())
        Rcpp::Rcout << "BH" << h->id() << " " << std::flush ;
      else
        Rcpp::Rcout << "CH" << h->id() << " " << std::flush ;

      print_vertex(h->vertex());

      h = h->next();
      if(h == end)
        break;

      Rcpp::Rcout << " ==> " << std::flush ;
    }

    Rcpp::Rcout << std::endl;
  }

  Rcpp::Rcout << "All halfedges: " << std::endl;

  for ( Halfedge_const_iterator h = ss.halfedges_begin(); h != ss.halfedges_end(); ++h )
  {
    if(h->is_inner_bisector())
      Rcpp::Rcout << "IBH" << h->id() << " " << std::flush ;
    else if(h->is_bisector())
      Rcpp::Rcout << "BH" << h->id() << " " << std::flush ;
    else
      Rcpp::Rcout << "CH" << h->id() << " " << std::flush ;

    print_vertex(h->prev()->vertex()) ;
    print_point(h->prev()->vertex()->point()) ;
    Rcpp::Rcout << " ==> " << std::flush ;
    print_vertex(h->vertex());
    print_point(h->vertex()->point()) ;
    Rcpp::Rcout << std::endl;
  }
}

} // namespace IO
} // namespace Straight_skeletons_2
} // namespace CGAL

#endif // CGAL_SLS_IO_PRINT_H

