#include "QuantumTerminalNode.h"

#include "QuantumTerminal.h"
#include "EquationNode.h"
#include "LatexMathExpression.h"

QuantumTerminalNode::QuantumTerminalNode(const Transform& transform)
    : QuantumCircuitElementNode(transform)
{

}

void QuantumTerminalNode::setContent(QuantumTerminal* terminal)
{
    mContent = terminal;
    mContentDirty = true;
}

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

void QuantumTerminalNode::createOrUpdateGeometry(SceneInfo*)
{
    if (!mLabel && mContent->getTerminalType() != QuantumTerminal::TerminalType::OUTPUT)
    {
        const auto value = mContent->getValue();
        std::string label;
        if (value.isIn0State())
        {
            label = "\\ket{0}";
        }
        else if(value.isIn1State())
        {
            label = "\\ket{1}";
        }
        else
        {
            label = "\\Psi";
        }

        mLabelExpression = std::make_unique<LatexMathExpression>(label);
        mLabel = std::make_unique<EquationNode>();
        mLabel->setContent(mLabelExpression.get());

        addChild(mLabel.get());
    }
}

Point QuantumTerminalNode::getConnectionLocation(AbstractQuantumWire*) const
{
    auto left = mTransform.getLocation();
    left.move(mWidth, mHeight/2.0);
    return left;
}
