package de.hft.stuttgart.water;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.tuple.Pair;

public class LeafIndexInterceptionCorrelation {

	public static void main(String[] args) throws IOException {
		List<String> lines = Files.readAllLines(Paths.get("PercipitationTable.txt"));
		String[] header = lines.get(0).split(",");
		System.out.println(Arrays.toString(header));
		
		List<Pair<Double, Double>> laiInterceptionList = new ArrayList<>();
		
		for (int i = 1; i < lines.size(); i++) {
			String line = lines.get(i);
			String code = line.substring(0, 3);
			line = line.substring(3);
//			System.out.println(code);
			int speciesIndex = indexOf(Pattern.compile("[0-9–]"), line);
			String species = line.substring(0, speciesIndex).strip();
			line = line.substring(speciesIndex);
//			System.out.println(species);
			String[] values = line.split(" ");
//			System.out.println(Arrays.toString(values));
			String lai = values[4];
//			System.out.println("LAI: " + values[4]);
//			System.out.println("I: " + values[values.length - 1]);
			String interception = values[values.length - 1];
			if (!(lai.equals("–") || interception.equals("–"))) {
				// worth looking at
				System.out.println(species);
				laiInterceptionList.add(Pair.of(Double.parseDouble(lai), Double.parseDouble(interception)));
			}
		}
		for (Pair<Double, Double> pair : laiInterceptionList) {
			System.out.println(pair.getLeft() + "," + pair.getRight());
		}
		
		
	}

	/**
	 *  @return index of pattern in s or -1, if not found
	 */
	public static int indexOf(Pattern pattern, String s) {
		Matcher matcher = pattern.matcher(s);
		return matcher.find() ? matcher.start() : -1;
	}

}
