package analyzer;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class TrainFileReader {
	String mFilename;
	float[][] mInput;
	float[][] mOutput;
	float[][] mNormalizedInput;
	float[][] mNormalizedOutput;
	float[] mMinInput;
	float[] mMaxInput;

	float[] mMinOutput;
	float[] mMaxOutput;

	public TrainFileReader(String fileName) {
		mFilename = fileName;

		mMinInput = new float[3];
		mMaxInput = new float[3];

		mMinOutput = new float[6];
		mMaxOutput = new float[6];

		for (int i = 0; i < 3; i++) {
			mMinInput[i] = Float.MAX_VALUE;
			mMaxInput[i] = Float.MIN_VALUE;
		}

		for (int i = 0; i < 6; i++) {
			mMinOutput[i] = Float.MAX_VALUE;
			mMaxOutput[i] = Float.MIN_VALUE;
		}

		Scanner in = null;
		ArrayList<ArrayList<Float>> inp = new ArrayList<ArrayList<Float>>();
		ArrayList<ArrayList<Float>> outp = new ArrayList<ArrayList<Float>>();

		try {
			in = new Scanner(new File(fileName));
			while (in.hasNext()) {

				String line = in.nextLine();
				String[] content = line.split(" ");
				if (content.length != 3)
					continue;

				String[] input = content[1].split("\\.");
				if (input.length != 3)
					continue;

				String[] output = content[2].split(",");
				if (output.length != 6)
					continue;

				ArrayList<Float> lineInput = new ArrayList<Float>();
				for (int i = 0; i < input.length; i++) {
					float inputI = Float.parseFloat(input[i]);
					lineInput.add(inputI);
					if (inputI > mMaxInput[i])
						mMaxInput[i] = inputI;

					if (inputI < mMinInput[i])
						mMinInput[i] = inputI;
				}
				inp.add(lineInput);

				ArrayList<Float> lineOutput = new ArrayList<Float>();
				for (int i = 0; i < output.length; i++) {
					float outputI = Float.parseFloat(output[i]);
					lineOutput.add(outputI);

					if (outputI > mMaxOutput[i])
						mMaxOutput[i] = outputI;

					if (outputI < mMinOutput[i])
						mMinOutput[i] = outputI;
				}

				outp.add(lineOutput);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			in.close();
		}
		mInput = new float[inp.size()][3];
		mNormalizedInput = new float[inp.size()][3];
		for (int i = 0; i < mInput.length; i++) {
			for (int j = 0; j < 3; j++) {
				mInput[i][j] = inp.get(i).get(j);
				mNormalizedInput[i][j] = (mInput[i][j] - mMinInput[j]) / (mMaxInput[j] - mMinInput[j]);
				// System.out.println(mNormalizedInput[i][j]+" ");
			}
			// System.out.println();
		}

		mOutput = new float[outp.size()][6];
		mNormalizedOutput = new float[outp.size()][6];
		for (int i = 0; i < mOutput.length; i++) {
			for (int j = 0; j < 6; j++) {
				mOutput[i][j] = outp.get(i).get(j);
				mNormalizedOutput[i][j] = (mOutput[i][j] - mMinOutput[j]) / (mMaxOutput[j] - mMinOutput[j]);
			}
		}
	}

	public float[][] getInput() {
		return mInput;
	}

	public float[][] getOutput() {
		return mOutput;
	}

	public float[][] getNormalizedInput() {
		return mNormalizedInput;
	}

	public float[][] getNormalizedOutput() {
		return mNormalizedOutput;
	}

}
