/*
 * 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 java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Reding CSV filesby columns. You can specify how many rows are header rows.
 *
 * @author xjuraj
 */
public class FastCSVReader {

    private final File input;
    private int headerLinesCount = -1;
    public FastCSVReader(File input, int headerLinesCount){
        this.input = input;
        this.headerLinesCount = headerLinesCount;
    }
    
    private String[][] header;
    private List<String> timeChannel = new ArrayList<>();
    private List<double[]> data = new ArrayList<>();

    public void read() throws FileNotFoundException, IOException {
        try (FileInputStream fis = new FileInputStream(input)) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(fis)));
            String line;
            header = new String[headerLinesCount][0];
            for (int i = 0; i < headerLinesCount; i++) {
                if((line = reader.readLine()) != null){
                    header[i] = line.split("','");
                    header[i][0] = header[i][0].replace("'", "");
                    header[i][header[i].length-1] = header[i][header[i].length-1].replace("'", "");
                }
            }
            while ((line = reader.readLine()) != null) {
                String[] split = line.split(",");
                split[0] = split[0].replace("'", "");
                split[split.length-1] = split[split.length-1].replace("'", "");
                timeChannel.add(split[0]);
                double[] dataRow = new double[split.length-1];
                for (int i = 0; i < dataRow.length; i++) {
                    if(split[i+1].equals("-"))split[i+1] = "0.0";
                    dataRow[i] = Double.parseDouble(split[i+1]);
                }
                data.add(dataRow);
            }
        }
    }
    
    /**
     * Convert loadeddata into columns. Original data arestored by rows
     * @return columns with data
     */
    public double[][] getDataColumns(){
        double[][] result = new double[data.get(0).length][data.size()];
        for (int i = 0; i < data.size(); i++) {
            for (int j = 0; j < data.get(0).length; j++) {
                result[j][i] = data.get(i)[j];
            }
        }
        return result;
    }

    public String[][] getHeader() {
        return header;
    }

    public void setHeader(String[][] header) {
        this.header = header;
    }

    public List<String> getTimeChannel() {
        return timeChannel;
    }

    public void setTimeChannel(List<String> timeChannel) {
        this.timeChannel = timeChannel;
    }

    public List<double[]> getData() {
        return data;
    }

    public void setData(List<double[]> data) {
        this.data = data;
    }
    
    
}
