#include "CircleNode.h"

#include "SceneModel.h"
#include "AbstractMesh.h"
#include "MeshPrimitives.h"
#include "SceneInfo.h"

#include "Circle.h"

CircleNode::CircleNode(const Transform& transform, double radius)
:    GeometryNode(transform),
     mRadius(radius)
{
    mMinorRadius = mRadius;
}

CircleNode::~CircleNode()
{

}

CircleNode::Type CircleNode::getType()
{
    return Type::Circle;
}

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

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

void CircleNode::setMinorRadius(double radius)
{
    if (mMinorRadius != radius)
    {
        mMinorRadius = radius;
        mGeometryIsDirty = true;
    }
}

std::unique_ptr<AbstractGeometricItem> CircleNode::createGeometryPrimitive() const
{
    auto circle = std::make_unique<Circle>(Point{ 0, 0 }, mRadius);
    circle->setMinorRadius(mMinorRadius);
    return circle;
}

std::unique_ptr<AbstractMesh> CircleNode::createMeshPrimitive() const
{
    return MeshPrimitives::buildCircleAsTriMesh();
}

void CircleNode::createOrUpdateGeometry(SceneInfo* sceneInfo)
{
    if (!mBackgroundItem)
    {
        if (sceneInfo->mSupportsGeometryPrimitives)
        {
            mBackgroundItem = std::make_unique<SceneModel>(createGeometryPrimitive());
        }
        else
        {
            mBackgroundItem = std::make_unique<SceneModel>(createMeshPrimitive());
            mBackgroundItem->setMeshTransform(Transform({}, mRadius, mRadius));
        }
        mBackgroundItem->setName(mName + "_Model");
    }
    else
    {
        if (sceneInfo->mSupportsGeometryPrimitives)
        {
            mBackgroundItem->updateGeometry(createGeometryPrimitive());
        }
        else
        {
            mBackgroundItem->updateMesh(createMeshPrimitive());
            mBackgroundItem->setMeshTransform(Transform({}, mRadius, mRadius));
        }
    }
}
