/*
  otp/transform.h

  Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com)

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/
#pragma once

#include "pdu.h"

#include <chrono>

namespace OTP::Transform {

using Timestamp = std::chrono::time_point<std::chrono::system_clock>;


namespace Point {

/**
 * @brief The address struct
 */
struct address
  : PDU::pdu_stream_object
{
  uint8_t  system;  //!< system referance number
  uint16_t group;   //!< group referance number
  uint32_t point;   //!< point referance number

  size_t streamSize() const override { return 7; }
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};


namespace Module {

/**
 * @brief Figure 16-1: Module Identifier Structure
 */
struct module_id
{
  uint16_t manufacturer;  //!< ESTA manufacturer ID
  uint16_t number;        //!< manurfactuer assigned model number
};

/// Table 16-1: Standard Module Types
static const uint16_t Position        = 0x0001; //!< the current position of a Point in all three linear directions.
static const uint16_t Position_VA     = 0x0002; //!< the positional velocity and acceleration of a Point.
static const uint16_t Rotation        = 0x0003; //!< the current rotation of the Point using intrinsic Euler rotation
static const uint16_t Rotation_VA     = 0x0004; //!< the rotational velocity and acceleration of the Point
static const uint16_t Scale           = 0x0005; //!< the unitless, absolute scale of the Point
static const uint16_t Reference_Frame = 0x0006; //!< the Address of the Reference Frame of the Point.


/**
 * @brief The OTP::Transform::Point::Module::pdu class
 */
class pdu
    : public OTP::PDU::pdu
{
public:
  void iStream(PDU::Stream) override;
};


struct module : PDU::pdu_stream_object {};


/**
 * @brief The position_module struct
 */
struct position_module
    : module
{
  union {
    uint8_t options;
    struct {
      uint8_t o_reserved : 7;
      bool    scaling    : 1;
    };
  };
  int32_t x;  //!< x-axis position
  int32_t y;  //!< y-axis position
  int32_t z;  //!< z-axis position

  size_t streamSize() const override { return 13; }
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};


/**
 * @brief The position_va_module struct
 */
struct position_va_module
    : module
{
  int32_t Vx; //!< x-axis velocity
  int32_t Vy; //!< y-axis velocity
  int32_t Vz; //!< z-axis velocity
  int32_t Ax; //!< x-axis acceleration
  int32_t Ay; //!< y-axis acceleration
  int32_t Az; //!< z-axis acceleration

  size_t streamSize() const override { return 24; }
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};


/**
 * @brief The rotation_module struct
 */
struct rotation_module
    : module
{
  int32_t Rx; //!< x-axis rotation
  int32_t Ry; //!< y-axis rotation
  int32_t Rz; //!< z-axis rotation

  size_t streamSize() const override { return 12; }
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};


/**
 * @brief The rotation_va_module struct
 */
struct rotation_va_module
    : module
{
  int32_t Vrx;  //!< x-axis rotational velocity
  int32_t Vry;  //!< y-axis rotational velocity
  int32_t Vrz;  //!< z-axis rotational velocity
  int32_t Arx;  //!< x-axis rotational acceleration
  int32_t Ary;  //!< y-axis rotational acceleration
  int32_t Arz;  //!< z-axis rotational acceleration

  size_t streamSize() const override { return 24; }
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};


/**
 * @brief The scale_module struct
 */
struct scale_module
    : module
{
  int32_t scale_x;  //!< x-axis scale
  int32_t scale_y;  //!< y-axis scale
  int32_t scale_z;  //!< z-axis scale

  size_t streamSize() const override { return 12; }
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};


/**
 * @brief The reference_frame_module struct
 */
struct reference_frame_module
    : module
{
  address point;  //!< referance point

  size_t streamSize() const override { return point.streamSize(); }
  void iStream(PDU::Stream s) override;
  void oStream(PDU::Stream s) const override;
};


/**
 * @brief The module_data struct
 */
struct module_data
    : OTP::PDU::pdu_data
{
  ~module_data();

  uint16_t number;          //!< module number
  module * mod = nullptr;   //!< module object

  size_t streamSize() const override;
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;

};

} // namespace Module


/**
 * @brief The OTP::Transform::Point::pdu class
 */
class pdu
    : public OTP::PDU::pdu
{
public:
  void iStream(PDU::Stream) override;
};


/**
 * @brief The point_data struct
 */
struct point_data
    : OTP::PDU::pdu_data
{
  uint8_t  priority = 100;  //!< priority, between 0 and 200
  uint16_t group;           //!< group number
  uint32_t point;           //!< point number
  Timestamp timestamp;      //!< timestamp
  union {
    uint8_t options;
    struct {
      uint8_t f_reserved : 8;
    };
  };
  uint32_t reserved;        //!< reserved
  PDU::Block<Module::pdu> modules;  //!< block of module PDU

  size_t streamSize() const override;
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};


} // namespace Point


/**
 * @brief The OTP::Transform::pdu class
 */
class pdu
    : public OTP::PDU::pdu
{
public:
  void iStream(PDU::Stream) override;
};


/**
 * @brief The transform_data struct
 */
struct transform_data
    : OTP::PDU::pdu_data
{
  uint8_t system;       //!< system number
  Timestamp timestamp;  //!< timestamp
  union {
    uint8_t options;
    struct {
      uint8_t f_reserved : 7;
      bool    full_set   : 1;
    };
  };
  uint32_t reserved;    //!< reserved
  PDU::Block<Point::pdu> points;  //!< block of Point PDU

  size_t streamSize() const override;
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};

ACN::PDU::pdu_stream& operator>>(ACN::PDU::pdu_stream&, OTP::Transform::Timestamp&);
ACN::PDU::pdu_stream& operator<<(ACN::PDU::pdu_stream&, const OTP::Transform::Timestamp&);

} // namespace OTP::Transform
