package org.rodyce.test.furnace;

import org.rodyce.test.furnace.services.HelloService;
import org.jboss.forge.furnace.Furnace;
import org.jboss.forge.furnace.addons.AddonId;
import org.jboss.forge.furnace.manager.AddonManager;
import org.jboss.forge.furnace.manager.impl.AddonManagerImpl;
import org.jboss.forge.furnace.manager.maven.addon.MavenAddonDependencyResolver;
import org.jboss.forge.furnace.manager.request.InstallRequest;
import org.jboss.forge.furnace.repositories.AddonRepositoryMode;
import org.jboss.forge.furnace.se.FurnaceFactory;
import org.jboss.forge.furnace.util.OperatingSystemUtils;

import java.util.Iterator;

/**
 * Created by rodyce on 9/30/14.
 */
public class LeApp {
    public static void main(String[] args) {
        System.out.println("Starting with working directory: " + OperatingSystemUtils.getWorkingDir());

        Furnace furnace = FurnaceFactory.getInstance();
        furnace.addRepository(AddonRepositoryMode.MUTABLE, OperatingSystemUtils.getWorkingDir());

        AddonManager manager = new AddonManagerImpl(furnace, new MavenAddonDependencyResolver());

        InstallRequest request = manager.install(AddonId.from("org.rodyce.test:modularity-test-hello-impl1", "0.1.0-SNAPSHOT"));
        request.perform();

        furnace.start();

        Iterator<HelloService> it = furnace.getAddonRegistry().getServices(HelloService.class).iterator();

        if (it.hasNext()) {
            int n = 1;
            while (it.hasNext()) {
                HelloService helloService = it.next();
                System.out.println(n + ".- Found implementation " + helloService.getClass().getName());
                System.out.println("Implementation ID: " + helloService.getImplId());

                helloService.sayHelloWorld();

            }
        } else {
            System.err.println("No service implementations found.");
        }
    }
}
