/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.domain.management.security.adduser;

import static org.jboss.as.domain.management.security.adduser.AddUser.NEW_LINE;
import static org.jboss.as.domain.management.security.adduser.AddUser.SPACE;

import java.util.ArrayList;
import java.util.List;

import org.jboss.as.domain.management.logging.DomainManagementLogger;

/**
 * State to display a message to the user with option to confirm a choice.
 * <p/>
 * This state handles either a yes or no outcome and will loop with an error
 * on invalid input.
 */
public class ConfirmationChoice implements State {

    private ConsoleWrapper theConsole;
    private final String[] messageLines;
    private final String prompt;
    private final State yesState;
    private final State noState;

    private static final int YES = 0;
    private static final int NO = 1;
    private static final int INVALID = 2;

    public ConfirmationChoice(ConsoleWrapper theConsole,final String[] messageLines, final String prompt, final State yesState, final State noState) {
        this.theConsole = theConsole;
        this.messageLines = messageLines;
        this.prompt = prompt;
        this.yesState = yesState;
        this.noState = noState;
    }

    public ConfirmationChoice(ConsoleWrapper theConsole, final String message, final String prompt, final State yesState,
            final State noState) {
        this(theConsole, new String[] { message }, prompt, yesState, noState);
    }

    @Override
    public State execute() {
        if (messageLines != null) {
            for (String message : messageLines) {
                theConsole.printf(message);
                theConsole.printf(NEW_LINE);
            }
        }

        theConsole.printf(prompt);
        String temp = theConsole.readLine(SPACE);

        switch (convertResponse(temp)) {
            case YES:
                return yesState;
            case NO:
                return noState;
            default: {
                List<String> acceptedValues = new ArrayList<String>(4);
                acceptedValues.add(DomainManagementLogger.ROOT_LOGGER.yes());
                if (DomainManagementLogger.ROOT_LOGGER.shortYes().length() > 0) {
                    acceptedValues.add(DomainManagementLogger.ROOT_LOGGER.shortYes());
                }
                acceptedValues.add(DomainManagementLogger.ROOT_LOGGER.no());
                if (DomainManagementLogger.ROOT_LOGGER.shortNo().length() > 0) {
                    acceptedValues.add(DomainManagementLogger.ROOT_LOGGER.shortNo());
                }
                StringBuilder sb = new StringBuilder(acceptedValues.get(0));
                for (int i = 1; i < acceptedValues.size() - 1; i++) {
                    sb.append(", ");
                    sb.append(acceptedValues.get(i));
                }

                return new ErrorState(theConsole, DomainManagementLogger.ROOT_LOGGER.invalidConfirmationResponse(sb.toString(), acceptedValues.get(acceptedValues.size() - 1)), this);
            }
        }
    }

    private int convertResponse(final String response) {
        if (response != null) {
            String temp = response.toLowerCase(); // We now need to match on the current local.
            if (DomainManagementLogger.ROOT_LOGGER.yes().equals(temp) || DomainManagementLogger.ROOT_LOGGER.shortYes().equals(temp)) {
                return YES;
            }

            if (DomainManagementLogger.ROOT_LOGGER.no().equals(temp) || DomainManagementLogger.ROOT_LOGGER.shortNo().equals(temp)) {
                return NO;
            }
        }

        return INVALID;
    }

}

@@ -1283,16 +1283,41 @@
 il.List;
+%0Aimport java.util.Locale;
 %0A%0Aimport
@@ -1622,16 +1622,613 @@
 tate %7B%0A%0A
+    // These are deliberately using the default locale i.e. the same as the language the interface is presented in.%0A    private static final String LONG_YES = DomainManagementLogger.ROOT_LOGGER.yes().toLowerCase(Locale.getDefault());%0A    private static final String LONG_NO = DomainManagementLogger.ROOT_LOGGER.no().toLowerCase(Locale.getDefault());%0A    private static final String SHORT_YES = DomainManagementLogger.ROOT_LOGGER.shortYes().toLowerCase(Locale.getDefault());%0A    private static final String SHORT_NO = DomainManagementLogger.ROOT_LOGGER.shortNo().toLowerCase(Locale.getDefault());%0A%0A
     priv
@@ -4871,286 +4871,150 @@
 if (
-DomainManagementLogger.ROOT_LOGGER.yes().equals(temp) %7C%7C DomainManagementLogger.ROOT_LOGGER.shortYes().equals(temp)) %7B%0A                return YES;%0A            %7D%0A%0A            if (DomainManagementLogger.ROOT_LOGGER.no().equals(temp) %7C%7C DomainManagementLogger.ROOT_LOGGER.shortNo()
+LONG_YES.equals(temp) %7C%7C SHORT_YES.equals(temp)) %7B%0A                return YES;%0A            %7D%0A%0A            if (LONG_NO.equals(temp) %7C%7C SHORT_NO
 .equ

Convert the acceptable responses to lower case so comparision matches conversion of user input.