/*
 * 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.apache.karaf.shell.console.completer;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.felix.gogo.commands.basic.AbstractCommand;
import org.apache.felix.gogo.runtime.CommandProxy;
import org.apache.felix.gogo.runtime.CommandSessionImpl;
import org.apache.felix.service.command.CommandSession;
import org.apache.felix.service.command.Function;
import org.apache.karaf.shell.console.CompletableFunction;
import org.apache.karaf.shell.console.Completer;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

/**
 * Like the {@link org.apache.karaf.shell.console.completer.CommandsCompleter} but does not use OSGi but is
 * instead used from the non-OSGi {@link org.apache.karaf.shell.console.Main}
 */
public class CommandsCompleter implements Completer {

    private CommandSession session;
    private final List<Completer> completers = new ArrayList<Completer>();
    private final Set<String> commands = new HashSet<String>();

    public CommandsCompleter(CommandSession session) {
        this.session = session;
    }


    public int complete(String buffer, int cursor, List<String> candidates) {
        checkData();
        int res = new AggregateCompleter(completers).complete(buffer, cursor, candidates);
        Collections.sort(candidates);
        return res;
    }

    protected synchronized void checkData() {
        // Copy the set to avoid concurrent modification exceptions
        // TODO: fix that in gogo instead
        Set<String> names = new HashSet<String>((Set<String>) session.get(CommandSessionImpl.COMMANDS));
        if (!names.equals(commands)) {
            commands.clear();
            completers.clear();
            for (String command : names) {
                Function function = (Function) session.get(command);
                function = unProxy(function);
                if (function instanceof AbstractCommand) {
                    completers.add(new ArgumentCompleter(session, (AbstractCommand) function, command));
                }
                commands.add(command);
            }
        }
    }

    protected Function unProxy(Function function) {
        try {
            if (function instanceof CommandProxy) {
                Field contextField = function.getClass().getDeclaredField("context");
                Field referenceField = function.getClass().getDeclaredField("reference");
                contextField.setAccessible(true);
                referenceField.setAccessible(true);
                BundleContext context = (BundleContext) contextField.get(function);
                ServiceReference reference = (ServiceReference) referenceField.get(function);
                Object target = context.getService(reference);
                try {
                    if (target instanceof Function) {
                        function = (Function) target;
                    }
                } finally {
                    context.ungetService(reference);
                }
            }
        } catch (Throwable t) {
        }
        return function;
    }

}


@@ -1074,16 +1074,62 @@
 ommand;%0A
+import org.apache.felix.gogo.runtime.Closure;%0A
 import o
@@ -2656,32 +2656,237 @@
 pleters.clear();
+%0A%0A            // get command aliases%0A            Set%3CString%3E aliases = this.getAliases();%0A            completers.add(new StringsCompleter(aliases));%0A%0A            // add argument completers for each command
 %0A            for
@@ -3272,32 +3272,510 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Get the aliases defined in the console session.%0A     *%0A     * @return the aliases set%0A     */%0A    private Set%3CString%3E getAliases() %7B%0A        Set%3CString%3E vars = (Set%3CString%3E) session.get(null);%0A        Set%3CString%3E aliases = new HashSet%3CString%3E();%0A        for (String var : vars) %7B%0A            Object content = session.get(var);%0A            if (content instanceof Closure)  %7B%0A                aliases.add(var);%0A            %7D%0A        %7D%0A        return aliases;%0A    %7D%0A%0A
     protected Fu

Add console aliases completer.