#include <cmath>
#include <iostream>

#include "emc/kinematics/kinematics.h"
#include "emc/motion/emcmotcfg.h"

int rtapi_app_main(void);
void rtapi_app_exit(void);

namespace {

int near(double actual, double expected)
{
    return std::fabs(actual - expected) < 1e-9;
}

int near_pose(const EmcPose &actual, const EmcPose &expected)
{
    return near(actual.tran.x, expected.tran.x) &&
        near(actual.tran.y, expected.tran.y) &&
        near(actual.tran.z, expected.tran.z) &&
        near(actual.b, expected.b) &&
        near(actual.c, expected.c) &&
        near(actual.w, expected.w);
}

int near_joints(const double *actual, const double *expected)
{
    for (int index = 0; index < 6; ++index) {
        if (!near(actual[index], expected[index])) {
            return 0;
        }
    }
    return 1;
}

void print_pose(const char *prefix, const EmcPose &pose)
{
    std::cout << prefix << "_xyz="
              << pose.tran.x << ","
              << pose.tran.y << ","
              << pose.tran.z << "\n";
    std::cout << prefix << "_bcw="
              << pose.b << ","
              << pose.c << ","
              << pose.w << "\n";
}

void print_joints(const char *prefix, const double *joints)
{
    std::cout << prefix << "_xyzbcw="
              << joints[0] << ","
              << joints[1] << ","
              << joints[2] << ","
              << joints[3] << ","
              << joints[4] << ","
              << joints[5] << "\n";
}

}  // namespace

int main()
{
    const int init_rc = rtapi_app_main();
    std::cout << "fiveaxis_init=" << init_rc << "\n";
    std::cout << "fiveaxis_type=" << kinematicsType() << "\n";
    std::cout << "fiveaxis_switchable=" << kinematicsSwitchable() << "\n";

    double joints[EMCMOT_MAX_JOINTS]{};
    joints[0] = 10.0;
    joints[1] = 20.0;
    joints[2] = 30.0;
    joints[3] = 45.0;
    joints[4] = 30.0;
    joints[5] = 5.0;

    KINEMATICS_FORWARD_FLAGS fflags = 0;
    KINEMATICS_INVERSE_FLAGS iflags = 0;
    EmcPose forward_pose{};
    const int forward_rc = kinematicsForward(joints, &forward_pose, &fflags, &iflags);
    std::cout << "fiveaxis_forward=" << forward_rc << "\n";
    print_pose("fiveaxis_forward", forward_pose);

    double inverse_joints[EMCMOT_MAX_JOINTS]{};
    const int inverse_rc = kinematicsInverse(&forward_pose, inverse_joints, &iflags, &fflags);
    std::cout << "fiveaxis_inverse=" << inverse_rc << "\n";
    print_joints("fiveaxis_inverse", inverse_joints);
    std::cout << "fiveaxis_roundtrip_joints=" << near_joints(inverse_joints, joints) << "\n";

    const int switch_rc = kinematicsSwitch(1);
    std::cout << "fiveaxis_switch_identity=" << switch_rc << "\n";
    EmcPose identity_pose{};
    const int identity_forward_rc = kinematicsForward(joints, &identity_pose, &fflags, &iflags);
    std::cout << "fiveaxis_identity_forward=" << identity_forward_rc << "\n";
    print_pose("fiveaxis_identity", identity_pose);

    EmcPose expected_identity{};
    expected_identity.tran.x = joints[0];
    expected_identity.tran.y = joints[1];
    expected_identity.tran.z = joints[2];
    expected_identity.b = joints[3];
    expected_identity.c = joints[4];
    expected_identity.w = joints[5];
    std::cout << "fiveaxis_identity_near=" << near_pose(identity_pose, expected_identity) << "\n";

    rtapi_app_exit();
    return 0;
}
