package leigh.elements;

import leigh.elements.sdk.App;
import leigh.elements.sdk.Theme;

import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

/**
 * The central Elements class, which is responsible for managing one or more elements applications.
 *
 * @author Alex Leigh
 */
public class ElementsEngine {
    private final HashMap<UUID, App> apps = new HashMap<>();
    private final AppFactory factory;

    /**
     * Create a new elements engine with the given app factory.
     */
    public ElementsEngine(AppFactory factory) {
        this.factory = factory;
    }

    /**
     * Return the {@link App} associated with this ID, or <code>null</code> if there is no such app.
     */
    public App getApp(UUID appId, Theme theme) throws IOException, ClassNotFoundException {
        App app = apps.get(appId);
        if (app == null) {
            app = factory.loadApplication(appId, theme);
            apps.put(appId, app);
        }
        return apps.get(appId);
    }
}
