package be.naaturel.boardmateapi.common.models;

import java.util.Collection;
import java.util.EnumSet;
import java.util.Set;

public class Move {

    private final Piece piece;
    private final Coordinate coordinate;
    private final Set<MoveAttribute> attributes;
    private final Piece promotionPiece;

    public Move(Piece piece, Coordinate coordinate, Collection<MoveAttribute> attributes){
        this(piece, null, coordinate, attributes);
    }

    public Move(Piece piece, Piece promotionPiece, Coordinate coordinate, Collection<MoveAttribute> attributes){
        this.piece = piece;
        this.coordinate = coordinate;
        this.attributes = EnumSet.copyOf(attributes);
        this.promotionPiece = promotionPiece;
    }

    private boolean hasAttribute(MoveAttribute attribute){
        return attributes.contains(attribute);
    }

    @Override
    public String toString(){

        if(hasAttribute(MoveAttribute.CASTLE_KINGSIDE)) return "O-O";
        if(hasAttribute(MoveAttribute.CASTLE_QUEENSIDE)) return "O-O-O";

        StringBuilder bob = new StringBuilder();
        bob.append(piece);
        if(hasAttribute(MoveAttribute.CAPTURE)) bob.append("x");
        bob.append(coordinate);
        if(hasAttribute(MoveAttribute.PROMOTION) && promotionPiece != null) bob.append("=").append(promotionPiece);
        if(hasAttribute(MoveAttribute.CHECKMATE)) bob.append("#");
        else if(hasAttribute(MoveAttribute.CHECK)) bob.append("+");

        return bob.toString();
    }

}
