package speiger.src.coreengine.rendering.inputOld.bindings.utils;

import java.util.Collections;
import java.util.Set;

import speiger.src.collections.ints.maps.impl.hash.Int2ObjectLinkedOpenHashMap;
import speiger.src.collections.ints.maps.interfaces.Int2ObjectMap;
import speiger.src.collections.objects.maps.impl.misc.Enum2ObjectMap;
import speiger.src.collections.objects.maps.interfaces.Object2ObjectMap;
import speiger.src.collections.objects.sets.ObjectLinkedOpenHashSet;
import speiger.src.coreengine.rendering.inputOld.bindings.InputBinding;

public class BindingRegistry
{
	Object2ObjectMap<BindingType, Int2ObjectMap<Set<InputBinding>>> handlers = new Enum2ObjectMap<>(BindingType.class);
	
	public void registerBinding(InputBinding binding) {
		handlers.supplyIfAbsent(binding.getInputType(), Int2ObjectLinkedOpenHashMap::new).supplyIfAbsent(binding.getKey(), ObjectLinkedOpenHashSet::new).add(binding);
	}
	
	public void removeBinding(InputBinding binding) {
		Int2ObjectMap<Set<InputBinding>> bindings = handlers.get(binding.getInputType());
		if(bindings == null) return;
		Set<InputBinding> entries = bindings.get(binding.getKey());
		if(entries.remove(binding) && entries.isEmpty()) {
			bindings.remove(binding.getKey());
			if(bindings.isEmpty()) handlers.remove(binding.getInputType());
		}
	}
	
	public void updatePressing(BindingType type, int key, boolean pressed) {
		Int2ObjectMap<Set<InputBinding>> bindings = handlers.get(type);
		if(bindings == null) return;
		for(InputBinding binding : bindings.getOrDefault(key, Collections.emptySet()))
			binding.setPressed(pressed);
	}
}
