#ifndef DATA_CONVERT_H
#define DATA_CONVERT_H

#include <geometry_msgs/TransformStamped.h>
#include <tf3/utils.h>
#include <tf3/compat.h>
#include <robot/time.h>

namespace laser_geometry
{
    robot::Time convertTime(tf3::Time time)
    {
        robot::Time time_tmp;
        time_tmp.sec = time.sec;
        time_tmp.nsec = time.nsec;
        return time_tmp;
    }

    tf3::Time convertTime(robot::Time time)
    {
        tf3::Time time_tmp;
        time_tmp.sec = time.sec;
        time_tmp.nsec = time.nsec;
        return time_tmp;
    }

    
    tf3::Transform convertToTf2Transform(const geometry_msgs::Transform& msg)
    {
        tf3::Quaternion q(
            msg.rotation.x,
            msg.rotation.y,
            msg.rotation.z,
            msg.rotation.w
        );

        tf3::Vector3 t(
            msg.translation.x,
            msg.translation.y,
            msg.translation.z
        );

        tf3::Transform tf(q, t);
        return tf;
    }

    tf3::Transform convertToTf2Transform(const tf3::TransformMsg& msg)
    {
        tf3::Quaternion q(
            msg.rotation.x,
            msg.rotation.y,
            msg.rotation.z,
            msg.rotation.w
        );

        tf3::Vector3 t(
            msg.translation.x,
            msg.translation.y,
            msg.translation.z
        );

        tf3::Transform tf(q, t);
        return tf;
    }

    tf3::TransformStampedMsg convertToTransformStampedMsg(const geometry_msgs::TransformStamped& msg)
    {
        tf3::TransformStampedMsg out;
        out.header.seq = msg.header.seq;
        out.header.stamp = convertTime(msg.header.stamp);
        out.header.frame_id = msg.header.frame_id;
        out.child_frame_id = msg.child_frame_id;
        out.transform.translation.x = msg.transform.translation.x;
        out.transform.translation.y = msg.transform.translation.y;
        out.transform.translation.z = msg.transform.translation.z;
        out.transform.rotation.x = msg.transform.rotation.x; 
        out.transform.rotation.y = msg.transform.rotation.y; 
        out.transform.rotation.z = msg.transform.rotation.z; 
        out.transform.rotation.w = msg.transform.rotation.w; 
        return out;
    }
    geometry_msgs::TransformStamped convertToTransformStamped(const tf3::TransformStampedMsg& msg)
    {
        geometry_msgs::TransformStamped out;
        out.header.seq = msg.header.seq;
        out.header.stamp = convertTime(msg.header.stamp);
        out.header.frame_id = msg.header.frame_id;
        out.child_frame_id = msg.child_frame_id;
        out.transform.translation.x = msg.transform.translation.x;
        out.transform.translation.y = msg.transform.translation.y;
        out.transform.translation.z = msg.transform.translation.z;
        out.transform.rotation.x = msg.transform.rotation.x; 
        out.transform.rotation.y = msg.transform.rotation.y; 
        out.transform.rotation.z = msg.transform.rotation.z; 
        out.transform.rotation.w = msg.transform.rotation.w; 
        return out;
    }
}

#endif // DATA_CONVERT_H