package com.google.hashcode.utils;

import com.google.hashcode.entity.Cell;
import com.google.hashcode.entity.Ingredient;
import com.google.hashcode.entity.SliceInstruction;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author Grigoriy Lyashenko (Grog).
 */
public class IoUtils {

    /**
     * Parses given input file to a 2d pizza cells array
     *
     * @param file input file
     * @return 2d array representing a pizza
     * @throws IOException parsing fail
     */
    public static Cell[][] parsePizza(String file) throws IOException {
        try (FileReader fileReader = new FileReader(file)) {
            BufferedReader br = new BufferedReader(fileReader);
            //parse the first line
            String[] headerTokens = br.readLine().split(" ");
            int rowsCount = Integer.parseInt(headerTokens[0]);
            int columnsCount = Integer.parseInt(headerTokens[1]);
            //declare a pizza cells array
            Cell[][] ingredients = new Cell[rowsCount][columnsCount];
            int row = 0;
            String fileLine;
            while ((fileLine = br.readLine()) != null) {
                for (int column = 0; column < fileLine.length(); column++) {
                    Character literal = fileLine.charAt(column);
                    if (literal.toString().equals(Ingredient.TOMATO.toString())) {
                        ingredients[row][column] = new Cell(column, row, Ingredient.TOMATO);
                    } else if (literal.toString().equals(Ingredient.MUSHROOM.toString())) {
                        ingredients[row][column] = new Cell(column, row, Ingredient.MUSHROOM);
                    }
                }
                row++;
            }
            return ingredients;
        }
    }

    /**
     * Produces SliceInstructions based on given input data set
     *
     * @param file input data set
     * @return slice instructions
     * @throws IOException file reading error
     */
    public static SliceInstruction parseSliceInstructions(String file) throws IOException {
        try (FileReader fileReader = new FileReader(file)) {
            BufferedReader br = new BufferedReader(fileReader);
            String[] headerTokens = br.readLine().split(" ");
            int minNumberOfIngredientPerSlice = Integer.parseInt(headerTokens[2]);
            int maxNumberOfCellsPerSlice = Integer.parseInt(headerTokens[3]);
            return new SliceInstruction(minNumberOfIngredientPerSlice, maxNumberOfCellsPerSlice);
        }
    }


    /**
     * Converts given pizza cells 2d array to human readable string representation
     *
     * @param ingredients given array
     * @return table like String representation
     */
    public static String convertToHumanReadableTable(Cell[][] ingredients) {
        StringBuilder output = new StringBuilder();
        for (Cell[] row : ingredients) {
            for (Cell cell : row) {
                output.append(cell).append(" ");
            }
            output.append("\n");
        }
        return output.toString();
    }

}

@@ -253,16 +253,96 @@
 ception;
+%0Aimport java.util.Comparator;%0Aimport java.util.Formatter;%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -3179,10 +3179,1008 @@
 %0A    %7D%0A%0A
+%0A    /**%0A     * this method formats data for output to file%0A     * @see issue 1 description%0A     * @see task description%0A     *%0A%09 * @author github.com/VadimKlindukhov skype: kv_vadim%0A%09 * @param list %E2%80%94 inner representation of pizza%0A%09 * @return long String that contains output data%0A%09 */%0A    public static String outputFormat(List%3CList%3CCell%3E%3E list)%7B%0A    %09Comparator%3CCell%3E cellComparator = (Cell c1, Cell c2) -%3E%7B%0A    %09%09if(c1.x != c2.x)%7B%0A    %09%09%09return Integer.compare(c1.x, c2.x);%0A    %09%09%7D else%0A    %09%09%09return Integer.compare(c1.y, c2.y);%0A    %09%7D;%0A    %09StringBuffer sb = new StringBuffer();%0A    %09Formatter textFormatter = new Formatter(sb);%0A    %09textFormatter.format(%22%25d%25n%22, list.size());%0A    %09Cell min, max;%0A    %09for(List%3CCell%3E innerList : list)%7B%0A    %09%09min = innerList.stream().min(cellComparator).get();%0A    %09%09max = innerList.stream().max(cellComparator).get();%0A    %09%09textFormatter.format(%22%25d %25d %25d %25d%25n%22, min.y, min.x, max.y, max.x);%0A    %09%7D%0A    %09textFormatter.close();%0A    %09return sb.toString();%0A    %7D%0A
 %7D%0A

outputFormat method added