package com.alexrnl.subtitlecorrector.gui.view;

import java.io.PrintStream;
import java.util.Scanner;
import java.util.logging.Logger;

import com.alexrnl.commons.utils.Word;
import com.alexrnl.subtitlecorrector.service.SessionParameters;
import com.alexrnl.subtitlecorrector.service.UserPrompt;
import com.alexrnl.subtitlecorrector.service.UserPromptAnswer;

/**
 * A console implementation for the {@link UserPrompt} interface.
 * @author Alex
 */
public class ConsoleUserPrompt implements UserPrompt {
	/** Logger */
	private static Logger		lg	= Logger.getLogger(ConsoleUserPrompt.class.getName());
	
	/** The scanner plugged on the console input */
	private Scanner				input;
	/** The console output */
	private final PrintStream	output;
	
	/**
	 * Constructor #1.<br />
	 */
	public ConsoleUserPrompt () {
		super();
		input = null;
		output = System.out;
	}
	
	@Override
	public void startSession (final SessionParameters parameters) {
		if (input != null) {
			throw new IllegalStateException("Session was not properly stop, input was not null");
		}
		input = new Scanner(System.in);
	}

	@Override
	public void stopSession () {
		if (input == null) {
			throw new IllegalStateException("Session was not properly started, input was null");
		}
		input.close();
		input = null;
		
	}

	@Override
	public UserPromptAnswer confirm (final String context, final Word original, final String replacement) {
		if (input == null) {
			throw new IllegalStateException("Session was not properly started, input is null, " +
					"cannot confirm replacement");
		}
		String answer;
		boolean cancelled = false;
		boolean rememberChoice;
		
		// TODO translate
		output.println("Replace '" + original + "' by '" + replacement + "'?");
		if (context != null) {
			output.println("Context:\n\t");
			output.println(context);
		}
		output.println("y/n >");
		final boolean keep = 'y' == input.nextLine().charAt(0);
		if (keep) {
			answer = replacement;
		} else {
			output.println("Type the replacement:");
			answer = input.nextLine();
			if (answer.isEmpty()) {
				cancelled = true;
			}
		}
		output.println("Remember choice for the rest of the session?");
		output.println("y/n >");
		rememberChoice = 'y' == input.nextLine().charAt(0);
		
		return new UserPromptAnswer(answer, cancelled, rememberChoice);
	}
	
	@Override
	public UserPromptAnswer confirm (final Word original, final String replacement) {
		return confirm(null, original, replacement);
	}
}

@@ -42,16 +42,44 @@
 .view;%0A%0A
+import java.io.InputStream;%0A
 import j
@@ -622,24 +622,91 @@
 tName());%0A%09%0A
+%09/** The console input stream */%0A%09private final InputStream%09input;%0A
 %09/** The sca
@@ -738,16 +738,23 @@
 le input
+Scanner
  */%0A%09pri
@@ -770,24 +770,31 @@
 ner%09%09%09%09input
+Scanner
 ;%0A%09/** The c
@@ -885,37 +885,257 @@
 %0A%09 *
-/%0A%09public ConsoleUserPrompt (
+ @param input%0A%09 *        the input stream to use for reading the user's answers.%0A%09 * @param output%0A%09 *        the output to use for displaying information to the user.%0A%09 */%0A%09public ConsoleUserPrompt (final InputStream input, final PrintStream output
 ) %7B%0A
@@ -1143,32 +1143,37 @@
 %09super();%0A%09%09
+this.
 input = 
 null;%0A%09%09outp
@@ -1164,24 +1164,217 @@
 t = 
-null;%0A%09%09output =
+input;%0A%09%09this.output = output;%0A%09%7D%0A%09%0A%09/**%0A%09 * Constructor #2.%3Cbr /%3E%0A%09 * Build a %7B@link ConsoleUserPrompt%7D with the %7B@link System#in%7D and %7B@link System#out%7D.%0A%09 */%0A%09public ConsoleUserPrompt () %7B%0A%09%09this(System.in,
  Sys
@@ -1380,16 +1380,17 @@
 stem.out
+)
 ;%0A%09%7D%0A%09%0A%09
@@ -1475,16 +1475,23 @@
 f (input
+Scanner
  != null
@@ -1562,24 +1562,31 @@
  stop, input
+Scanner
  was not nul
@@ -1597,24 +1597,31 @@
 %0A%09%09%7D%0A%09%09input
+Scanner
  = new Scann
@@ -1623,25 +1623,21 @@
 Scanner(
-System.in
+input
 );%0A%09%7D%0A%0A%09
@@ -1679,32 +1679,39 @@
 () %7B%0A%09%09if (input
+Scanner
  == null) %7B%0A%09%09%09t
@@ -1781,16 +1781,23 @@
 d, input
+Scanner
  was nul
@@ -1812,16 +1812,23 @@
 %0A%09%09input
+Scanner
 .close()
@@ -1836,16 +1836,23 @@
 %0A%09%09input
+Scanner
  = null;
@@ -1986,16 +1986,23 @@
 f (input
+Scanner
  == null
@@ -2080,16 +2080,23 @@
 d, input
+Scanner
  is null
@@ -2456,32 +2456,39 @@
 p = 'y' == input
+Scanner
 .nextLine().char
@@ -2601,24 +2601,31 @@
 swer = input
+Scanner
 .nextLine();
@@ -2807,16 +2807,23 @@
 == input
+Scanner
 .nextLin

Update ConsoleUSerPrompt to allow custom input/output