/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.sonatype.gshell.core.console;

import org.sonatype.gshell.Branding;
import org.sonatype.gshell.VariableNames;
import org.sonatype.gshell.Variables;
import org.sonatype.gshell.ansi.AnsiRenderer;
import org.sonatype.gshell.console.Console;
import org.codehaus.plexus.interpolation.AbstractValueSource;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.Interpolator;
import org.codehaus.plexus.interpolation.PrefixedObjectValueSource;
import org.codehaus.plexus.interpolation.StringSearchInterpolator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * {@link org.sonatype.gshell.console.Console.Prompter} component.
 *
 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
 *
 * @since 2.0
 */
public class ConsolePrompterImpl
    implements Console.Prompter, VariableNames
{
    private final Logger log = LoggerFactory.getLogger(getClass());

    private static final String SHELL_BRANDING = "shell.branding";

    // NOTE: Have to use %{} here to avoid causing problems with ${} variable interpolation

    private final Interpolator interp = new StringSearchInterpolator("%{", "}");

    private final AnsiRenderer renderer = new AnsiRenderer();

    private final Variables vars;

    private final Branding branding;

    public ConsolePrompterImpl(final Variables vars, final Branding branding) {
        assert vars != null;
        assert branding != null;
        this.vars = vars;
        this.branding = branding;

        interp.addValueSource(new AbstractValueSource(false) {
            public Object getValue(final String expression) {
                return vars.get(expression);
            }
        });
        interp.addValueSource(new PrefixedObjectValueSource(SHELL_BRANDING, branding));
        interp.addValueSource(new AbstractValueSource(false) {
            public Object getValue(final String expression) {
                return System.getProperty(expression);
            }
        });
    }

    public String prompt() {
        String pattern = vars.get(SHELL_PROMPT, String.class);
        String prompt = interpolate(pattern);

        // Use a default prompt if we don't have anything here
        if (prompt == null) {
            prompt = interpolate(branding.getPrompt());
            if (prompt == null) {
                prompt = DEFAULT_PROMPT;
            }
        }

        //
        // TODO: Support rendering ~ for home dir here somewhere
        //

        // Encode ANSI muck if it looks like there are codes encoded, need to render here as the console uses raw streams
        if (AnsiRenderer.test(prompt)) {
            prompt = renderer.render(prompt);
        }

        return prompt;
    }

    private String interpolate(final String pattern) {
        String prompt = null;
        if (pattern != null) {
            try {
                prompt = interp.interpolate(pattern);
            }
            catch (InterpolationException e) {
                log.warn("Failed to interpolate: " + pattern, e);
            }
        }
        return prompt;
    }
}
@@ -844,16 +844,73 @@
 nsole;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -1112,380 +1112,71 @@
 le;%0A
+%0A
 import 
-org.codehaus.plexus.interpolation.AbstractValueSource;%0Aimport org.codehaus.plexus.interpolation.InterpolationException;%0Aimport org.codehaus.plexus.interpolation.Interpolator;%0Aimport org.codehaus.plexus.interpolation.PrefixedObjectValueSource;%0Aimport org.codehaus.plexus.interpolation.StringSearchInterpolator;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
+java.util.regex.Matcher;%0Aimport java.util.regex.Pattern
 ;%0A%0A/
@@ -1661,72 +1661,71 @@
 ate 
-final Interpolator interp = new StringSearchInterpolator(%22%25%7B%22, %22
+static final Pattern PATTERN = Pattern.compile(%22%5C%5C%25%5C%5C%7B(%5B%5E%7D%5D+)%5C%5C
 %7D%22);
@@ -2067,499 +2067,8 @@
 ing;
-%0A%0A        interp.addValueSource(new AbstractValueSource(false) %7B%0A            public Object getValue(final String expression) %7B%0A                return vars.get(expression);%0A            %7D%0A        %7D);%0A        interp.addValueSource(new PrefixedObjectValueSource(SHELL_BRANDING, branding));%0A        interp.addValueSource(new AbstractValueSource(false) %7B%0A            public Object getValue(final String expression) %7B%0A                return System.getProperty(expression);%0A            %7D%0A        %7D);
 %0A   
@@ -2820,52 +2820,167 @@
 ing 
-interpolate(final String pattern) %7B%0A
+evaluate(String input) %7B%0A        if (input != null) %7B%0A            Matcher matcher = PATTERN.matcher(input);%0A%0A            while (matcher.find()) %7B%0A     
         
 Stri
@@ -2979,38 +2979,99 @@
     
+   
 String 
-prompt = null;%0A
+key = matcher.group(1);%0A                Object rep = vars.get(key);%0A     
         
 if (
@@ -3070,21 +3070,20 @@
     
+   
 if (
-pattern !
+rep =
 = nu
@@ -3104,65 +3104,174 @@
     
-try %7B%0A                prompt = interp.interpolate(pattern
+        rep = System.getProperty(key);%0A                %7D%0A                if (rep != null) %7B%0A                    input = input.replace(matcher.group(0), rep.toString()
 );%0A 
@@ -3270,37 +3270,32 @@
 ng());%0A         
-   %7D%0A
             catc
@@ -3293,43 +3293,47 @@
     
- c
+m
 atch
- (InterpolationException e) %7B
+er.reset(input);%0A                %7D
 %0A   
@@ -3345,75 +3345,205 @@
     
+%7D%0A
     
-log.warn(%22Failed to interpolate: %22 + pattern, e);%0A            %7D
+    %7D%0A%0A        return input;%0A    %7D%0A%0A    private String interpolate(final String pattern) %7B%0A        String prompt = null;%0A        if (pattern != null) %7B%0A            prompt = evaluate(pattern);
 %0A   

Drop plexus-interpolation