
#include <Spectre/Core/String.h>
#include <math.h>
#include "Vector2.h"

template <typename T>
Vector2<T>::Vector2()
{
	x = T(0); y = T(0);
}

template <typename T>
Vector2<T>::Vector2(T x, T y)
{
	v[0] = x; v[1] = y;
}

template <typename T>
Vector2<T>::Vector2(T s)
{
	x = s; y = s;
}

template <typename T>
template <typename U>
inline Vector2<T>::Vector2(const Vector2<U>& vec)
{
	x = T(vec.x); y = T(vec.y);
}

template <typename T>
inline float Vector2<T>::length() const
{
	return std::sqrt((x * x) + (y * y));
}

template <typename T>
inline Vector2<T>& Vector2<T>::normalize()
{
	float il = 1.0f / length();
	x *= il; y *= il;
	return *this;
}

template <typename T>
inline T Vector2<T>::dot(const Vector2<T>& vec) const
{
	return x * vec.x + y * vec.y;
}

template <typename T>
inline Vector2<T>& Vector2<T>::reflect(const Vector2<T>& n)
{
	*this = T(2) * n * dot(n) - *this;
	return *this;
}

template <typename T>
std::string Vector2<T>::toString() const
{
	return core::to_string(x) + ", " + core::to_string(y);
}

// ---------
//  Compare
// ---------

template <typename T>
inline bool operator==(const Vector2<T>& a, const Vector2<T>& b)
{
	return a.x == b.x && a.y == b.y;
}

template <typename T>
inline bool operator!=(const Vector2<T>& a, const Vector2<T>& b)
{
	return !(a == b);
}

template<typename T>
inline bool operator<(const Vector2<T>& a, const Vector2<T>& b)
{
	return a.x < b.x && a.y < b.y;
}

template<typename T>
inline bool operator<=(const Vector2<T>& a, const Vector2<T>& b)
{
	return a.x <= b.x && a.y <= b.y;
}

template<typename T>
inline bool operator>(const Vector2<T>& a, const Vector2<T>& b)
{
	return a.x > b.x && a.y > b.y;
}

template<typename T>
inline bool operator>=(const Vector2<T>& a, const Vector2<T>& b)
{
	return a.x >= b.x && a.y >= b.y
}

// ------------------
//  Unary arithmetic
// ------------------

template <typename T>
inline Vector2<T> operator+(const Vector2<T>& v)
{
	return v;
}

template <typename T>
inline Vector2<T> operator-(const Vector2<T>& v)
{
	return Vector2<T>(-v.x, -v.y);
}

// ------------
//  Arithmetic
// ------------

template<typename T>
inline Vector2<T> operator+(const Vector2<T>& a, const Vector2<T>& b)
{
	return Vector2<T>(a.x + b.x, a.y + b.y);
}

template <typename T>
inline Vector2<T> operator-(const Vector2<T>& a, const Vector2<T>& b)
{
	return Vector2<T>(a.x - b.x, a.y - b.y);
}

template <typename T>
inline Vector2<T> operator/(const Vector2<T>& a, const Vector2<T>& b)
{
	return Vector2<T>(a.x / b.x, a.y / b.y);
}

template <typename T>
inline Vector2<T> operator*(const Vector2<T>& a, const Vector2<T>& b)
{
	return Vector2<T>(a.x * b.x, a.y * b.y);
}

template <typename T>
inline Vector2<T>& operator+=(Vector2<T>& a, const Vector2<T>& b)
{
	a.x += b.x; a.y += b.y;
	return a;
}

template <typename T>
inline Vector2<T>& operator-=(Vector2<T>& a, const Vector2<T>& b)
{
	a.x -= b.x; a.y -= b.y;
	return a;
}

template <typename T>
inline Vector2<T>& operator/=(Vector2<T>& a, const Vector2<T>& b)
{
	a.x /= b.x; a.y /= b.y;
	return a;
};

template <typename T>
inline Vector2<T>& operator*=(Vector2<T>& a, const Vector2<T>& b)
{
	a.x *= b.x; a.y *= b.y;
	return a;
}

// ------------
//  Scalar Arithmetic
// ------------

template <typename T>
inline Vector2<T> operator+(const Vector2<T>& v, T s)
{
	return Vector2<T>(v.x + s, v.y + s);
}

template <typename T>
inline Vector2<T> operator+(T s, const Vector2<T>& v)
{
	return v + s;
}

template <typename T>
inline Vector2<T> operator-(const Vector2<T>& v, T s)
{
	return Vector2<T>(v.x - s, v.y - s);
}

template <typename T>
inline Vector2<T> operator-(T s, const Vector2<T>& v)
{
	return Vector2<T>(s - v.x, s - v.y);
}

template <typename T>
inline Vector2<T> operator/(const Vector2<T>& v, T s)
{
	return Vector2<T>(v.x / s, v.y / s);
}

template <typename T>
inline Vector2<T> operator/(T s, const Vector2<T>& v)
{
	return Vector2<T>(s / v.x, s / v.y);
}

template <typename T>
inline Vector2<T> operator*(const Vector2<T>& v, T s)
{
	return Vector2<T>(v.x * s, v.y * s);
}

template <typename T>
inline Vector2<T> operator*(T s, const Vector2<T>& v)
{
	return v * s;
}

template <typename T>
inline Vector2<T>& operator+=(Vector2<T>& v, T s)
{
	v.x += s; v.y += s;
	return v;
}

template <typename T>
inline Vector2<T>& operator-=(Vector2<T>& v,T s)
{
	v.x -= s; v.y -= s;
	return v;
}

template <typename T>
inline Vector2<T>& operator/=(Vector2<T>& v, T s)
{
	v.x /= s; v.y /= s;
	return v;
}

template <typename T>
inline Vector2<T>& operator*=(Vector2<T>& v, T s)
{
	v.x *= s; v.y *= s;
	return v;
}

// ----------------
//  Scalar compare
// ----------------

template <typename T>
inline bool operator<(const Vector2<T>& v, T s)
{
	return v.x < s && v.y < s;
}

template <typename T>
inline bool operator<(T s, const Vector2<T>& v)
{
	return s < v.x && s < v.y;
}

template <typename T>
inline bool operator<=(const Vector2<T>& v, T s)
{
	return v.x <= s && v.y <= s;
}

template <typename T>
inline bool operator<=(T s, const Vector2<T>& v)
{
	return s <= v.x && s <= v.y;
}

template <typename T>
inline bool operator>(const Vector2<T>& v, T s)
{
	return v.x > s && v.y > s;
}

template <typename T>
inline bool operator>(T s, const Vector2<T>& v)
{
	return s < v.x && s < v.y;
}

template <typename T>
inline bool operator>=(const Vector2<T>& v, T s)
{
	return v.x >= s && v.y >= s;
}

template <typename T>
inline bool operator>=(T s, const Vector2<T>& v)
{
	return s >= v.x && s >= v.y;
}

// -------------------
//  Stream operations
// -------------------

template <typename T>
inline std::ostream& operator<<(std::ostream &s, const Vector2<T>& v)
{
	return s << "(" << v.x << "," << v.y << ")";
}
