package com.mindoo.ec09.example5.ca.top;

/*
 * Licensed Materials - Property of IBM
 * 
 * Lisence: 5724-E62 
 * 
 * (C) Copyright IBM Corp. 2007. All rights reserved.
 * 
 * IBM, the IBM logo, Lotus and Notes are trademarks of IBM Corporation
 * in the United States, other coutnries, or both. Other company,
 * product and service names may be trademarks or service marks
 * of others.
 * 
 * This program is licensed under the terms of the license agreement
 * accompanying the Program. This liscence agreement may be either 
 * located in a Program directory folder orlibrary identified as 
 * "License" or "Non IBM License", if applicable. Please read this 
 * agreement carefully before using the Program. By using the Program
 * you agree to these terms. 
 */

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import com.ibm.rcp.topology.ComponentData;
import com.ibm.rcp.topology.Navigation;
import com.ibm.rcp.topology.Page;
import com.ibm.rcp.topology.TopologyHandler;
import com.mindoo.ec09.example5.ca.DebugUtils;
import com.mindoo.ec09.example5.ca.PCSBean;
import com.mindoo.ec09.example5.ca.pb.PBHandler;

public class TopologyHelper 
{
	public static TopologyHandler getTopologyHandler(BundleContext context)
	{
		ServiceReference ref = context.getServiceReference(TopologyHandler.class.getName( ));
		if (ref == null)
			return null;
		TopologyHandler handler = (TopologyHandler )context.getService(ref);
		return handler;
	}
	
	public static ComponentData getComponentData(BundleContext context, String secondaryID)
	{
		ServiceReference ref = context.getServiceReference(TopologyHandler.class.getName( ));
		if (ref == null)
			return null;
		TopologyHandler handler = (TopologyHandler )context.getService(ref);
		if (handler == null)
			return null;
		ComponentData data = handler.getComponentData(secondaryID);
		return data;
	}
	
	public static boolean isDataReady(BundleContext context, String secondaryID)
	{
		ComponentData data = getComponentData(context, secondaryID);
		if (data == null)
			return false;
		return true;
	}
	
	public static boolean isGlobalToolbarHidden(BundleContext context, PCSBean bean, String secondaryID) {
		ComponentData data = getComponentData(context, secondaryID);
		if (data == null)
			return false;
		String[] hideGlobalToolbar=data.getPreference("HideGlobalToolbar");
		if ((hideGlobalToolbar != null) && (hideGlobalToolbar.length > 0)) {
			return Boolean.valueOf(hideGlobalToolbar[0]);
		}
		return false;
	}
	
	public static boolean isVCToolbarHidden(BundleContext context, PCSBean bean, String secondaryID) {
		ComponentData data = getComponentData(context, secondaryID);
		if (data == null)
			return false;
		String[] hideVCToolbar=data.getPreference("HideViewComponentToolbar");
		if ((hideVCToolbar != null) && (hideVCToolbar.length > 0)) {
			return Boolean.valueOf(hideVCToolbar[0]);
		}
		return false;
	}

	public static void initialize(BundleContext context, PCSBean bean, String secondaryID)
	{
		ComponentData data = getComponentData(context, secondaryID);
		if (data == null)
			return;
		String[] keys = data.getPreferenceKeys();
		for (int i = 0; i < keys.length; i++)
		{
			String key = keys[i];
			if (key.indexOf('.') >= 0)
				continue;				
			String[] vals = data.getPreference(key);
			if ((vals != null) && (vals.length > 0))
			{
				DebugUtils.println("TopologyHelper:"+secondaryID+"."+key+"="+vals[0]);
				PBHandler.setValue(bean, "set"+key, vals[0]);
			}
		}
	}
	
	public static String getSetting(BundleContext context, String secondaryID, String key, String def)
	{
		ComponentData data = getComponentData(context, secondaryID);
		if (data == null)
			return def;
		String[] keys = getPreference(data, key);
		if (keys.length == 0)
			return def;
		if (keys.length == 1)
			return keys[0];
		StringBuffer sb = new StringBuffer(keys[0]);
		for (int i = 1; i < keys.length; i++)
		{
			sb.append(";");
			sb.append(keys[i]);
		}
		return sb.toString();
	}
	
	private static String[] getPreference(ComponentData data, String key)
	{
		String[] vals = data.getPreference(key);
		if (vals != null)
			return vals;
		String[] keys = data.getPreferenceKeys();
		for (int i = 0; i < keys.length; i++)
			if (keys[i].equalsIgnoreCase(key))
				return data.getPreference(keys[i]);
		return null;
	}
	
	public static Page findPage(BundleContext context, String applicationGUID, String viewID) {
		TopologyHandler handler = getTopologyHandler(context);
		if (handler == null)
			return null;
		
		return searchForPage(handler, handler.getApplicationNavigationTree(applicationGUID), viewID);
	}
	
    public static String getPageTitle(BundleContext context, String ourPerspective) 
    {
		TopologyHandler handler = getTopologyHandler(context);
		if (handler == null)
			return "";
    	String[] guids = handler.getApplicationGUIDs();
    	for (int i = 0; i < guids.length; i++)
    	{
    		Navigation nav = searchForPerspective(handler.getApplicationNavigationTree(guids[i]), ourPerspective);
			if (nav != null)
				return nav.getName();
    	}
    	return "";
	}
	
    private static Navigation searchForPerspective(Navigation nav, String ourPerspective) 
    {
		if (nav instanceof Page)
		{
			if (((Page)nav).getPerspective().equals(ourPerspective))
				return nav;
		}
		Navigation[] children = nav.getNavigation();
		if (children != null)
			for (int i = 0; i < children.length; i++)
			{
				Navigation ret = searchForPerspective(children[i], ourPerspective);
				if (ret != null)
					return ret;
			}
		return null;
	}
    
    private static Page searchForPage(TopologyHandler handler, Navigation nav, String viewID) {
    	if (nav==null)
    		return null;
    	
		if (nav instanceof Page) {
			for (String id:((Page)nav).getComponents()) {
				ComponentData cd = handler.getComponentData(id);
				if (cd!=null && viewID.equals(cd.getViewId()))
					return (Page)nav;
			}
		}
		
		Navigation[] children = nav.getNavigation();
		if (children != null)
			for (int i = 0; i < children.length; i++)
			{
				Page ret = searchForPage(handler,children[i],viewID);
				if (ret != null)
					return ret;
			}
		return null;
	}
}
