#include "runway.h"
namespace germanairlinesva_simdata
{
  Runway::Runway(std::string designator,
                 double latitudeStart,
                 double longitudeStart,
                 double latitudeEnd,
                 double longitudeEnd,
                 double width)
  {
    this->designator = std::move(designator);
    this->width = width;
    this->length = germanairlinesva_geodata::distanceEarthD(latitudeStart,
                                                            longitudeStart,
                                                            latitudeEnd,
                                                            longitudeEnd);
    this->trueHeading = (std::uint16_t)std::round(
        germanairlinesva_geodata::bearingDD(latitudeStart,
                                            longitudeStart,
                                            latitudeEnd,
                                            longitudeEnd));
    this->bounds = germanairlinesva_geodata::calculateBoxDD(
        {latitudeStart, longitudeStart},
        {latitudeEnd, longitudeEnd},
        this->trueHeading,
        this->width);

    file = std::vector<std::uint8_t>(
        1 + this->designator.length() + 1 + sizeof(this->bounds) +
            sizeof(this->width) + sizeof(this->length) +
            sizeof(this->trueHeading),
        0);
    std::uint8_t *bufPtr = file.data();
    memset(bufPtr,
           static_cast<std::uint8_t>(this->designator.length()),
           sizeof(std::uint8_t));
    bufPtr++;
    memcpy(bufPtr, this->designator.c_str(), this->designator.length());
    bufPtr += this->designator.length() + 1;
    memcpy(bufPtr, &this->bounds, sizeof(this->bounds));
    bufPtr += sizeof(this->bounds);
    memcpy(bufPtr, &this->width, sizeof(this->width));
    bufPtr += sizeof(this->width);
    memcpy(bufPtr, &this->length, sizeof(this->length));
    bufPtr += sizeof(this->length);
    memcpy(bufPtr, &this->trueHeading, sizeof(this->trueHeading));
  }

  Runway::Runway(std::string designator,
                 germanairlinesva_geodata::box bounds,
                 std::uint8_t width,
                 std::uint16_t length,
                 std::uint16_t trueHeading)
  {
    this->designator = std::move(designator);
    this->bounds = bounds;
    this->width = width;
    this->length = length;
    this->trueHeading = trueHeading;

    file = std::vector<std::uint8_t>(
        1 + this->designator.length() + 1 + sizeof(this->bounds) +
            sizeof(this->width) + sizeof(this->length) +
            sizeof(this->trueHeading),
        0);
    std::uint8_t *bufPtr = file.data();
    memset(bufPtr,
           static_cast<std::uint8_t>(this->designator.length()),
           sizeof(std::uint8_t));
    bufPtr++;
    memcpy(bufPtr, this->designator.c_str(), this->designator.length());
    bufPtr += this->designator.length() + 1;
    memcpy(bufPtr, &this->bounds, sizeof(this->bounds));
    bufPtr += sizeof(this->bounds);
    memcpy(bufPtr, &this->width, sizeof(this->width));
    bufPtr += sizeof(this->width);
    memcpy(bufPtr, &this->length, sizeof(this->length));
    bufPtr += sizeof(this->length);
    memcpy(bufPtr, &this->trueHeading, sizeof(this->trueHeading));
  }
} // namespace germanairlinesva_simdata