package h04.chesspieces;

import fopbot.Robot;
//Wichtig für Implementation
//import h04.movement.MoveStrategy;
import fopbot.World;
import h04.movement.MoveStrategy;
import h04.template.ChessUtils;

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

public class King extends Robot implements ChessPiece {

    private final Team team;

    public King(final int x, final int y, final Team team) {
        super(x, y, team == Team.WHITE ? Families.KING_WHITE : Families.KING_BLACK);
        this.team = team;
    }

    @Override
    public Team getTeam() {
        return team;
    }

    //TODO H4.4
    @Override
    public void moveStrategy(int dx, int dy, MoveStrategy strategy) {
        strategy.move(this, dx, dy);
    }

    @Override
    public Point[] getPossibleMoveFields() {
        var list = new ArrayList<Point>();

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

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

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

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

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

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

            var target = new Point(getX()+1, getY());
            var r = ChessUtils.getPieceAt(target);
            if (r == null || r.getTeam() != getTeam()) {
                list.add(target);
            }

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

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