package bagaturchess.bitboard.bughouse;


import java.util.ArrayList;

public class PositionsComparator {

    /*
    Used as a comparator, given two different positions in inicialization
    Methods are used for calculating if there has been a taking move between the positions and getting
    the taken piece if there has been one

    Pieces in both arrays are kept in following order and represent count of:
    pawns, bishops, knights, rooks, queens, king

    King is checked just in case the position is valid
    */

    final char[] pieceType = {'p', 'b', 'n', 'r', 'q', 'k', 'P', 'B', 'N', 'R', 'Q', 'K'};
    final int OPPOSITE_PIECE_COEFFICIENT = 6;
    int[] firstBoardPieces;
    int[] secondBoardPieces;
    String currentFEN, afterFEN;
    String strippedCurrentFEN, strippedAfterFEN;


    public PositionsComparator(String currentPositionFEN, String positionAfterMoveFEN) {
        firstBoardPieces = new int[12];
        secondBoardPieces = new int[12];
        for (int i = 0; i < 12; i++) {
            firstBoardPieces[i] = 0;
            secondBoardPieces[i] = 0;
        }
        this.currentFEN = currentPositionFEN;
        this.afterFEN = positionAfterMoveFEN;
        this.strippedCurrentFEN = stripFEN(currentPositionFEN);
        this.strippedAfterFEN = stripFEN(positionAfterMoveFEN);
    }

    public void compareTwoPositions(String currentNewFEN, String afterNewFEN) {
        this.currentFEN = currentNewFEN;
        this.afterFEN = afterNewFEN;
        this.strippedCurrentFEN = stripFEN(currentNewFEN);
        this.strippedAfterFEN = stripFEN(afterNewFEN);
        for (int i = 0; i < 12; i++) {
            firstBoardPieces[i] = 0;
            secondBoardPieces[i] = 0;
        }
    }


    private String stripFEN(String positionAfterMoveFEN) {
        return positionAfterMoveFEN.split(" ")[0];
    }

    public char getPieceIfTakeMove() {
        for (int i = 0; i < strippedCurrentFEN.length(); i++) {
            incPieceType(strippedCurrentFEN.charAt(i), 0);
        }

        for (int i = 0; i < strippedAfterFEN.length(); i++) {
            incPieceType(strippedAfterFEN.charAt(i), 1);
        }

        for (int i = 0; i < 12; i++) {
            if (firstBoardPieces[i] - secondBoardPieces[i] > 0) {
                return pieceType[i];
            }
        }
        return 'x';
    }

    private void incPieceType(char charAt, int boardNum) {
        if (boardNum == 0) {
            switch (charAt) {
                case 'p':
                    firstBoardPieces[0]++;
                    break;
                case 'P':
                    firstBoardPieces[OPPOSITE_PIECE_COEFFICIENT]++;
                    break;
                case 'b':
                    firstBoardPieces[1]++;
                    break;
                case 'B':
                    firstBoardPieces[1 + OPPOSITE_PIECE_COEFFICIENT]++;
                    break;
                case 'n':
                    firstBoardPieces[2]++;
                    break;
                case 'N':
                    firstBoardPieces[2 + OPPOSITE_PIECE_COEFFICIENT]++;
                    break;
                case 'r':
                    firstBoardPieces[3]++;
                    break;
                case 'R':
                    firstBoardPieces[3 + OPPOSITE_PIECE_COEFFICIENT]++;
                    break;
                case 'q':
                    firstBoardPieces[4]++;
                    break;
                case 'Q':
                    firstBoardPieces[4 + OPPOSITE_PIECE_COEFFICIENT]++;
                    break;
                case 'k':
                    firstBoardPieces[5]++;
                    break;
                case 'K':
                    firstBoardPieces[5 + OPPOSITE_PIECE_COEFFICIENT]++;
                    break;
            }
        } else {
            switch (charAt) {
                    case 'p':
                        secondBoardPieces[0]++;
                        break;
                    case 'P':
                        secondBoardPieces[OPPOSITE_PIECE_COEFFICIENT]++;
                        break;
                    case 'b':
                        secondBoardPieces[1]++;
                        break;
                    case 'B':
                        secondBoardPieces[1 + OPPOSITE_PIECE_COEFFICIENT]++;
                        break;
                    case 'n':
                        secondBoardPieces[2]++;
                        break;
                    case 'N':
                        secondBoardPieces[2 + OPPOSITE_PIECE_COEFFICIENT]++;
                        break;
                    case 'r':
                        secondBoardPieces[3]++;
                        break;
                    case 'R':
                        secondBoardPieces[3 + OPPOSITE_PIECE_COEFFICIENT]++;
                        break;
                    case 'q':
                        secondBoardPieces[4]++;
                        break;
                    case 'Q':
                        secondBoardPieces[4 + OPPOSITE_PIECE_COEFFICIENT]++;
                        break;
                    case 'k':
                        secondBoardPieces[5]++;
                        break;
                    case 'K':
                        secondBoardPieces[5 + OPPOSITE_PIECE_COEFFICIENT]++;
                        break;
            }
        }
    }

    public Character getSideToMove(String positionFEN) {
        return positionFEN.split(" ")[1].charAt(0);
    }
}
