/*
 * Copyright 2014-2015 See AUTHORS file.
 *
 * Licensed 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 com.kotcrab.vis.runtime.system;

import com.artemis.*;
import com.artemis.EntitySubscription.SubscriptionListener;
import com.artemis.annotations.Wire;
import com.artemis.utils.ImmutableBag;
import com.badlogic.gdx.utils.ObjectMap;
import com.kotcrab.vis.runtime.component.IDComponent;

/**
 * Allows to get entities by their string id that was set in VisEditor
 * @author Kotcrab
 */
@Wire
public class VisIDManager extends Manager {
	private ComponentMapper<IDComponent> idCm;
	private AspectSubscriptionManager subscriptionManager;

	private ObjectMap<String, Entity> idStore = new ObjectMap<String, Entity>();

	@Override
	protected void initialize () {
		EntitySubscription subscription = subscriptionManager.get(Aspect.all(IDComponent.class));

		subscription.addSubscriptionListener(new SubscriptionListener() {
			@Override
			public void inserted (ImmutableBag<Entity> entities) {
				ObjectMap<String, Entity> tmpCache = new ObjectMap<String, Entity>();

				for (Entity entity : entities) {
					tmpCache.put(idCm.get(entity).id, entity);
				}

				idStore.putAll(tmpCache);
			}

			@Override
			public void removed (ImmutableBag<Entity> entities) {
				for (Entity entity : entities) {
					idStore.remove(idCm.get(entity).id);
				}
			}
		});
	}

	public Entity get (String id) {
		return idStore.get(id);
	}
}

@@ -795,16 +795,53 @@
 bleBag;%0A
+import com.badlogic.gdx.utils.Array;%0A
 import c
@@ -1204,23 +1204,30 @@
 String, 
+Array%3C
 Entity%3E
+%3E
  idStore
@@ -1247,31 +1247,38 @@
 Map%3CString, 
+Array%3C
 Entity%3E
+%3E
 ();%0A%0A%09@Overr
@@ -1553,164 +1553,221 @@
 %09%09%09%09
-ObjectMap%3CString, Entity%3E tmpCache = new ObjectMap%3CString, E
+for (Entity entity : entities) %7B%0A%09%09%09%09%09String id = idCm.get(e
 ntity
-%3E()
+).id
 ;%0A%0A%09%09%09%09
-for (Entity entity : entities) %7B%0A%09%09%09%09%09tmpCache.put(idCm.get(entity).id, entity
+%09Array%3CEntity%3E idList = idStore.get(id);%0A%0A%09%09%09%09%09if (idList == null) %7B%0A%09%09%09%09%09%09idList = new Array%3CEntity%3E();%0A%09%09%09%09%09%09idStore.put(id, idList
 );%0A
+%09
 %09%09%09%09
@@ -1777,33 +1777,34 @@
 %09%09%09%09
+%09
 id
-Store.putAll(tmpCache);
+List.add(entity);%0A%09%09%09%09%7D
 %0A%09%09%09
@@ -1922,51 +1922,195 @@
 %09%09%09%09
-idStore.remove(idCm.get(entity).
+String id = idCm.get(entity).id;%0A%0A%09%09%09%09%09Array%3CEntity%3E idList = idStore.get(id);%0A%09%09%09%09%09idList.removeValue(entity, true);%0A%0A%09%09%09%09%09if (idList.size == 0) %7B%0A%09%09%09%09%09%09idStore.remove(
 id);%0A
+%09
 %09%09%09%09%7D%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D
@@ -2125,64 +2125,505 @@
 %7D%0A%0A%09
-public Entity get (String id) %7B%0A%09%09return idStore.get(id)
+/**%0A%09 * Returns entity for given ID. If multiple entities has the same id only the first one will be returned.%0A%09 * @see #getMultiple(String)%0A%09 */%0A%09public Entity get (String id) %7B%0A%09%09return getMultiple(id).get(0);%0A%09%7D%0A%0A%09/**%0A%09 * Returns all entities with this ID.%0A%09 * @see #get(String)%0A%09 */%0A%09public Array%3CEntity%3E getMultiple (String id) %7B%0A%09%09Array%3CEntity%3E entities = idStore.get(id);%0A%09%09if (entities == null) throw new IllegalStateException(%22Could not find any entity with ID: %22 + id);%0A%09%09return entities
 ;%0A%09%7D

Support multiple entities with same id