/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.liferay.blade.cli.util;

import java.io.InputStream;
import java.io.PrintStream;

import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Scanner;

/**
 * @author Christopher Bryan Boyd
 */
public class Prompter {

	public static boolean confirm(String question) {
		return confirm(question, System.in, System.out, Optional.empty());
	}

	public static boolean confirm(String question, InputStream in, PrintStream out, Optional<Boolean> defaultAnswer) {
		String questionWithPrompt = _buildQuestionWithPrompt(question, defaultAnswer);

		Optional<Boolean> answer = _getBooleanAnswer(questionWithPrompt, in, out, defaultAnswer);

		if (answer.isPresent()) {
			return answer.get();
		}
		else {
			throw new NoSuchElementException("Unable to acquire an answer");
		}
	}

	private static String _buildQuestionWithPrompt(String question, Optional<Boolean> defaultAnswer) {
		String yesDefault = "y";
		String noDefault = "n";

		if (defaultAnswer.isPresent()) {
			if (defaultAnswer.get()) {
				yesDefault = "Y";
			}
			else {
				noDefault = "N";
			}
		}

		return question + " [" + yesDefault + "/" + noDefault + "]";
	}

	private static Optional<Boolean> _getBooleanAnswer(
		String questionWithPrompt, InputStream in, PrintStream out, Optional<Boolean> defaultAnswer) {

		Optional<Boolean> answer = Optional.empty();

		while (!answer.isPresent()) {
			out.println(questionWithPrompt);

			try (Scanner scanner = new Scanner(in)) {
				String decision = scanner.nextLine();

				decision = decision.toLowerCase();

				decision = decision.trim();

				switch (decision) {
					case "y":
					case "yes":
						answer = Optional.of(true);

						break;
					case "n":
					case "no":
						answer = Optional.of(false);

						break;
					default:
						if (defaultAnswer.isPresent()) {
							answer = Optional.of(defaultAnswer.get());
						}
						else {
							out.println("Unrecognized input: " + decision);
							continue;
						}

						break;
				}
			}
			catch (NoSuchElementException nsee) {
				if (defaultAnswer.isPresent()) {
					answer = Optional.of(defaultAnswer.get());
				}
				else {
					out.println(nsee.getMessage());
					continue;
				}
			}
		}

		return answer;
	}

}
@@ -1749,9 +1749,9 @@
 + %22 
-%5B
+(
 %22 + 
@@ -1786,9 +1786,9 @@
  + %22
-%5D
+)
 %22;%0A%09
@@ -2186,35 +2186,32 @@
 ase();%0A%0A%09%09%09%09
-decision = 
+switch (
 decision.tri
@@ -2213,39 +2213,16 @@
 n.trim()
-;%0A%0A%09%09%09%09switch (decision
 ) %7B%0A%09%09%09%09
@@ -2556,16 +2556,17 @@
 ision);%0A
+%0A
 %09%09%09%09%09%09%09c
@@ -2746,24 +2746,24 @@
 %0A%09%09%09%09else %7B%0A
-
 %09%09%09%09%09out.pri
@@ -2783,24 +2783,25 @@
 Message());%0A
+%0A
 %09%09%09%09%09continu

use more common format (y/N) instead of [y/N]