package org.academiadecodigo.enuminatti.paint.painter;

import org.academiadecodigo.enuminatti.paint.grid.Cell;
import org.academiadecodigo.enuminatti.paint.grid.Cursor;
import org.academiadecodigo.enuminatti.paint.grid.Grid;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class Terminal implements Painter {

    private int delay;
    private Grid grid;

    Terminal(int delay, Grid grid) {
        this.delay = delay;
        this.grid = grid;
    }

    @Override
    public void paint() throws InterruptedException {
        Cell[] cells = grid.getCells();
        Cursor cursor = grid.getCursor();

        int cellNum = 0;
        for (int i = 0; i < grid.getCols(); i++) {
            System.out.printf("----");
        }
        System.out.println("-");
        for (int row = 0; row < grid.getRows(); row++) {
            for (int col = 0; col < grid.getCols(); col++) {
                char cellValue = cells[cellNum++].toChar();
                if (cursor.getCol() == col && cursor.getRow() == row) {
                    System.out.printf("|_%c_", cellValue);
                    continue;
                }
                System.out.printf("| %c ", cellValue);
            }
            System.out.println("|");
            for (int i = 0; i < grid.getCols(); i++) {
                System.out.printf("----");
            }
            System.out.println("-");
        }
        Thread.sleep(delay);
    }
}
