#define private public
#include "emc/rs274ngc/rs274ngc_interp.hh"
#undef private

#include <cstring>
#include <iostream>

#include "canon_event_sink.hh"
#include "linuxcnc_hal_adapter.hh"
#include "linuxcnc_tool_adapter.hh"

namespace {

void initialize_indexer_interp(Interp &interp)
{
    standalone::reset_hal_adapter();
    standalone::reset_tool_adapter();

    interp._setup.length_units = CANON_UNITS_MM;
    interp._setup.distance_mode = DISTANCE_MODE::ABSOLUTE;
    interp._setup.ijk_distance_mode = DISTANCE_MODE::ABSOLUTE;
    interp._setup.feed_mode = FEED_MODE::UNITS_PER_MINUTE;
    interp._setup.plane = CANON_PLANE::XY;
    interp._setup.motion_mode = G_0;
    interp._setup.percent_flag = false;
    interp._setup.sequence_number = 1;
    interp._setup.parameter_occurrence = 0;
    interp._setup.num_spindles = 1;
    interp._setup.a_indexer_jnum = 0;
    interp._setup.parameters[5599] = 1.0;
    std::memcpy(interp._readers, Interp::default_readers, sizeof(Interp::default_readers));
}

}  // namespace

int main()
{
    Interp interp;
    standalone::reset_canon_events();
    initialize_indexer_interp(interp);

    const int rc = interp.execute("G0 A90\n");
    std::cout << "execute=" << rc << "\n";
    std::cout << "post_a=" << interp._setup.AA_current << "\n";
    std::cout << "canon_event_count=" << standalone::canon_events().size() << "\n";
    for (const auto &event : standalone::canon_events()) {
        std::cout << "canon_event=" << event << "\n";
    }

    return rc == INTERP_OK ? 0 : 1;
}
