%module{Slic3r::XS};

%{
#include <xsinit.h>

#include "libslic3r.h"
#include "Polyline.hpp"
#include "ShortestPath.hpp"

%}

%name{Slic3r::Polyline::Collection} class PolylineCollection {
    ~PolylineCollection();
    Clone<PolylineCollection> clone()
        %code{% RETVAL = THIS; %};
    void clear()
        %code{% THIS->polylines.clear(); %};
    PolylineCollection* chained_path(bool no_reverse)
        %code{%
            RETVAL = new PolylineCollection();
            RETVAL->polylines = chain_polylines(THIS->polylines, &THIS->polylines.front().first_point());
        %};
    PolylineCollection* chained_path_from(Point* start_near, bool no_reverse)
        %code{%
            RETVAL = new PolylineCollection();
            RETVAL->polylines = chain_polylines(THIS->polylines, start_near);
        %};
    int count()
        %code{% RETVAL = THIS->polylines.size(); %};
%{

PolylineCollection*
PolylineCollection::new(...)
    CODE:
        RETVAL = new PolylineCollection ();
        // ST(0) is class name, others are Polylines
        RETVAL->polylines.resize(items-1);
        for (unsigned int i = 1; i < items; i++) {
            // Note: a COPY of the input is stored
            from_SV_check(ST(i), &RETVAL->polylines[i-1]);
        }
    OUTPUT:
        RETVAL

SV*
PolylineCollection::arrayref()
    CODE:
        AV* av = newAV();
        av_fill(av, THIS->polylines.size()-1);
        int i = 0;
        for (Polylines::iterator it = THIS->polylines.begin(); it != THIS->polylines.end(); ++it) {
            av_store(av, i++, perl_to_SV_ref(*it));
        }
        RETVAL = newRV_noinc((SV*)av);
    OUTPUT:
        RETVAL

SV*
PolylineCollection::pp()
    CODE:
        AV* av = newAV();
        av_fill(av, THIS->polylines.size()-1);
        int i = 0;
        for (Polylines::iterator it = THIS->polylines.begin(); it != THIS->polylines.end(); ++it) {
            av_store(av, i++, to_SV_pureperl(&*it));
        }
        RETVAL = newRV_noinc((SV*)av);
    OUTPUT:
        RETVAL

void
PolylineCollection::append(...)
    CODE:
        for (unsigned int i = 1; i < items; i++) {
            Polyline polyline;
            from_SV_check(ST(i), &polyline);
            THIS->polylines.push_back(polyline);
        }

%}
};