#include "LineEndNode.h"

#include "GeometryNode.h"
#include "PolygonNode.h"

LineEndNode::LineEndNode(const Transform& t)
    : MaterialNode(t)
{

}

void LineEndNode::setStyle(LineEndNode::Style style)
{
    if (mStyle != style)
    {
        mStyle = style;
        mContentDirty = true;
    }
}

void LineEndNode::setSize(double size)
{
    if (mSize != size)
    {
        mSize = size;
        mContentDirty = true;
    }
}

void LineEndNode::createOrUpdateGeometry(SceneInfo*)
{
    if (!mContentNode)
    {
        if (mStyle == Style::CLOSED_ARROW)
        {
            auto polygon = std::make_unique<PolygonNode>();
            auto p0 = Point(0.0, -mSize / 2.0);
            auto p1 = Point(mSize, 0.0);
            auto p2 = Point(0.0, mSize / 2.0);
            polygon->setPoints({ p0, p1, p2 });
            polygon->setFillColor({ 0, 0, 0 });

            mContentNode = std::move(polygon);
            addChild(mContentNode.get());
        }
    }

}

void LineEndNode::update(SceneInfo* sceneInfo)
{
    if (mContentDirty)
    {
        createOrUpdateGeometry(sceneInfo);
        mContentDirty = false;
    }

    if (mMaterialIsDirty)
    {
        //updateMaterial();
        mMaterialIsDirty = false;
    }
}
