package electrosphere.engine.service;

import java.util.LinkedList;
import java.util.List;

import electrosphere.logger.LoggerInterface;

/**
 * Manages all services
 */
public class ServiceManager {
    
    /**
     * The services that this manager created
     */
    public List<Service> trackedServices;

    /**
     * Initializes the service manager
     */
    public void init(){
        LoggerInterface.loggerEngine.DEBUG("[ServiceManager] Init");
        trackedServices = new LinkedList<Service>();
    }

    /**
     * Registers a type of service to be created
     * @param serviceClass The class of the service
     * @return The service
     */
    public Service registerService(Service service){
        LoggerInterface.loggerEngine.DEBUG("[ServiceManager] Register service " + service.getName());
        trackedServices.add(service);
        return service;
    }

    /**
     * Instantiates all registered service types
     */
    public void instantiate(){
        for(Service service : trackedServices){
            LoggerInterface.loggerEngine.DEBUG("[ServiceManager] Instantiate service " + service.getName());
            service.init();
        }
    }

    /**
     * Destroys the service manager and all tracked services
     */
    public void destroy(){
        for(Service service : trackedServices){
            LoggerInterface.loggerEngine.DEBUG("[ServiceManager] Destroy service " + service.getName());
            service.destroy();
        }
        this.trackedServices = null;
    }

}
