package com.imnotjames.tribbleengine.entity;

import com.imnotjames.tribbleengine.Family;

import java.util.ArrayList;
import java.util.List;

public class Entity {
	private static int singletonIndex = 0;

	private int index;

	private List<Component> components;

	private Family family;

	public Entity() {
		this.index = Entity.singletonIndex++;

		this.family = new Family();
		this.components = new ArrayList<Component>();
	}

	public int getIndex() {
		return this.index;
	}

	public Family getFamily() {
		return this.family;
	}

	public void addComponent(Component component) {
		if (component == null) {
			return;
		}

		if (this.hasComponent(component)) {
			throw new IllegalArgumentException("Entity already has component of type " + component.getClass());
		}

		this.components.add(component);

		this.family.addComponentType(component.getClass());
	}

	public Component[] getComponents() {
		return this.components.toArray(new Component[this.components.size()]);
	}

	public Component getComponent(Class<? extends Component> componentType) {
		for (Component component : this.components) {
			if (component.getClass() == componentType) {
				return component;
			}
		}

		return null;
	}

	public boolean hasComponent(Component component) {
		return this.hasComponent(component.getClass());
	}

	public boolean hasComponent(Class<? extends Component> componentType) {
		return this.family.hasComponentType(componentType);
	}

	public boolean removeComponent(Component component) {
		return this.removeComponent(component.getClass());
	}

	public boolean removeComponent(Class<? extends Component> componentType) {
		for (Component component : this.components) {
			if (component.getClass().equals(componentType)) {
				this.components.remove(component);
				this.family.removeComponentType(componentType);
				return true;
			}
		}

		return false;
	}

	public void removeComponents() {
		this.family.removeComponentTypes();
		this.components.clear();
	}
}

@@ -976,32 +976,43 @@
 %5D);%0A%09%7D%0A%0A%09public 
+%3CT extends 
 Component getCom
@@ -1004,16 +1004,19 @@
 omponent
+%3E T
  getComp
@@ -1027,35 +1027,17 @@
 t(Class%3C
-? extends Component
+T
 %3E compon
@@ -1122,20 +1122,24 @@
 tClass()
- == 
+.equals(
 componen
@@ -1140,24 +1140,25 @@
 mponentType)
+)
  %7B%0A%09%09%09%09retur
@@ -1168,16 +1168,36 @@
 omponent
+Type.cast(component)
 ;%0A%09%09%09%7D%0A%09

use generics for Entity.getComponent()