%module{Slic3r::XS};

%{
#include <myinit.h>
#include "ExtrusionEntityCollection.hpp"
%}

%name{Slic3r::ExtrusionPath::Collection} class ExtrusionEntityCollection {
    %name{_new} ExtrusionEntityCollection();
    void reverse();
    void clear()
        %code{% THIS->entities.clear(); %};
    ExtrusionEntityCollection* chained_path(bool no_reverse)
        %code{% const char* CLASS = "Slic3r::ExtrusionPath::Collection"; RETVAL = THIS->chained_path(no_reverse); %};
    ExtrusionEntityCollection* chained_path_from(Point* start_near, bool no_reverse)
        %code{% const char* CLASS = "Slic3r::ExtrusionPath::Collection"; RETVAL = THIS->chained_path_from(start_near, no_reverse); %};
    Point* first_point()
        %code{% const char* CLASS = "Slic3r::Point"; RETVAL = THIS->first_point(); %};
    Point* last_point()
        %code{% const char* CLASS = "Slic3r::Point"; RETVAL = THIS->last_point(); %};
    int count()
        %code{% RETVAL = THIS->entities.size(); %};
    std::vector<size_t> orig_indices()
        %code{% RETVAL = THIS->orig_indices; %};
%{

void
ExtrusionEntityCollection::DESTROY()
    CODE:
        for (ExtrusionEntitiesPtr::iterator it = THIS->entities.begin(); it != THIS->entities.end(); ++it) {
            delete *it;
        }
        delete THIS;

SV*
ExtrusionEntityCollection::arrayref()
    CODE:
        AV* av = newAV();
        av_fill(av, THIS->entities.size()-1);
        int i = 0;
        for (ExtrusionEntitiesPtr::iterator it = THIS->entities.begin(); it != THIS->entities.end(); ++it) {
            SV* sv = newSV(0);
            // return our item by reference
            if (ExtrusionPath* path = dynamic_cast<ExtrusionPath*>(*it)) {
                sv_setref_pv( sv, "Slic3r::ExtrusionPath::Ref", path );
            } else if (ExtrusionLoop* loop = dynamic_cast<ExtrusionLoop*>(*it)) {
                sv_setref_pv( sv, "Slic3r::ExtrusionLoop::Ref", loop );
            } else {
                sv_setref_pv( sv, "Slic3r::ExtrusionPath::Collection::Ref", *it );
            }
            av_store(av, i++, sv);
        }
        RETVAL = newRV_noinc((SV*)av);
    OUTPUT:
        RETVAL

void
ExtrusionEntityCollection::append(...)
    CODE:
        for (unsigned int i = 1; i < items; i++) {
            ExtrusionEntity* entity = (ExtrusionEntity *)SvIV((SV*)SvRV( ST(i) ));
            // append COPIES
            if (ExtrusionPath* path = dynamic_cast<ExtrusionPath*>(entity)) {
                THIS->entities.push_back( new ExtrusionPath(*path) );
            } else if (ExtrusionLoop* loop = dynamic_cast<ExtrusionLoop*>(entity)) {
                THIS->entities.push_back( new ExtrusionLoop(*loop) );
            } else {
                THIS->entities.push_back( (*(ExtrusionEntityCollection*)entity).clone() );
            }
        }

bool
ExtrusionEntityCollection::no_sort(...)
    CODE:
        if (items > 1) {
            THIS->no_sort = SvTRUE(ST(1));
        }
        RETVAL = THIS->no_sort;
    OUTPUT:
        RETVAL

ExtrusionEntityCollection*
ExtrusionEntityCollection::chained_path_indices(bool no_reverse)
    PREINIT:
        const char* CLASS = "Slic3r::ExtrusionPath::Collection";
    CODE:
        std::vector<size_t> indices;
        RETVAL = THIS->chained_path(no_reverse, &indices);
        RETVAL->orig_indices = indices;
    OUTPUT:
        RETVAL

%}
};
