#include "Edge.h"

#include "Node.h"

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

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

Edge::~Edge()
{

}

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

unsigned 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;
    }
}

unsigned Edge::getId() const
{
    return mId;
}

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

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

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

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

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

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

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

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