#include "LineNode.h"

#include "SceneModel.h"

LineNode::LineNode(const Transform& transform, const std::vector<Point>& points)
    : GeometryNode(transform),
        mPoints(points)
{

}

LineNode::Type LineNode::getType()
{
    return Type::Line;
}

void LineNode::createOrUpdateGeometry(SceneInfo* sceneInfo)
{
    if (sceneInfo->mSupportsGeometryPrimitives)
    {
        auto line = std::make_unique<Line>(Point{ 0, 0 }, mPoints);
        mBackgroundItem = std::make_unique<SceneModel>(std::move(line));

        if (mStartEndStyle != LineEndNode::Style::NONE)
        {
            if (!mStartNode)
            {
                mStartNode = std::make_unique<LineEndNode>(Point{ 0, 0 });
                addChild(mStartNode.get());
            }
            mStartNode->setStyle(mStartEndStyle);
            mStartNode->setSize(mStartEndSize);
        }

        if (mEndEndStyle != LineEndNode::Style::NONE)
        {
            if (!mEndNode)
            {
                auto end_loc = mPoints[mPoints.size() - 1];

                mEndNode = std::make_unique<LineEndNode>(end_loc);
                addChild(mEndNode.get());
            }
            mEndNode->setStyle(mEndEndStyle);
            mEndNode->setSize(mEndEndSize);
        }
    }
    mBackgroundItem->setName(mName + "_Model");
}

void LineNode::setStartEndStyle(LineEndNode::Style style)
{
    if (mStartEndStyle != style)
    {
        mStartEndStyle = style;
        mGeometryIsDirty = true;
    }
}

void LineNode::setEndEndStyle(LineEndNode::Style style)
{
    if (mStartEndStyle != style)
    {
        mEndEndStyle = style;
        mGeometryIsDirty = true;
    }
}