#include "QuantumWireNode.h"

#include "QuantumWire.h"
#include "LineNode.h"

QuantumWireNode::QuantumWireNode(const Transform& t)
    : AbstractVisualNode(t)
{

}

QuantumWireNode::~QuantumWireNode()
{

}

void QuantumWireNode::setContent(QuantumWire* content)
{
	mContent = content;
	mContentDirty = true;
}

void QuantumWireNode::setInputLocation(const Point& point)
{
    if (mInputLocation != point)
    {
        mContentDirty = true;
        mInputLocation = point;
    }
}

void QuantumWireNode::setOutputLocation(const Point& point)
{
    if (mOutputLocation != point)
    {
        mContentDirty = true;
        mOutputLocation = point;
    }
}

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

void QuantumWireNode::createOrUpdateGeometry(SceneInfo*)
{
    if (!mLine)
    {
        auto loc = mOutputLocation;
        loc.move(-mInputLocation.getX(), -mInputLocation.getY(), -mInputLocation.getZ());

        std::vector<Point> points;

        if (loc.getY() == 0.0)
        {
            points = { loc };
        }
        else
        {
            auto join0 = Point(loc.getX() * 3.0 / 4.0, 0.0);
            auto join1 = Point(loc.getX() * 3.0 / 4.0, loc.getY());
            points = { join0,  join1 , loc };
        }

        mLine = std::make_unique<LineNode>(Transform(mInputLocation), points);

        addChild(mLine.get());
    }
}
