from helix.models.dxf.graph_direction import GraphDirection


class GraphNode(object):
    def __init__(self, panel, x_spacing, y_spacing):
        self.neighbors = {}
        self.neighbors = {
            GraphDirection.North: None,
            GraphDirection.NorthEast: None,
            GraphDirection.East: None,
            GraphDirection.SouthEast: None,
            GraphDirection.South: None,
            GraphDirection.SouthWest: None,
            GraphDirection.West: None,
            GraphDirection.NorthWest: None
        }
        self.panel = panel
        self.x_spacing = x_spacing
        self.y_spacing = y_spacing

    @property
    def coordinate(self):
        return self.panel.coordinate

    def has_existing_neighbor(self, direction):
        return self.neighbors.get(direction) is not None

    def add_neighbor(self, other_node, direction):
        if other_node is not None:
            if direction:
                self.neighbors[direction] = other_node
                opposite_direction = direction.opposite_direction()
                other_node.neighbors[opposite_direction] = self

    def neighboring_nodes(self):
        return [neighbor for neighbor in self.neighbors.values() if neighbor is not None]

    def ordinal_neighbors(self):
        neighbors = {}
        for direction in GraphDirection.ordinal_directions():
            if self.neighbors.get(direction):
                neighbors[direction] = self.neighbors[direction]
        return neighbors

    def __hash__(self):
        return self.panel.coordinate.__hash__()

    def __repr__(self):
        neighbors = {}
        for key, value in self.neighbors.items():
            if value is not None:
                neighbors[key] = value.panel
        return str(self.panel) + " - " + str(neighbors)

    def __eq__(self, other):
        if self is other:
            return True
        if not self.panel == other.panel and self.x_spacing != other.x_spacing and self.y_spacing != other.y_spacing:
            return False
        for key, value in self.neighbors.items():
            other_neighbor = other.neighbors[key]
            if value is None and other_neighbor is None:
                continue
            elif value is None or other_neighbor is None:
                return False
            elif value.panel != other_neighbor.panel:
                return False
        return True
