package h04.movement;

import fopbot.World;
import h04.chesspieces.ChessPiece;
import h04.template.ChessUtils;

import java.awt.Point;
import java.util.ArrayList;

public interface OrthogonalMover extends ChessPiece {

    default Point[] getOrthogonalMoves() {
        var list = new ArrayList<Point>();

        for (int i = getX()-1; i >= 0; i--) {
            var target = new Point(i, getY());
            var r = ChessUtils.getPieceAt(target);
            if (r == null || r.getTeam() != getTeam()) {
                list.add(target);
            }
            if (r != null) break;
        }

        for (int i = getX()+1; i < World.getWidth(); i++) {
            var target = new Point(i, getY());
            var r = ChessUtils.getPieceAt(target);
            if (r == null || r.getTeam() != getTeam()) {
                list.add(target);
            }
            if (r != null) break;
        }

        for (int i = getY()-1; i >= 0; i--) {
            var target = new Point(getX(), i);
            var r = ChessUtils.getPieceAt(target);
            if (r == null || r.getTeam() != getTeam()) {
                list.add(target);
            }
            if (r != null) break;
        }

        for (int i = getY()+1; i < World.getHeight(); i++) {
            var target = new Point(getX(), i);
            var r = ChessUtils.getPieceAt(target);
            if (r == null || r.getTeam() != getTeam()) {
                list.add(target);
            }
            if (r != null) break;
        }

        return list.toArray(Point[]::new);
    }
}
