/**
 * Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium)
 * Copyright (c) 2016, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package eu.itesla_project.online.tools;

import eu.itesla_project.commons.io.ForwardingOutputStream;
import eu.itesla_project.commons.io.table.*;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author c.biasuzzi@techrain.it
 */
public class PrintOnlineWorkflowUtils {

    enum TableFormatterFactorySupplier {

        ascii(AsciiTableFormatterFactory::new),
        csv(CsvTableFormatterFactory::new);

        public final Supplier<TableFormatterFactory> supplier;

        private TableFormatterFactorySupplier(Supplier<TableFormatterFactory> supplier) {
            this.supplier = Objects.requireNonNull(supplier, "null supplier for name " + name());
        }
    }

    public static String availableTableFormatterFormats() {
        return Arrays.stream(TableFormatterFactorySupplier.values()).map(x -> x.name()).collect(Collectors.joining(", "));
    }

    public static boolean isTableFactoryAvailable(String formatName) {
        return Arrays.stream(TableFormatterFactorySupplier.values()).anyMatch((t) -> t.name().equals(formatName));
    }

    public static TableFormatterFactory tableFactoryByFormatName(String formatName) {
        return TableFormatterFactorySupplier.valueOf(formatName).supplier.get();
    }

    public static TableFormatter createFormatter(TableFormatterConfig config, String outputFormatName, Path outputFilePath, String tableTitle, Column... columns) throws IOException {
        Writer writer;
        TableFormatterFactory formatterFactory;
        if (isTableFactoryAvailable(outputFormatName)) {
            formatterFactory = tableFactoryByFormatName(outputFormatName);
        } else {
            throw new RuntimeException("output format not supported: " + outputFormatName);
        }
        if (outputFilePath != null) {
            writer = Files.newBufferedWriter(outputFilePath, StandardCharsets.UTF_8);
        } else {
            writer = new OutputStreamWriter(new ForwardingOutputStream<PrintStream>(System.out) {
                @Override
                public void close() throws IOException {
                }
            });
        }
        return formatterFactory.create(writer, tableTitle, config, columns);
    }
}

@@ -403,68 +403,8 @@
 s;%0A%0A
-import eu.itesla_project.commons.io.ForwardingOutputStream;%0A
 impo
@@ -512,36 +512,8 @@
 er;%0A
-import java.io.PrintStream;%0A
 impo
@@ -2490,48 +2490,8 @@
 ter(
-new ForwardingOutputStream%3CPrintStream%3E(
 Syst
@@ -2575,32 +2575,61 @@
 s IOException %7B%0A
+                    flush();%0A
                 
@@ -2643,17 +2643,16 @@
        %7D
-)
 ;%0A      

Fix IOException when writing to System.out