package zero1hd.rhythmbullet.entity.coordinator;

import com.badlogic.gdx.utils.Pool;

import zero1hd.rhythmbullet.entity.EntityManager;

public class CoordinatorFrame<T extends Coordinator> {
	private Pool<T> pool;
	private EntityManager em;
	CoordinatorFrame<T> cf;
	Class<T> coordinatorType;
	
	/**
	 * Similar to an entityFrame, however this time, for a coordinator.
	 * @param entityManager
	 * @param classtype
	 */
	public CoordinatorFrame(EntityManager entityManager, Class<T> classtype) {
		this.em = entityManager;
		coordinatorType = classtype;
		cf = this;
		pool = new Pool<T>() {
			@Override
			protected T newObject() {
				try {
					T coordinator = coordinatorType.newInstance();
					coordinator.setup(em, cf);
					return coordinator;
				} catch (InstantiationException | IllegalAccessException e) {
					e.printStackTrace();
					return null;
				}
			}
		};
	}
	
	public T buildCoordinator() {
		T coordinator = pool.obtain();
		return coordinator;
	}
	
	protected void recycleCoordinator(Coordinator coordinator) {
		pool.free(coordinatorType.cast(coordinator));
	}
	
	@Override
	public String toString() {
		return coordinatorType.getSimpleName();
	}
}
