package ca.recrown.islandsurvivalcraft.utilities.pathfinding;

import java.util.Objects;

import ca.recrown.islandsurvivalcraft.utilities.datatypes.Point2;

public class DFSNode extends Point2 implements Comparable<DFSNode> {
    private DFSNode goal;
    public DFSNode[] child;
    private DFSNode parent;

    public DFSNode(DFSNode parent, int x, int y, DFSNode goal) {
        super(x, y);
        child = new DFSNode[4];
        this.parent = parent;
        this.goal = goal;
    }

    public DFSNode(DFSNode parent, Point2 coords, DFSNode goal) {
        this(parent, coords.x, coords.y, goal);
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public DFSNode getGoal() {
        return goal;
    }

    @Override
    public int compareTo(DFSNode o) {
        if (goal == null) return 0;
        return Math.round(distanceToGoal(goal) - o.distanceToGoal(goal));
    }
    
    public DFSNode getParent() {
        return parent;
    }

    public float distanceToGoal(DFSNode goal) {
        float distanceX = goal.x - this.x;
        float distanceY = goal.y - this.y;
        float distance = (float) Math.sqrt(distanceX * distanceX + distanceY * distanceY);
        return distance;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof DFSNode) {
            DFSNode comp = (DFSNode) obj;
            if (comp.x == this.x && comp.y == this.y) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }
}