#include "RectangleNode.h"

#include "Rectangle.h"

#include "MeshPrimitives.h"

#include "SceneInfo.h"
#include "SceneModel.h"

RectangleNode::RectangleNode(const Transform& transform, double width, double height)
    : GeometryNode(transform),
      mWidth(width),
      mHeight(height)
{

}

std::unique_ptr<RectangleNode> RectangleNode::Create(const Transform& transform, double width, double height)
{
    return std::make_unique<RectangleNode>(transform, width, height);
}

GeometryNode::Type RectangleNode::getType()
{
    return GeometryNode::Type::Rectangle;
}

double RectangleNode::getWidth() const
{
    return mWidth;
}

double RectangleNode::getHeight() const
{
    return mHeight;
}

double RectangleNode::getRadius() const
{
    return mRadius;
}

void RectangleNode::setWidth(double width)
{
    if (mWidth != width)
    {
        mGeometryIsDirty = true;
        mWidth = width;
    }
}

void RectangleNode::setHeight(double height)
{
    if (mHeight != height)
    {
        mGeometryIsDirty = true;
        mHeight = height;
    }
}

void RectangleNode::setRadius(double radius)
{
    if (mRadius != radius)
    {
        mGeometryIsDirty = true;
        mRadius = radius;
    }
}

void RectangleNode::createOrUpdateGeometry(SceneInfo* sceneInfo)
{
    if (!mBackgroundItem)
    {
        if (sceneInfo->mSupportsGeometryPrimitives)
        {
            auto rect = std::make_unique<ntk::Rectangle>(Point{ 0, 0 }, mWidth, mHeight);
            rect->setRadius(mRadius);
            mBackgroundItem = std::make_unique<SceneModel>(std::move(rect));
        }
        else
        {
            auto mesh = MeshPrimitives::buildRectangleAsTriMesh();
            mBackgroundItem = std::make_unique<SceneModel>(std::move(mesh));

        }
        mBackgroundItem->setName(mName + "_Model");
    }
    else
    {
        if (sceneInfo->mSupportsGeometryPrimitives)
        {
            auto rect = std::make_unique<ntk::Rectangle>(Point{ 0, 0 }, mWidth, mHeight);
            rect->setRadius(mRadius);
            mBackgroundItem->updateGeometry(std::move(rect));
        }
    }
}