package de.theves.eclipse.gems.spotlight.internal.providers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandImageService;
import org.eclipse.ui.commands.ICommandService;

import de.theves.eclipse.gems.spotlight.internal.view.SpotlightItem;
import de.theves.eclipse.gems.spotlight.internal.view.SpotlightItemProvider;
import de.theves.eclipse.gems.spotlight.internal.view.SpotlightView.SpotlightItemsFilter;

public class CommandProvider implements SpotlightItemProvider {
	private IWorkbenchWindow window;

	public CommandProvider(IWorkbenchWindow window) {
		this.window = window;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<SpotlightItem> getItems(SpotlightItemsFilter filter, IProgressMonitor monitor) {
		List<SpotlightItem> items = new ArrayList<>();
		ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class);
		final Collection commandIds = commandService.getDefinedCommandIds();
		final Iterator commandIdItr = commandIds.iterator();
		while (commandIdItr.hasNext()) {
			final String currentCommandId = (String) commandIdItr.next();
			final Command command = commandService.getCommand(currentCommandId);
			if (command != null) {
				try {
					Collection combinations = ParameterizedCommand.generateCombinations(command);
					for (Iterator it = combinations.iterator(); it.hasNext();) {
						ParameterizedCommand pc = (ParameterizedCommand) it.next();
						items.add(new CommandItem(this, pc));
					}
				} catch (final NotDefinedException e) {
					// we do not add undefined commands because they are invalid
				}
			}
		}
		return items;
	}

	public ICommandImageService getCommandImageService() {
		return this.window.getService(ICommandImageService.class);
	}
	
	public IWorkbenchWindow getWindow() {
		return window;
	}

	@Override
	public String getLabel() {
		return "Commands";
	}

}

@@ -1792,16 +1792,138 @@
 next();%0A
+%09%09%09%09%09%09if (pc.getCommand().isDefined() && pc.getCommand().isEnabled()) %7B%0A%09%09%09%09%09%09%09// only add commands that can be executed%0A%09
 %09%09%09%09%09%09it
@@ -1958,16 +1958,24 @@
 , pc));%0A
+%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%7D%0A%09
@@ -2238,17 +2238,16 @@
 ss);%0A%09%7D%0A
-%09
 %0A%09public

add only commands that can be executed