package ec.app.complexregression;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import library.semantics.TestCase;

import org.apache.commons.math3.complex.Complex;

import ec.EvolutionState;
import ec.app.regression.testcases.TestCaseFactory;
import ec.util.Parameter;

public class ComplexCsvTestCaseFactory extends TestCaseFactory {

	@Override
	public List<TestCase> compute(EvolutionState state) {
		File file = state.parameters.getFile(new Parameter("testCases.inputData"), null);
		List<TestCase> testCases = new ArrayList<TestCase>();

		BufferedReader reader = null;
		double argRe, argIm;
		double valRe, valIm;

		try {
			reader = new BufferedReader(new FileReader(file));

			//skip header
			reader.readLine();

			String line;
			while ((line = reader.readLine()) != null) {
				StringTokenizer tokenizer = new StringTokenizer(line, ",");

				argRe = Double.parseDouble(tokenizer.nextToken());
				argIm = Double.parseDouble(tokenizer.nextToken());
				valRe = Double.parseDouble(tokenizer.nextToken());
				valIm = Double.parseDouble(tokenizer.nextToken());

				//testCases.put(new Complex(argRe, argIm), new Complex(valRe, valIm));
				TestCase<Complex> tc = new TestCase<Complex>(new Complex(valRe, valIm), new Complex(argRe, argIm));
				testCases.add(tc);
			}

			return testCases;
		} catch (IOException ex) {
			throw new RuntimeException(ex);
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException ex) {
				throw new RuntimeException(ex);
			}
		}
	}

}
