package org.openforis.collect.earth.core.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import au.com.bytecode.opencsv.CSVReader;

public class CsvReaderUtils {
	
	private CsvReaderUtils(){
		
	}

	public static CSVReader getCsvReader(String csvFile) throws IOException {
		
		CSVReader commaSeparatedReader = getCsvReader(csvFile, ',');
		
		if( checkCsvReaderWorks( commaSeparatedReader ) ){
			// Close and open again a reader so it starts from the first line!
			commaSeparatedReader.close();
			return getCsvReader(csvFile, ',');
		}else{
			commaSeparatedReader.close();
			CSVReader semicolonSeparatedReader = getCsvReader(csvFile, ';');
			if( checkCsvReaderWorks(semicolonSeparatedReader) ){
				// Close and open again a reader so it starts from the first line!
				semicolonSeparatedReader.close();
				return getCsvReader(csvFile, ';');
			}else{
				semicolonSeparatedReader.close();
			}
		}
		
		throw new IllegalArgumentException("The CSV/CED plot file does not seem to contain actual comma separated values! " + csvFile );
	}
			
	public static boolean onlyEmptyCells(String[] csvRow) {
		for (String csvColumn : csvRow) {
			if( csvColumn.trim().length()>0){
				return false;
			}
		}
		return true;
	}

	
	private static boolean checkCsvReaderWorks(CSVReader csvReader) throws IOException {

		String[] csvRow = null;
				
		while ((csvRow = csvReader.readNext()) != null) {		
			if( csvRow.length == 1 && csvRow[0].trim().length() == 0 ){
				// This would be an empty line
				continue;
			} else if( csvRow.length == 1 && csvRow[0].trim().length() > 0){
				
				return false;
			}else if( csvRow.length < 3 ){
				return false;
			}else{
				return true;
			}
		}
		
		// If the script reaches this point it means that all the lines in the CSV file were empty!
		throw new IllegalArgumentException("The CSV/CED plot file has no data! All the lines are empty!");
	}

	private static CSVReader getCsvReader(String csvFile, char columnSeparator) throws FileNotFoundException {
		CSVReader reader;
		final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(csvFile), Charset.forName("UTF-8")));
		reader = new CSVReader(bufferedReader, columnSeparator);
		return reader;
	}
}

@@ -451,314 +451,137 @@
 %0D%0A%09%09
-CSVReader commaSeparatedReader = getCsvReader(csvFile, ',');%0D%0A%09%09%0D%0A%09%09if( checkCsvReaderWorks( commaSeparatedReader ) )%7B%0D%0A%09%09%09// Close and open again a reader so it starts from the first line!%0D%0A%09%09%09commaSeparatedReader.close();%0D%0A%09%09%09return getCsvReader(csvFile, ',');%0D%0A%09%09%7Delse%7B%0D%0A%09%09%09commaSeparatedReader.close();
+char%5B%5D possibleSeparators = new char%5B%5D%7B',', ';','%5Ct', '%7C'%7D;%0D%0A%09%09CSVReader csvReader = null;%0D%0A%09%09for (char c : possibleSeparators) %7B
 %0D%0A%09%09
@@ -591,25 +591,21 @@
 VReader 
-semicolon
+comma
 Separate
@@ -632,27 +632,25 @@
 er(csvFile, 
-';'
+c
 );%0D%0A%09%09%09if( c
@@ -668,25 +668,22 @@
 erWorks(
-semicolon
+ comma
 Separate
@@ -689,16 +689,17 @@
 edReader
+ 
 ) )%7B%0D%0A%09%09
@@ -704,152 +704,119 @@
 %09%09%09%09
-// Close and open again a reader so it starts from the first line!%0D%0A%09%09%09%09semicolonSeparatedReader.close();
+csvReader =getCsvReader(csvFile, c); // Get the reader again so that it starts from the first column
 %0D%0A%09%09%09%09
+b
 re
-turn getCsvReader(csvFile, ';')
+ak
 ;%0D%0A%09
@@ -833,17 +833,13 @@
 %09%09%09%09
-semicolon
+comma
 Sepa
@@ -875,16 +875,45 @@
 %09%7D%0D%0A%09%09%0D%0A
+%09%09if( csvReader == null )%7B%0D%0A%09
 %09%09throw 
@@ -1037,17 +1037,60 @@
 le );%0D%0A%09
-%7D
+%09%7Delse%7B%0D%0A%09%09%09return csvReader;%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A%09
 %0D%0A%09%09%09%0D%0A%09

Improve handling of different types of CSV separator characters