%module{Slic3r::XS};

%{
#include <xsinit.h>
#include "libslic3r/Print.hpp"
#include "libslic3r/PlaceholderParser.hpp"
%}

%package{Slic3r::Print::State};
%{

IV
_constant()
  ALIAS:
    STEP_LAYERS             = posLayers
    STEP_SLICE              = posSlice
    STEP_PERIMETERS         = posPerimeters
    STEP_DETECT_SURFACES    = posDetectSurfaces
    STEP_PREPARE_INFILL     = posPrepareInfill
    STEP_INFILL             = posInfill
    STEP_SUPPORTMATERIAL    = posSupportMaterial
    STEP_SKIRT              = psSkirt
    STEP_BRIM               = psBrim
  PROTOTYPE:
  CODE:
    RETVAL = ix;
  OUTPUT: RETVAL

%}


%name{Slic3r::Print::Region} class PrintRegion {
    // owned by Print, no constructor/destructor

    Ref<StaticPrintConfig> config()
        %code%{ RETVAL = &THIS->config; %};
    Ref<Print> print();
    
    Clone<Flow> flow(FlowRole role, double layer_height, bool bridge, bool first_layer, double width, PrintObject* object)
        %code%{ RETVAL = THIS->flow(role, layer_height, bridge, first_layer, width, *object); %};
};


%name{Slic3r::Print::Object} class PrintObject {
    // owned by Print, no constructor/destructor

    void add_region_volume(int region_id, int volume_id);
    std::vector<int> get_region_volumes(int region_id)
        %code%{
            if (0 <= region_id && region_id < THIS->region_volumes.size())
                RETVAL = THIS->region_volumes[region_id];
        %};
    int region_count()
        %code%{ RETVAL = THIS->print()->regions.size(); %};

    Ref<Print> print();
    Ref<ModelObject> model_object();
    Ref<StaticPrintConfig> config()
        %code%{ RETVAL = &THIS->config; %};
    Points copies();
    t_layer_height_ranges layer_height_ranges()
        %code%{ RETVAL = THIS->layer_height_ranges; %};
    Ref<LayerHeightSpline> layer_height_spline()
        %code%{ RETVAL = &THIS->layer_height_spline; %};
    Ref<Point3> size()
        %code%{ RETVAL = &THIS->size; %};
    Clone<BoundingBox> bounding_box();
    Ref<Point> _copies_shift()
        %code%{ RETVAL = &THIS->_copies_shift; %};
    std::vector<int> support_material_extruders()
        %code%{
            std::set<size_t> extruders = THIS->support_material_extruders();
            RETVAL.reserve(extruders.size());
            for (std::set<size_t>::const_iterator e = extruders.begin(); e != extruders.end(); ++e) {
                RETVAL.push_back(*e);
            }
        %};
    std::vector<int> extruders()
        %code%{
            std::set<size_t> extruders = THIS->extruders();
            RETVAL.reserve(extruders.size());
            for (std::set<size_t>::const_iterator e = extruders.begin(); e != extruders.end(); ++e) {
                RETVAL.push_back(*e);
            }
        %};
    
    bool typed_slices()
        %code%{ RETVAL = THIS->typed_slices; %};
    void set_typed_slices(bool value)
        %code%{ THIS->typed_slices = value; %};
    
    Points _shifted_copies()
        %code%{ RETVAL = THIS->_shifted_copies; %};
    void set_shifted_copies(Points value)
        %code%{ THIS->_shifted_copies = value; %};

    bool add_copy(Pointf* point)
        %code%{ RETVAL = THIS->add_copy(*point); %};
    bool delete_last_copy();
    bool delete_all_copies();
    bool set_copies(Points copies);
    bool reload_model_instances();
    void set_layer_height_ranges(t_layer_height_ranges layer_height_ranges)
        %code%{ THIS->layer_height_ranges = layer_height_ranges; %};

    size_t total_layer_count();
    size_t layer_count();
    void clear_layers();
    Ref<Layer> get_layer(int idx);
    Ref<Layer> add_layer(int id, coordf_t height, coordf_t print_z,
        coordf_t slice_z);
    void delete_layer(int idx);

    size_t support_layer_count();
    void clear_support_layers();
    Ref<SupportLayer> get_support_layer(int idx);
    Ref<SupportLayer> add_support_layer(int id, coordf_t height, coordf_t print_z);
    void delete_support_layer(int idx);
    
    bool invalidate_step(PrintObjectStep step);
    bool invalidate_all_steps();
    bool step_done(PrintObjectStep step)
        %code%{ RETVAL = THIS->state.is_done(step); %};
    void set_step_done(PrintObjectStep step)
        %code%{ THIS->state.set_done(step); %};
    void set_step_started(PrintObjectStep step)
        %code%{ THIS->state.set_started(step); %};
    
    %name{_detect_surfaces_type} void detect_surfaces_type();
    void process_external_surfaces();
    void bridge_over_infill();
    void _slice();
    SV* _slice_region(size_t region_id, std::vector<double> z, bool modifier)
        %code%{
            std::vector<float> z_f(z.begin(), z.end());
            std::vector<ExPolygons> layers = THIS->_slice_region(region_id, z_f, modifier);
            AV* layers_av = newAV();
            size_t len = layers.size();
            if (len > 0) av_extend(layers_av, len-1);
            for (unsigned int i = 0; i < layers.size(); i++) {
                AV* expolygons_av = newAV();
                len = layers[i].size();
                if (len > 0) av_extend(expolygons_av, len-1);
                unsigned int j = 0;
                for (ExPolygons::iterator it = layers[i].begin(); it != layers[i].end(); ++it) {
                    av_store(expolygons_av, j++, perl_to_SV_clone_ref(*it));
                }
                av_store(layers_av, i, newRV_noinc((SV*)expolygons_av));
            }
            RETVAL = (SV*)newRV_noinc((SV*)layers_av);
        %};
    void _make_perimeters();
    void _infill();
    
    int ptr()
        %code%{ RETVAL = (int)(intptr_t)THIS; %};
};


%name{Slic3r::Print} class Print {
    Print();
    ~Print();

    Ref<StaticPrintConfig> config()
        %code%{ RETVAL = &THIS->config; %};
    Ref<StaticPrintConfig> default_object_config()
        %code%{ RETVAL = &THIS->default_object_config; %};
    Ref<StaticPrintConfig> default_region_config()
        %code%{ RETVAL = &THIS->default_region_config; %};
    Ref<PlaceholderParser> placeholder_parser()
        %code%{ RETVAL = &THIS->placeholder_parser; %};
    // TODO: status_cb
    Ref<ExtrusionEntityCollection> skirt()
        %code%{ RETVAL = &THIS->skirt; %};
    Ref<ExtrusionEntityCollection> brim()
        %code%{ RETVAL = &THIS->brim; %};

    PrintObjectPtrs* objects()
        %code%{ RETVAL = &THIS->objects; %};
    void clear_objects();
    Ref<PrintObject> get_object(int idx);
    void delete_object(int idx);
    void reload_object(int idx);
    bool reload_model_instances();
    size_t object_count()
        %code%{ RETVAL = THIS->objects.size(); %};

    PrintRegionPtrs* regions()
        %code%{ RETVAL = &THIS->regions; %};
    Ref<PrintRegion> get_region(int idx);
    Ref<PrintRegion> add_region();
    size_t region_count()
        %code%{ RETVAL = THIS->regions.size(); %};
    
    bool invalidate_step(PrintStep step);
    bool invalidate_all_steps();
    bool step_done(PrintStep step)
        %code%{ RETVAL = THIS->state.is_done(step); %};
    bool object_step_done(PrintObjectStep step)
        %code%{ RETVAL = THIS->step_done(step); %};
    void set_step_done(PrintStep step)
        %code%{ THIS->state.set_done(step); %};
    void set_step_started(PrintStep step)
        %code%{ THIS->state.set_started(step); %};
    
    std::vector<int> object_extruders()
        %code%{
            std::set<size_t> extruders = THIS->object_extruders();
            RETVAL.reserve(extruders.size());
            for (std::set<size_t>::const_iterator e = extruders.begin(); e != extruders.end(); ++e) {
                RETVAL.push_back(*e);
            }
        %};
    std::vector<int> support_material_extruders()
        %code%{
            std::set<size_t> extruders = THIS->support_material_extruders();
            RETVAL.reserve(extruders.size());
            for (std::set<size_t>::const_iterator e = extruders.begin(); e != extruders.end(); ++e) {
                RETVAL.push_back(*e);
            }
        %};
    std::vector<int> extruders()
        %code%{
            std::set<size_t> extruders = THIS->extruders();
            RETVAL.reserve(extruders.size());
            for (std::set<size_t>::const_iterator e = extruders.begin(); e != extruders.end(); ++e) {
                RETVAL.push_back(*e);
            }
        %};
    int brim_extruder();
    void clear_filament_stats()
        %code%{
            THIS->filament_stats.clear();
        %};
    void set_filament_stats(int extruder_id, float length)
        %code%{
            THIS->filament_stats.insert(std::pair<size_t,float>(extruder_id, 0));
            THIS->filament_stats[extruder_id] += length;
        %};
    SV* filament_stats()
        %code%{
            HV* hv = newHV();
            for (std::map<size_t,float>::const_iterator it = THIS->filament_stats.begin(); it != THIS->filament_stats.end(); ++it) {
                // stringify extruder_id
                std::ostringstream ss;
                ss << it->first;
                std::string str = ss.str();
                
                (void)hv_store( hv, str.c_str(), str.length(), newSViv(it->second), 0 );
                RETVAL = newRV_noinc((SV*)hv);
            }
        %};
    void _simplify_slices(double distance);
    double max_allowed_layer_height() const;
    bool has_support_material() const;
    void auto_assign_extruders(ModelObject* model_object);
    std::string output_filename();
    std::string output_filepath(std::string path = "");
    
    void add_model_object(ModelObject* model_object, int idx = -1);
    bool apply_config(DynamicPrintConfig* config)
        %code%{ RETVAL = THIS->apply_config(*config); %};
    bool apply_static_config(StaticPrintConfig* config)
        %code%{
            DynamicPrintConfig dpc;
            dpc.apply(*config);
            RETVAL = THIS->apply_config(dpc);
        %};
    bool has_infinite_skirt();
    bool has_skirt();
    std::string _validate()
        %code%{ RETVAL = THIS->validate(); %};
    Clone<BoundingBox> bounding_box();
    Clone<BoundingBox> total_bounding_box();
    double skirt_first_layer_height();
    Clone<Flow> brim_flow();
    Clone<Flow> skirt_flow();
    void _make_brim();
%{

double
Print::total_used_filament(...)
    CODE:
        if (items > 1) {
            THIS->total_used_filament = (double)SvNV(ST(1));
        }
        RETVAL = THIS->total_used_filament;
    OUTPUT:
        RETVAL

double
Print::total_extruded_volume(...)
    CODE:
        if (items > 1) {
            THIS->total_extruded_volume = (double)SvNV(ST(1));
        }
        RETVAL = THIS->total_extruded_volume;
    OUTPUT:
        RETVAL


double
Print::total_weight(...)
    CODE:
        if (items > 1) {
            THIS->total_weight = (double)SvNV(ST(1));
        }
        RETVAL = THIS->total_weight;
    OUTPUT:
        RETVAL

double
Print::total_cost(...)
    CODE:
        if (items > 1) {
            THIS->total_cost = (double)SvNV(ST(1));
        }
        RETVAL = THIS->total_cost;
    OUTPUT:
        RETVAL

%}
};
