package org.xander.chessboard;

import java.util.*;

import static org.xander.chessboard.Figure.*;

public class Chessboard {
    private final PlacementBehavior knightsPlacement = new KnightsPlacement(this);
    private final PlacementBehavior rooksPlacement = new RooksPlacement(this);
    private int dimension;
    private int boardSize;
    private Map<String, Integer> figureQuantityMap = new HashMap<>();

    public int getDimension() {
        return dimension;
    }

    public void setDimension(int dimension) {
        this.dimension = dimension;
    }

    public Map<String, Integer> getFigureQuantityMap() {
        return figureQuantityMap;
    }

    public void setFigureQuantityMap(Map<String, Integer> figureQuantityMap) {
        this.figureQuantityMap = figureQuantityMap;
    }

    public String drawABoard() {
        int xDimension = dimension;
        int yDimension = dimension;
        calculateBoardSize(xDimension, yDimension);

        StringBuilder chessBoard = new StringBuilder();
        for (int y = 0; y < yDimension; y++) {
            for (int x = 0; x < xDimension; x++) {
                chessBoard.append(".");
            }
            chessBoard.append("\n");
        }
        return chessBoard.toString();
    }

    private void calculateBoardSize(int xDimension, int yDimension) {
        boardSize = xDimension * yDimension;
    }

    public String placeFiguresOnBoard(Map<String, Integer> figureQuantityMap, String emptyBoard) {
        int numberOfKings = extractA(KING.toString(), figureQuantityMap);
        int numberOfQueens = extractA(QUEEN.toString(), figureQuantityMap);
        int numberOfBishops = extractA(BISHOP.toString(), figureQuantityMap);
        int numberOfRooks = extractA(ROOK.toString(), figureQuantityMap);
        int numberOfKnights = extractA(KNIGHT.toString(), figureQuantityMap);
        int sumOfAllFigures = numberOfBishops + numberOfKings + numberOfKnights + numberOfQueens + numberOfRooks;

        if (sumOfAllFigures > boardSize) {
            throw new IllegalStateException("There are more figures than places to put them");
        }
        String boardWithKnights = knightsPlacement.placeOneFigureOnBoardSequentially(emptyBoard);


//        String boardWithKnightsAndRooks = placeRooks(numberOfRooks, boardWithKnights);
//        String boardWithKnightsRooksAndBishops = placeBishops(numberOfBishops, boardWithKnightsAndRooks);
//        String boardWithKnightsRooksBishopsAndQueens = placeQueens(numberOfQueens, boardWithKnightsRooksAndBishops);
//        String boardWithAllFigures = placeKings(numberOfKings, boardWithKnightsRooksBishopsAndQueens);

        return boardWithKnights;
    }

    private int extractA(String figure, Map<String, Integer> figureQuantityMap) {
        if (figureQuantityMap.containsKey(figure)) {
            return figureQuantityMap.get(figure);
        }
        return 0;
    }

    public void checkBoard(String board, int dimension) {
        if (board.isEmpty() || board.length() % dimension != 0) {
            throw new IllegalStateException("There is something wrong with your board");
        }
    }
}

@@ -273,32 +273,114 @@
 lacement(this);%0A
+    private final PlacementBehavior bishopPlacement = new BishopsPlacement(this);%0A
     private int 
@@ -900,17 +900,21 @@
 ing draw
-A
+Empty
 Board() 
@@ -2306,80 +2306,397 @@
 d);%0A
-%0A%0A//        String boardWithKnightsAndRooks = placeRooks(numberOfRooks, 
+//        String boardWithKnightsAndAttackPlaces = knightsPlacement.calculateAttackPlaces(boardWithKnights);%0A%0A//        String boardWithKnightsAndRooks = rooksPlacement.placeOneFigureOnBoardSequentially(boardWithKnightsAndAttackPlaces);%0A        //todo: in this situation rook should not be standing at the first line%0A//        String boardWithKaRaAP = rooksPlacement.calculateAttackPlaces(
 boar
@@ -2707,19 +2707,28 @@
 hKnights
+AndRooks
 );%0A
+%0A
 //      
@@ -2774,38 +2774,106 @@
 s = 
-placeBishops(numberOfBishops, 
+bishopPlacement.placeOneFigureOnBoardSequentially(boardWithKaRaAP);%0A%0A//        System.out.println(
 boar
@@ -2880,31 +2880,38 @@
 dWithKnights
-And
 Rook
+sAndBishop
 s);%0A//      

change method name