package de.fe1k.game9.components;

import de.fe1k.game9.entities.Entity;
import de.fe1k.game9.exceptions.ComponentNotFoundException;


public abstract class Component {
	private Entity owner;

	/**
	 * @return the entity that owns this component.
	 */
	public Entity getOwner() {
		return owner;
	}

	/**
	 * Sets the entity that owns this component.
	 * Not part of constructor for all components to have a default constructor.
	 * @param owner the entity's owner
	 */
	public void setOwner(Entity owner) {
		this.owner = owner;
	}

	public boolean dependenciesSatisfied() {
		if (getOwner() == null) {
			return false;
		}
		Depends depends = getClass().getAnnotation(Depends.class);
		if (depends == null) {
			return true;
		}
		for (Class<? extends Component> componentClass : depends.components()) {
			if (!getOwner().hasComponent(componentClass)) {
				return false;
			}
		}
		return true;
	}

	public void destroy() {}

	////////////////// STATIC STUFF //////////////////

	/**
	 * Looks up a component class by name.
	 *
	 * @param name name of the component
	 * @return class of the component, or null if not found
	 */
	@SuppressWarnings("unchecked")
	public static <T extends Component> Class<T> getByName(String name) {
		Class clazz;
		try {
			clazz = Class.forName(Component.class.getPackage().getName() + "." + name);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new ComponentNotFoundException();
		}
		if (!Component.class.isAssignableFrom(clazz)) {
			throw new ComponentNotFoundException();
		}
		return (Class<T>) clazz;
	}

	@Override
	public boolean equals(Object o) {
		// generated by IntelliJ IDEA
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		Component component = (Component) o;

		return owner != null ? owner.equals(component.owner) : component.owner == null;
	}

	@Override
	public int hashCode() {
		// generated by IntelliJ IDEA
		return owner != null ? owner.hashCode() : 0;
	}
}

@@ -923,16 +923,499 @@
 y() %7B%7D%0A%0A
+%09public String getName() %7B%0A%09%09return getClass().getSimpleName();%0A%09%7D%0A%0A%09@Override%0A%09public boolean equals(Object o) %7B%0A%09%09// generated by IntelliJ IDEA%0A%09%09if (this == o) return true;%0A%09%09if (o == null %7C%7C getClass() != o.getClass()) return false;%0A%0A%09%09Component component = (Component) o;%0A%0A%09%09return owner != null ? owner.equals(component.owner) : component.owner == null;%0A%09%7D%0A%0A%09@Override%0A%09public int hashCode() %7B%0A%09%09// generated by IntelliJ IDEA%0A%09%09return owner != null ? owner.hashCode() : 0;%0A%09%7D%0A%0A
 %09///////
@@ -2053,422 +2053,7 @@
 ;%0A%09%7D
-%0A%0A%09@Override%0A%09public boolean equals(Object o) %7B%0A%09%09// generated by IntelliJ IDEA%0A%09%09if (this == o) return true;%0A%09%09if (o == null %7C%7C getClass() != o.getClass()) return false;%0A%0A%09%09Component component = (Component) o;%0A%0A%09%09return owner != null ? owner.equals(component.owner) : component.owner == null;%0A%09%7D%0A%0A%09@Override%0A%09public int hashCode() %7B%0A%09%09// generated by IntelliJ IDEA%0A%09%09return owner != null ? owner.hashCode() : 0;%0A%09%7D
 %0A%7D%0A

move some methods of Component above static stuff, add getName