#include "CircleNode.h"

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

#include <iostream>

CircleNode::CircleNode(const DiscretePoint& location, unsigned radius)
:    GeometryNode(location),
     mRadius(radius)
{

}

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

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

SceneItem* CircleNode::getSceneItem(std::size_t idx) const
{
    if (idx == 0)
    {
        return mBackgroundItem.get();
    }
    else
    {
        return nullptr;
    }
}

unsigned CircleNode::getNumSceneItems() const
{
    return 1;
}

void CircleNode::setRadius(unsigned radius)
{
    if (mRadius != radius)
    {
        mRadius = radius;
        mTransformIsDirty = true;
    }

}

void CircleNode::update(FontsManager* fontsManager)
{
    if (!mBackgroundItem || mGeometryIsDirty)
    {
        auto mesh = MeshPrimitives::buildCircleAsTriMesh();

        if (!mBackgroundItem)
        {
            mBackgroundItem = std::make_unique<SceneModel>(std::move(mesh));
            mBackgroundItem->setName(mName + "_Model");
        }
        else
        {
            mBackgroundItem->updateMesh(std::move(mesh));
        }
        mGeometryIsDirty = false;
    }

    if (mTransformIsDirty)
    {
        mBackgroundItem->updateTransform({mLocation, static_cast<double>(2*mRadius), static_cast<double>(2*mRadius)});
        mTransformIsDirty = false;
    }

    if (mMaterialIsDirty)
    {
        mBackgroundItem->updateUniformColor(mFillColor);
        mMaterialIsDirty = false;
    }
}
