package sudoku;

import sudoku.constraint.BlockConstraint;
import sudoku.constraint.ColumnConstraint;
import sudoku.constraint.IConstraint;
import sudoku.constraint.LineConstraint;

import java.util.ArrayList;
import java.util.List;

public class SudokuFactory {

	private static List<Cell> initCells(int size) {
		List<Cell> cells = new ArrayList<>(size * size);
		for (int i = 0; i < size * size; i++) {
			cells.add(new MutableCell());
		}
		return cells;
	}

	private static List<Block> initRectangleBlocs(List<Cell> cells, int width, int height) {
		List<Block> blocs = new ArrayList<>();
		int size = width * height;
		for (int i = 0; i < size; i++) {
			Block newBlock = new Block();
			int blockX = i % height;
			int blockY = i / height;
			for (int y = 0; y < height; y++) {
				for (int x = 0; x < width; x++) {
					int index = ((y + blockY * height) * size + (x + blockX * width));
					Cell blockCell = cells.get(index);
					blockCell.setBlock(newBlock);
					newBlock.addCell(blockCell);
				}
			}
			blocs.add(newBlock);
		}
		return blocs;
	}

	public static MultiDoku createBasicEmptyRectangleSudoku(int widthBlock, int heightBlock) {
		int symbolCount = widthBlock * heightBlock;
		List<Cell> cases = initCells(symbolCount);
		List<Block> blocs = initRectangleBlocs(cases, widthBlock, heightBlock);
		List<IConstraint> constraints = new ArrayList<>();
		constraints.add(new ColumnConstraint());
		constraints.add(new LineConstraint());
		constraints.add(new BlockConstraint());
		Sudoku s = new Sudoku(cases, blocs, constraints);
		List<Sudoku> subSudoku = new ArrayList<>();
		subSudoku.add(s);
		return new MultiDoku(subSudoku);
	}

	public static MultiDoku createBasicEmptySquareSudoku(int size) {
		return createBasicEmptyRectangleSudoku(size, size);
	}

}
