package analyzer;

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

import javax.swing.JOptionPane;

public class StructureReader {
	private String mFilepath;
	private int mM, mL, mN;
	private boolean mStructureState = true;
	private float[][] mWeightH;
	private float[][] mWeightO;

	public StructureReader(String filepath) {
		mFilepath = filepath;

		Scanner in = null;
		try {
			in = new Scanner(new File(mFilepath));

			// if (in.hasNext())
			// if (!in.next().equalsIgnoreCase("NN"))
			// throw new Exception("Incorrect Structure");

			if (in.hasNext())
				mM = Integer.parseInt(in.next());
			// else
			// throw new Exception("Incorrect Structure");

			if (in.hasNext())
				mL = Integer.parseInt(in.next());
			// else
			// throw new Exception("Incorrect Structure");

			if (in.hasNext())
				mN = Integer.parseInt(in.next());
			// else
			// throw new Exception("Incorrect Structure");

			mWeightH = new float[mL][mM];
			mWeightO = new float[mN][mL];

			for (int i = 0; i < mL; i++)
				for (int j = 0; j < mM; j++)
					if (in.hasNext())
						mWeightH[i][j] = Float.parseFloat(in.next());
			// else
			// throw new Exception("Incorrect Structure");

			for (int i = 0; i < mN; i++)
				for (int j = 0; j < mL; j++)
					if (in.hasNext())
						mWeightO[i][j] = Float.parseFloat(in.next());
			// else
			// throw new Exception("Incorrect Structure");
			in.close();
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "File not found.", "", JOptionPane.INFORMATION_MESSAGE);
			e.printStackTrace();
			mStructureState = false;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Parsing Error.", "", JOptionPane.INFORMATION_MESSAGE);
			e.printStackTrace();
			mStructureState = false;
			System.out.println("yes");
		}
		// System.out.println(mFilepath);
	}

	public boolean isCorrectStructure() {
		return mStructureState;
	}

	public int getInputLayerNeuronsNumber() {
		return mM;
	}

	public int getHiddenLayerNeuronsNumber() {
		return mL;
	}

	public int getOutputLayerNeuronsNumber() {
		return mN;
	}

	public float[][] getWeightH() {
		return mWeightH;
	}

	public float[][] getWeightO() {
		return mWeightO;
	}
}
