package h04.movement;

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

import java.awt.*;
import java.util.ArrayList;

public interface DiagonalMover extends ChessPiece {

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

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

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

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

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

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