package org.allmyinfo.meaning.impl.privateKey;

import org.allmyinfo.coordinator.Coordinator;
import org.allmyinfo.meaning.MeaningRegistry;
import org.allmyinfo.meaning.privateKey.PrivateKeyMeaning;
import org.allmyinfo.nature.privateKey.PrivateKeyNature;
import org.allmyinfo.nodeid.meaning.int1.Int1MeaningNodeIdType;
import org.allmyinfo.nodes.NodeId;
import org.allmyinfo.system.cp.SystemContextProvider;
import org.allmyinfo.util.osgi.activator.BaseActivator;
import org.allmyinfo.util.osgi.activator.RequiredService;
import org.eclipse.jdt.annotation.NonNull;
import org.osgi.framework.BundleContext;

public class Activator extends BaseActivator {
	public Activator() {
		final RequiredService mrRS = addRequiredService(MeaningRegistry.class.getName(), null);
		final RequiredService privateKeyNatureRS = addRequiredService(
			PrivateKeyNature.class.getName(), null);
		final RequiredService coordRS = addRequiredService(Coordinator.class.getName(), null);
		final RequiredService scRS = addRequiredService(SystemContextProvider.class.getName(), null);
		addListener(new Listener() {
			@Override
			public void startService(final @NonNull BundleContext context) {
				final MeaningRegistry meaningRegistry = (MeaningRegistry) mrRS.getInstance();
				final Coordinator coordinator = (Coordinator) coordRS.getInstance();
				final Coordinator.Reactor coordReactor = new Coordinator.Reactor(context,
						coordinator);
				final String meaningName = PrivateKeyMeaningImpl.NAME;
				final SystemContextProvider sc = (SystemContextProvider) scRS.getInstance();
				final NodeId meaningNodeId = sc.getMeaningNodeId(meaningName,
					Int1MeaningNodeIdType.INSTANCE);
				final PrivateKeyMeaning meaning = new PrivateKeyMeaningImpl(meaningNodeId, //
						new PrivateKeyNature.Reactor(context,
								(PrivateKeyNature) privateKeyNatureRS.getInstance()), coordReactor);
				meaningRegistry.add(meaningName, meaning);
				Activator.this.addRegistration(context.registerService(PrivateKeyMeaning.class,
					meaning, null));
			}

			@Override
			public void stopService(final @NonNull BundleContext context) {
			}

			@Override
			public String getName() {
				return Activator.class.getPackage().getName();
			}
		});
	}
}
