package core.keyChain.managers;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import core.config.Config;
import core.keyChain.KeyStroke;
import core.keyChain.TaskActivation;
import core.userDefinedTask.UserDefinedAction;

public class GlobalKeyActionManager extends KeyStrokeManager {

	private Set<UserDefinedAction> onKeyStrokePressedTasks;
	private Set<UserDefinedAction> onKeyReleasedTasks;

	public GlobalKeyActionManager(Config config) {
		super(config);
		onKeyStrokePressedTasks = new HashSet<>();
		onKeyReleasedTasks = new HashSet<>();
	}

	@Override
	public void startListening() {
		// Nothing to do.
	}

	@Override
	public Set<UserDefinedAction> onKeyStrokePressed(KeyStroke stroke) {
		return new HashSet<>(onKeyStrokePressedTasks);
	}

	@Override
	public Set<UserDefinedAction> onKeyStrokeReleased(KeyStroke stroke) {
		return new HashSet<>(onKeyReleasedTasks);
	}

	@Override
	public void clear() {
		// Nothing to do.
	}

	@Override
	public Set<UserDefinedAction> collision(Collection<TaskActivation> activations) {
		return new HashSet<>();
	}

	@Override
	public Set<UserDefinedAction> registerAction(UserDefinedAction action) {
		if (action.getActivation().getGlobalActivation().isOnKeyPressed()) {
			onKeyStrokePressedTasks.add(action);
		}
		if (action.getActivation().getGlobalActivation().isOnKeyReleased()) {
			onKeyReleasedTasks.add(action);
		}
		return new HashSet<>();
	}

	@Override
	public Set<UserDefinedAction> unRegisterAction(UserDefinedAction action) {
		Set<UserDefinedAction> removed = new HashSet<>();
		for (Iterator<UserDefinedAction> it = onKeyStrokePressedTasks.iterator(); it.hasNext();) {
			UserDefinedAction pressed = it.next();
			if (pressed.equals(action)) {
				removed.add(pressed);
				it.remove();
			}
		}
		for (Iterator<UserDefinedAction> it = onKeyReleasedTasks.iterator(); it.hasNext();) {
			UserDefinedAction released = it.next();
			if (released.equals(action)) {
				removed.add(released);
				it.remove();
			}
		}
		return removed;
	}

}

@@ -26,16 +26,41 @@
 agers;%0A%0A
+import java.util.Arrays;%0A
 import j
@@ -183,16 +183,47 @@
 Config;%0A
+import core.keyChain.KeyChain;%0A
 import c
@@ -813,140 +813,481 @@
 %7B%0A%09%09
-return new HashSet%3C%3E(onKeyStrokePressedTasks);%0A%09%7D%0A%0A%09@Override%0A%09public Set%3CUserDefinedAction%3E onKeyStrokeReleased(KeyStroke stroke) %7B
+for (UserDefinedAction action : onKeyStrokePressedTasks) %7B%0A%09%09%09action.setInvoker(TaskActivation.newBuilder().withHotKey(new KeyChain(Arrays.asList(stroke))).build());%0A%09%09%7D%0A%0A%09%09return new HashSet%3C%3E(onKeyStrokePressedTasks);%0A%09%7D%0A%0A%09@Override%0A%09public Set%3CUserDefinedAction%3E onKeyStrokeReleased(KeyStroke stroke) %7B%0A%09%09for (UserDefinedAction action : onKeyReleasedTasks) %7B%0A%09%09%09action.setInvoker(TaskActivation.newBuilder().withHotKey(new KeyChain(Arrays.asList(stroke))).build());%0A%09%09%7D%0A
 %0A%09%09r

Add invoker for global activation task.