/*
 * Author:          Patrick-Christopher Mattulat
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2022-08-05
 * Changed:         2023-06-02
 *
 * */

#include <cmath>
#include <limits>
#include <ls-math/vector/Vector2.hpp>

using ls::math::core::type::vector2_component;
using ls::math::core::type::vector2_components;
using ls::math::core::type::vector_scalar;
using ls::math::vector::Vector2;

Vector2::Vector2(const vector2_component &_x, const vector2_component &_y)
{
  this->components[0] = _x;
  this->components[1] = _y;
}

Vector2 Vector2::operator+(const Vector2 &_vector) const
{
  return Vector2::_add(*this, _vector);
}

Vector2 Vector2::operator-(const Vector2 &_vector) const
{
  return Vector2::_subtract(*this, _vector);
}

vector_scalar Vector2::operator*(const Vector2 &_vector) const
{
  return Vector2::_dot(*this, _vector);
}

Vector2 Vector2::operator*(double _value)
{
  return Vector2{components[0] * _value, components[1] * _value};
}

Vector2 Vector2::operator/(double _divisor)
{
  return Vector2::_operatorDivision(this->components[0], this->components[1], _divisor);
}

bool Vector2::operator==(const Vector2 &_vector)
{
  return this->components[0] == _vector.components[0] && this->components[1] == _vector.components[1];
}

bool Vector2::operator!=(const Vector2 &_vector)
{
  return this->components[0] != _vector.components[0] || this->components[1] != _vector.components[1];
}

Vector2 Vector2::add(const Vector2 &_leftAddend, const Vector2 &_rightAddend)
{
  return Vector2::_add(_leftAddend, _rightAddend);
}

vector_scalar Vector2::dot(const Vector2 &_leftAddend, const Vector2 &_rightAddend)
{
  return Vector2::_dot(_leftAddend, _rightAddend);
}

vector2_components Vector2::getComponents() const
{
  return this->components;
}

double Vector2::getLength()
{
  return Vector2::_getLength(this->components[0], this->components[1]);
}

vector2_component Vector2::getX()
{
  return this->components[0];
}

vector2_component Vector2::getY()
{
  return this->components[1];
}

Vector2 Vector2::normalize()
{
  double length = Vector2::_getLength(this->components[0], this->components[1]) + std::numeric_limits<double>::min();
  return Vector2::_operatorDivision(this->components[0], this->components[1], length);
}

void Vector2::setX(const vector2_component &_value)
{
  this->components[0] = _value;
}

void Vector2::setY(const vector2_component &_value)
{
  this->components[1] = _value;
}

Vector2 Vector2::subtract(const Vector2 &_leftAddend, const Vector2 &_rightAddend)
{
  return Vector2::_subtract(_leftAddend, _rightAddend);
}

Vector2 Vector2::_add(const Vector2 &_leftAddend, const Vector2 &_rightAddend)
{
  return Vector2{_leftAddend.components[0] + _rightAddend.components[0], _leftAddend.components[1] + _rightAddend.components[1]};
}

vector_scalar Vector2::_dot(const Vector2 &_leftAddend, const Vector2 &_rightAddend)
{
  return _leftAddend.components[0] * _rightAddend.components[0] + _leftAddend.components[1] * _rightAddend.components[1];
}

Vector2 Vector2::_subtract(const Vector2 &_leftAddend, const Vector2 &_rightAddend)
{
  return Vector2{_leftAddend.components[0] - _rightAddend.components[0], _leftAddend.components[1] - _rightAddend.components[1]};
}

double Vector2::_getLength(const vector2_component &_x, const vector2_component &_y)
{
  return sqrt(_x * _x + _y * _y);
}

Vector2 Vector2::_operatorDivision(const vector2_component &_x, const vector2_component &_y, double _divisor)
{
  return Vector2{_x / _divisor, _y / _divisor};
}
