/*
 * 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 cz.academyofscience.upt.challenge2014.qrs.QRSFinder;
import cz.academyofscience.upt.challenge2014.qrs.SelectionMember;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;

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

    private static final boolean LOCAL_GENERATION = false;
    private static final QRSFinder.OUTPUT_TYPE[] CHALLENGE_OUT_TYPE = new QRSFinder.OUTPUT_TYPE[]{QRSFinder.OUTPUT_TYPE.STDOUT_QRS};
    private static final QRSFinder.OUTPUT_TYPE[] LOCAL_OUT_TYPE = new QRSFinder.OUTPUT_TYPE[]{QRSFinder.OUTPUT_TYPE.OUTPUT_STREAM_SEL};

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        if (LOCAL_GENERATION) {
            generateAllMarks();
            if (true) {
                return;
            }
        }
//        if(true) throw new IllegalArgumentException("This shit happens when java program crashes - throwing example exception at the beginning of the program.");
        if (args.length != 1) {
            throw new IllegalArgumentException("Number of parameters have to be 1 - full path to CSV file."
                    + "Number of parameters: " + args.length + " and they are: " + Arrays.deepToString(args));
        }
        String csvFile = args[0];

        Channels chs = (new Channels()).loadChannels(new File(csvFile));

        QRSFinder qrs = new QRSFinder();
        List<SelectionMember> marks = qrs.findQRS(chs);
        qrs.printMarkersIntoOutputStream(marks, System.out, (LOCAL_GENERATION) ? LOCAL_OUT_TYPE : CHALLENGE_OUT_TYPE);
    }

    public static void generateAllMarks() throws FileNotFoundException, IOException {
        //Arrythmia DB
//        String inputFolder = "/home/xjuraj/Dropbox/Work/AcademyOfScience/converted/MIT-BIH-Arrythmia database/";
//        String outFolder = "/home/xjuraj/Dropbox/Work/AcademyOfScience/Challenge2014/markers/arrythmia/";
//        int from = 233;
//        int to = 233;
        //Challenge test data
//        String inputFolder = "c:\\Users\\xjuraj\\Documents\\Work\\datasets\\challenge2014\\";
//        String outFolder = "x:\\d02\\us\\xjuraj\\challenge\\";
//        int from = 100;
//        int to = 199;
//        
//        DescriptiveStatistics ds = new DescriptiveStatistics();
//        for (int i = from; i <= to; i++) {
//            long startTime = System.nanoTime();
//            System.out.println("Processing file "+i+"...");
////            System.out.print(String.format("\n%d\t", i));
//            File in = new File(inputFolder + i + ".csv");
//            File out = new File(outFolder + i + "_java.sel");
//
//            FileOutputStream output = new FileOutputStream(out);
//            Channels chs = (new Channels()).loadChannels(in);
//
//            QRSFinder qrs = new QRSFinder();
//            List<SelectionMember> marks = qrs.findQRS(chs);
//            qrs.printMarkersIntoOutputStream(marks, output, (LOCAL_GENERATION) ? LOCAL_OUT_TYPE : CHALLENGE_OUT_TYPE);
//            
//            ds.addValue(System.nanoTime() - startTime);
//        }
        
        
        DescriptiveStatistics ds = new DescriptiveStatistics();
        String inputFolder = "X:\\d02\\us\\plesinger\\Výsledky CINC\\výběr pro test\\";
        String outFolder = "X:\\d02\\us\\plesinger\\Výsledky CINC\\výběr pro test\\results_submission_2_4\\";
        String[] fileNameList = new String[]{"002_rec2_divnej_undetectable.csv", 
            "011_rec2_BP_100Hz.csv", "011_rec2_BP_500Hz.csv", "043_rec2_250Hz_extra50.csv", 
            "043_rec2_test_Vx_100Hz.csv", "10_257hz.csv", "20_130hz.csv", 
            "Easrec-1305141046_extras_5000Hz.csv", "Easrec-1305141046_extras_500Hz.csv", 
            "Easrec-1310211010_5000Hz.csv", "Easrec-1403071253_1250Hz.csv", "Easrec-1403071253_125Hz.csv", 
            "Easrec-1403071253_625Hz.csv", "RECORD_1403101051_1024Hz.csv", "RECORD_1403101051_256Hz.csv", 
            "RECORD_1403241110_128Hz.csv", "RECORD_1403241110_512Hz.csv"};
        

        for (String string : fileNameList) {
            long startTime = System.nanoTime();
            
            System.out.println("Processing file "+string+"...");
//            System.out.print(String.format("\n%d\t", i));
            File in = new File(inputFolder + string);
            File out = new File(outFolder + string + "_java2.sel");

            FileOutputStream output = new FileOutputStream(out);
            Channels chs = (new Channels()).loadChannels(in);

            QRSFinder qrs = new QRSFinder();
            List<SelectionMember> marks = qrs.findQRS(chs);
            qrs.printMarkersIntoOutputStream(marks, output, (LOCAL_GENERATION) ? LOCAL_OUT_TYPE : CHALLENGE_OUT_TYPE);
            ds.addValue(System.nanoTime() - startTime);
        }
        
        System.out.println(String.format("Total time: %.4fms", ds.getSum()));
        System.out.println(String.format("Mean time: %.4fms", ds.getMean()));
        System.out.println(String.format("StDev time: %.4fms", ds.getStandardDeviation()));

    }

}
