/******************************************************************************
*	File		-	types.cpp
*	Author		-	Joey Pollack
*	Date		-	2021/08/30 (y/m/d)
*	Mod Date	-	2021/09/02 (y/m/d)
*	Description	-	Defines some generic types for use through out the engine.
******************************************************************************/


#include "types.h"

namespace lunarium
{

    ////////////////////////////////////////////////////////////
    //				COLOR
    ////////////////////////////////////////////////////////////
    Color::Color(float _r, float _g, float _b, float _a)
        : R(_r), G(_g), B(_b), A(_a)
    {

    }

    Color::Color()
        : R(0.0f), G(0.0f), B(0.0f), A(0.0f)
    {

    }

    Color Color::White()
    {
        return Color(1.0f, 1.0f, 1.0f, 1.0f);
    }

    Color Color::Black()
    {
        return Color(0.0f, 0.0f, 0.0f, 1.0f);
    }

    Color Color::Red()
    {
        return Color(1.0f, 0.0f, 0.0f, 1.0f);
    }

    Color Color::Green()
    {
        return Color(0.0f, 1.0f, 0.0f, 1.0f);
    }

   Color  Color::Blue()
    {
        return Color(0.0f, 0.0f, 1.0f, 1.0f);
    }

    Color Color::Transparent()
    {
        return Color(0.0f, 0.0f, 0.0f, 0.0f);
    }

    ////////////////////////////////////////////////////////////
    //				RECTANGLE
    ////////////////////////////////////////////////////////////
    Rectangle::Rectangle()
        : X(0.0f), Y(0.0f), HalfWidth(1.0f), HalfHeight(1.0f)
    {
        
    }

    Rectangle::Rectangle(b2AABB& box)
    {
        X = box.GetCenter().x;
        Y = box.GetCenter().y;
        HalfWidth = box.GetExtents().x;
        HalfHeight = box.GetExtents().y;
    }

    Rectangle::Rectangle(float x, float y, float half_width, float half_height)
        : X(x), Y(y), HalfWidth(half_width), HalfHeight(half_height)
    {
    }

    Rectangle Rectangle::MakeFromTopLeft(float left, float top, float width, float height)
    {
        float halfw = width / 2;
        float halfh = height / 2;
        return Rectangle(left + halfw, top + halfh, halfw, halfh);
    }

    void Rectangle::Scale(float x, float y)
    {
        HalfWidth *= x;
        HalfHeight *= y;
    }

    float Rectangle::Area() const
    {
        return HalfWidth * 2 * HalfHeight * 2;
    }

    bool Rectangle::ContainsPoint(glm::vec2 point) const
    {
        if (point.x < this->left() || point.x > this->right())
            return false;

        if (point.y < this->top() || point.y > this->bottom())
            return false;

        return true;
    }

    glm::vec2 Rectangle::CenterPoint() const
    {
        return glm::vec2(X, Y);
    }

    bool Rectangle::Intersects(const Rectangle& other, glm::vec2& centers) const
    {
        glm::vec2 myCenter = glm::vec2(X, Y);
        glm::vec2 otherCenter = glm::vec2(other.X, other.Y);
        centers = myCenter - otherCenter;

        if (this->left() > other.right() || other.left() > this->right())
            return false;

        if (this->top() > other.bottom() || other.top() > this->bottom())
            return false;

        return true;
    }
}