/*******************************************************************************
 * Copyright (C) 2017 Philipp Spohr
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 ******************************************************************************/
package de.hhu.ba.yoshikoWrapper;

import java.util.Properties;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.application.events.SetCurrentNetworkListener;
import org.cytoscape.application.swing.CySwingApplication;
import org.cytoscape.application.swing.CytoPanelComponent;
import org.cytoscape.model.CyNetworkFactory;
import org.cytoscape.model.CyNetworkManager;
import org.cytoscape.model.events.AddedEdgesListener;
import org.cytoscape.model.events.ColumnCreatedListener;
import org.cytoscape.model.events.ColumnDeletedListener;
import org.cytoscape.model.events.NetworkAddedListener;
import org.cytoscape.model.events.RemovedEdgesListener;
import org.cytoscape.model.subnetwork.CyRootNetworkManager;
import org.cytoscape.service.util.AbstractCyActivator;
import org.cytoscape.service.util.CyServiceRegistrar;
import org.cytoscape.session.events.SessionLoadedListener;
import org.cytoscape.task.visualize.ApplyVisualStyleTaskFactory;
import org.cytoscape.view.model.CyNetworkViewFactory;
import org.cytoscape.view.layout.CyLayoutAlgorithmManager;
import org.cytoscape.view.model.CyNetworkViewManager;
import org.cytoscape.view.presentation.RenderingEngineFactory;
import org.cytoscape.view.vizmap.VisualMappingFunctionFactory;
import org.cytoscape.view.vizmap.VisualMappingManager;
import org.cytoscape.view.vizmap.VisualStyleFactory;
import org.cytoscape.work.swing.DialogTaskManager;
import org.osgi.framework.BundleContext;

import de.hhu.ba.yoshikoWrapper.core.ConfigurationManager;
import de.hhu.ba.yoshikoWrapper.core.CyCore;
import de.hhu.ba.yoshikoWrapper.core.LocalizationManager;
import de.hhu.ba.yoshikoWrapper.core.NetChangeListener;
import de.hhu.ba.yoshikoWrapper.core.YoshikoLoader;
import de.hhu.ba.yoshikoWrapper.gui.MainPanel;

public class CyActivator extends AbstractCyActivator {

	public CyActivator() {
		super();
	}
		
	@Override
	public void start(BundleContext context) throws Exception {
		//Initialize cytoscape configuration system
		ConfigurationManager cm = new ConfigurationManager("yoshikoWrapper", "yoshiko.props");
		Properties propsReaderServiceProps = new Properties();
		propsReaderServiceProps.setProperty("cyPropertyName", "yoshiko.props");
		registerAllServices(context,cm,propsReaderServiceProps);
				
		//Create symbolic links to give other classes access
		CyCore.cy = getService(context, CyApplicationManager.class);	
		CyCore.dialogTaskManager = getService(context, DialogTaskManager.class);
		CyCore.registrar = getService(context, CyServiceRegistrar.class);
		CyCore.cm = cm;
		CyCore.networkViewFactory = getService(context, CyNetworkViewFactory.class);
		CyCore.networkFactory = getService(context, CyNetworkFactory.class);
		CyCore.swing = getService(context, CySwingApplication.class);
		CyCore.networkViewManager  = getService(context,CyNetworkViewManager.class);
		CyCore.networkManager = getService(context,CyNetworkManager.class);
		CyCore.layoutAlgorithmManager = getService(context,CyLayoutAlgorithmManager.class);
		CyCore.visualMappingManager = getService(context,VisualMappingManager.class);
		CyCore.visualStyleFactory = getService(context,VisualStyleFactory.class);
		CyCore.continuousMappingFactory = getService(context,VisualMappingFunctionFactory.class, "(mapping.type=continuous)");
		CyCore.rootNetworkManager = getService(context,CyRootNetworkManager.class);
		CyCore.applyVisualStyleTaskFactory = getService(context,ApplyVisualStyleTaskFactory.class);
		//TODO: Not sure how to infer type here
		CyCore.renderingEngineFactory = getService(context,RenderingEngineFactory.class);

		//Set language according to settings
		LocalizationManager.switchLanguage(cm.getProperties().getProperty("locale", "enUS"));

		//Attempt to find the yoshiko lib in r a previously stored location
		if (!YoshikoLoader.isLibraryLoaded()){
			String pathToYosh = cm.getProperties().getProperty("pathToYoshiko");
			if (pathToYosh != null && pathToYosh != "null") {
				YoshikoLoader.loadLibrary(pathToYosh);
			}
		}
		

		//main panel
		MainPanel mainPanel = new MainPanel();
		registerService(context,mainPanel,CytoPanelComponent.class, new Properties());
		
		//Listener for Network-Changes -> Updating possible columns for mapping
		NetChangeListener netChangeListener = new NetChangeListener(mainPanel.getColumnMapper());
		registerService(context,netChangeListener, NetworkAddedListener.class, new Properties());
		registerService(context,netChangeListener, AddedEdgesListener.class, new Properties());
		registerService(context,netChangeListener, RemovedEdgesListener.class, new Properties());
		registerService(context,netChangeListener, ColumnCreatedListener.class, new Properties());
		registerService(context,netChangeListener, ColumnDeletedListener.class, new Properties());
		registerService(context,netChangeListener, SessionLoadedListener.class, new Properties());
		registerService(context,netChangeListener, SetCurrentNetworkListener.class, new Properties());

	}

}