#include "Edge.h"

#include "Node.h"

Edge::Edge(Node* node0, Node* node1, std::size_t id)
    : mId(id),
      mNode0(node0),
      mNode1(node1)
{
}

std::unique_ptr<Edge> Edge::Create(Node* node0, Node* node1, std::size_t id)
{
    return std::make_unique<Edge>(node0, node1, id);
}

Edge::~Edge()
{

}

std::size_t Edge::getNode0Id() const
{
    return mNode0->getIndex();
}

std::size_t Edge::getNode1Id() const
{
    return mNode1->getIndex();
}

bool Edge::isOverlapping(Edge* edge) const
{
    auto overlaps = edge->getNode0()->isCoincident(mNode0) && edge->getNode1()->isCoincident(mNode1);
    if (overlaps)
    {
        return true;
    }
    else
    {
        return edge->getNode1()->isCoincident(mNode0) && edge->getNode0()->isCoincident(mNode1);
    }
}

bool Edge::hasSameNodes(Edge* edge) const
{
    return edge->getNode0() == edge->getNode1() || edge->getNode1() == edge->getNode0();
}

void Edge::replaceNodes(Node* node0, Node* node1)
{
    mNode0 = node0;
    mNode1 = node1;
}

Node* Edge::getNode0() const
{
    return mNode0;
}

Node* Edge::getNode1() const
{
    return mNode1;
}

Node* Edge::getOtherNode(Node* node) const
{
    if (node == mNode0)
    {
        return mNode1;
    }
    else
    {
        return mNode0;
    }
}

std::size_t Edge::getId() const
{
    return mId;
}

void Edge::setState(State state)
{
    mState = state;
}

Edge::State Edge::getState() const
{
    return mState;
}

void Edge::clearConnectivity()
{
    mAssociatedFaceIds.clear();
}

std::size_t Edge::getNumConnectedFaces() const
{
    return mAssociatedFaceIds.size();
}

void Edge::associateFace(std::size_t faceId)
{
    mAssociatedFaceIds.push_back(faceId);
}

std::size_t Edge::getConnectedFaceId(std::size_t idx) const
{
    return mAssociatedFaceIds[idx];
}

void Edge::associateWithNodes()
{
    mNode0->associateEdge(mId);
    mNode1->associateEdge(mId);
}

void Edge::setIndex(std::size_t idx)
{
    mId = idx;
}
