package javalabra.chess.core.impl.state;

import java.util.Collection;

import javalabra.chess.core.GameContext;
import javalabra.chess.core.MoveDirector;
import javalabra.chess.core.impl.GameEventEmitter;
import javalabra.chess.core.impl.GameState;
import javalabra.chess.domain.Color;
import javalabra.chess.domain.Move;
import javalabra.chess.domain.Piece;
import javalabra.chess.domain.Square;

public class SelectPieceState extends AbstractGameState {

	private final Color color;

	public SelectPieceState(final Color color) {
		this.color = color;
	}

	@Override
	protected GameState handle(final Square square, final MoveDirector director, final GameContext context, final GameEventEmitter em) {
		final Piece piece = square.getPiece();

		if (null == piece || color != piece.getColor()) {
			return this;
		}

		final Collection<Move> moves = piece.getLegalMoves(director, context);

		if (moves.isEmpty()) {
			return this;
		}

		em.trigger(piece.getColor(), square, moves);

		return new SelectMoveState(piece, moves);
	}

}

@@ -890,54 +890,8 @@
 );%0A%0A
-%09%09if (moves.isEmpty()) %7B%0A%09%09%09return this;%0A%09%09%7D%0A%0A
 %09%09em

allow to select piece with no legal moves