package innovimax.mixthem.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Random;

/**
* <p>Reads lines from a character-input file.</p>
* <p>This is the default implementation of IInputLine.</p>
* @see IInputLine
* @author Innovimax
* @version 1.0
*/
public class DefaultLineReader implements IInputLine {

	private final static int DEFAULT_RANDOM_SEED = 1789;

	private final Path path;
	private final BufferedReader reader;
	private final boolean first;
	private final Random random;
	private boolean jump;

	/**
 	* Creates a line reader.
 	* @param input The input file to be read
 	* @param first True is this reader is the first one for mixing
 	* @throws IOException - If an I/O error occurs
 	*/
	public DefaultLineReader(File input, boolean first) throws IOException {
		this.path = input.toPath();
		this.reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
		this.first = first;
		this.random = new Random(DEFAULT_RANDOM_SEED);
		this.jump = !first;
	}

	@Override
	public void setSeed(int seed) {
		this.random.setSeed(seed);
	}

	@Override
	public boolean hasLine() throws IOException {
		return this.reader.ready();
	}

	@Override
	public String nextLine(ReadType type) throws IOException {
		String line = null;
		if (hasLine()) {
			switch (type) {
				case _REGULAR:					
					line = this.reader.readLine();
					break;
				case _ALT_SIMPLE:
					if (!this.jump) {
						line = this.reader.readLine();						
					} else {
						this.reader.readLine();
					}					
					this.jump = !this.jump;
					break;
				case _ALT_RANDOM:					
					if (random.nextBoolean() == this.first) {
						line = this.reader.readLine();
					} else {
						this.reader.readLine();
					}					
					break;
			}
		}
		return line;
	}

	@Override
	public void close() throws IOException {
		this.reader.close();
	}

}

@@ -471,193 +471,55 @@
 nal 
-static int DEFAULT_RANDOM_SEED = 1789;%0A%0A%09private final Path path;%0A%09private final BufferedReader reader;%0A%09private final boolean first;%0A%09private final Random random;%0A%09private boolean jump
+Path path;%0A%09private final BufferedReader reader
 ;%0A%0A%09
@@ -595,73 +595,10 @@
 read
-%0A
  %09
-* @param first True is this reader is the first one for mixing
 %0A %09*
@@ -688,23 +688,8 @@
 nput
-, boolean first
 ) th
@@ -811,182 +811,15 @@
 _8);
-%0A
 %09%09
-this.first = first;%0A%09%09this.random = new Random(DEFAULT_RANDOM_SEED);%0A%09%09this.jump = !first;%0A%09%7D%0A%0A%09@Override%0A%09public void setSeed(int seed) %7B%0A%09%09this.random.setSeed(seed);
 %0A%09%7D%0A
+%09
 %0A%09@O
@@ -1026,464 +1026,38 @@
 %0A%09%09%09
-switch (type) %7B%0A%09%09%09%09case _REGULAR:%09%09%09%09%09%0A%09%09%09%09%09line = this.reader.readLine();%0A%09%09%09%09%09break;%0A%09%09%09%09case _ALT_SIMPLE:%0A%09%09%09%09%09if (!this.jump) %7B%0A%09%09%09%09%09%09line = this.reader.readLine();%09%09%09%09%09%09%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09this.reader.readLine();%0A%09%09%09%09%09%7D%09%09%09%09%09%0A%09%09%09%09%09this.jump = !this.jump;%0A%09%09%09%09%09break;%0A%09%09%09%09case _ALT_RANDOM:%09%09%09%09%09%0A%09%09%09%09%09if (random.nextBoolean() == this.first) %7B%0A%09%09%09%09%09%09line = this.reader.readLine();%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09this.reader.readLine();%0A%09%09%09%09%09%7D%09%09%09%09%09%0A%09%09%09%09%09break;%0A%09%09%09%7D
+line = this.reader.readLine();
 %0A%09%09%7D

remove reading type, seed and alt management