/*
 * The MIT License
 *
 * Copyright © 2014 Filip Plesinger, Juraj Jurco.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * Authors: 
 * 
 * Filip Plesinger <fplesinger@isibrno.cz>
 * Institute of Scientific Instruments of the ASCR, v. v. i.
 * Brno, Czech Republic
 * 
 * Juraj Jurco <xjuraj@isibrno.cz>
 * Institute of Scientific Instruments of the ASCR, v. v. i.
 * Brno, Czech Republic
 */

package cz.academyofscience.upt.challenge2014;

import com.googlecode.jcsv.CSVStrategy;
import com.googlecode.jcsv.reader.CSVReader;
import com.googlecode.jcsv.reader.internal.CSVReaderBuilder;
import com.googlecode.jcsv.reader.internal.DefaultCSVEntryParser;
import com.googlecode.jcsv.writer.CSVWriter;
import com.googlecode.jcsv.writer.internal.CSVWriterBuilder;
import com.googlecode.jcsv.writer.internal.DefaultCSVEntryConverter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

/**
 *
 * @author xjuraj
 */
public class CSVUtils {

    public static final String FS = System.getProperty("file.separator");


    /**
     * Save CSV file to string
     *
     * @param write Data to store
     * @param file File where store results
     * @return File where data were saved
     * @throws IOException in case there is a problem to store data
     */
    public static File saveCSVToFile(List<String[]> write, File file) throws IOException {
        Writer out = new FileWriter(file);
        CSVWriter<String[]> csvWriter = new CSVWriterBuilder<String[]>(out).entryConverter(new DefaultCSVEntryConverter()).strategy(CSVStrategy.UK_DEFAULT).build();
        csvWriter.writeAll(write);
        csvWriter.flush();
        return file;
    }

    public static List<String[]> loadCSVFile(File file) throws FileNotFoundException, IOException {
        CSVReaderBuilder<String[]> csvReaderBuilder = new CSVReaderBuilder<String[]>(new FileReader(file)).
                strategy(CSVStrategy.UK_DEFAULT).
                entryParser(new DefaultCSVEntryParser());
        CSVReader<String[]> reader = csvReaderBuilder.build();
        return reader.readAll();
    }

    public static List<String[]> loadCSVFile(File file, char delimiter) throws FileNotFoundException, IOException {

        CSVStrategy strategy = new CSVStrategy(delimiter, CSVStrategy.UK_DEFAULT.getQuoteCharacter(), CSVStrategy.UK_DEFAULT.getCommentIndicator(), false, true);

        CSVReaderBuilder<String[]> csvReaderBuilder = new CSVReaderBuilder<String[]>(new FileReader(file)).
                strategy(strategy).entryParser(new DefaultCSVEntryParser());
        CSVReader<String[]> reader = csvReaderBuilder.build();
        return reader.readAll();
    }
    
    public static List<String[]> loadCSVFile(File file, char delimiter, char quoteIndicator) throws FileNotFoundException, IOException {

        CSVStrategy strategy = new CSVStrategy(delimiter, quoteIndicator, CSVStrategy.UK_DEFAULT.getCommentIndicator(), false, true);

        CSVReaderBuilder<String[]> csvReaderBuilder = new CSVReaderBuilder<String[]>(new FileReader(file)).
                strategy(strategy).entryParser(new DefaultCSVEntryParser());
        CSVReader<String[]> reader = csvReaderBuilder.build();
        return reader.readAll();
    }

}
