#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-7;
}

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.a, expected.a) &&
           near(actual.b, expected.b) &&
           near(actual.c, expected.c);
}

int near_identity_pose(const EmcPose &actual, const double *joints)
{
    return near(actual.tran.x, joints[0]) &&
           near(actual.tran.y, joints[1]) &&
           near(actual.tran.z, joints[2]) &&
           near(actual.a, joints[3]) &&
           near(actual.b, joints[4]) &&
           near(actual.c, joints[5]);
}

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 << "_abc="
              << pose.a << ","
              << pose.b << ","
              << pose.c << "\n";
}

void print_joints(const char *prefix, const double *joints)
{
    std::cout << prefix << "_xyzabc="
              << 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 << "puma_init=" << init_rc << "\n";
    std::cout << "puma_type=" << kinematicsType() << "\n";
    std::cout << "puma_switchable=" << kinematicsSwitchable() << "\n";

    double joints[EMCMOT_MAX_JOINTS]{};
    joints[0] = 20.0;
    joints[1] = -30.0;
    joints[2] = 40.0;
    joints[3] = 15.0;
    joints[4] = 35.0;
    joints[5] = -25.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 << "puma_forward=" << forward_rc << "\n";
    std::cout << "puma_forward_iflags=" << iflags << "\n";
    print_pose("puma_forward", forward_pose);

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

    KINEMATICS_INVERSE_FLAGS roundtrip_iflags = 0;
    EmcPose roundtrip_pose{};
    const int roundtrip_rc = kinematicsForward(
        inverse_joints, &roundtrip_pose, &fflags, &roundtrip_iflags);
    std::cout << "puma_roundtrip_forward=" << roundtrip_rc << "\n";
    print_pose("puma_roundtrip", roundtrip_pose);
    std::cout << "puma_roundtrip_pose=" << near_pose(roundtrip_pose, forward_pose) << "\n";

    const int switch_rc = kinematicsSwitch(1);
    std::cout << "puma_switch_identity=" << switch_rc << "\n";
    EmcPose identity_pose{};
    const int identity_forward_rc = kinematicsForward(joints, &identity_pose, &fflags, &iflags);
    std::cout << "puma_identity_forward=" << identity_forward_rc << "\n";
    print_pose("puma_identity", identity_pose);
    std::cout << "puma_identity_near=" << near_identity_pose(identity_pose, joints) << "\n";

    rtapi_app_exit();
    return 0;
}
