package com.mindoo.ec09.example5.ca;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.StringTokenizer;

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

import com.ibm.rcp.topology.ComponentData;
import com.ibm.rcp.topology.TopologyHandler;
import com.mindoo.ec09.example5.Activator;

public class DebugUtils {
	public static boolean	mDebug = true;
	public static File		mOutputFile = null;

	public static void println(String msg) {
		if (!mDebug)
			return;
		if (mOutputFile == null)
			System.out.println(escape(msg));
		else {
			try {
				FileWriter wtr = new FileWriter(mOutputFile, true);
				wtr.write(getTimePrefix()+" "+escape(msg));
				wtr.write("\n");
				wtr.close();
			} catch (IOException e) {
			}
		}
	}

	public static String escape(String msg) {
		StringBuffer sb = new StringBuffer();
		char[] c = msg.toCharArray();
		for (int i = 0; i < c.length; i++)
			if (c[i] < 0x10)
				sb.append("{0x0"+Integer.toHexString(c[i])+"}");
			else if ((c[i] < ' ') || (c[i] > '~'))
				sb.append("{0x"+Integer.toHexString(c[i])+"}");
			else
				sb.append(c[i]);
		return sb.toString();
	}

	public static String compressPackageName(String name) {
		StringBuffer ret = new StringBuffer();
		StringTokenizer st = new StringTokenizer(name, ".");
		int l = st.countTokens();
		for (int i = 0; i < l; i++) {
			String n = st.nextToken();
			if (i > 0)
				ret.append('.');
			if (i + 2 > l)
				ret.append(n);
			else
				ret.append(n.charAt(0));
		}
		if ((ret.length() > 56) && (ret.charAt(ret.length() - 40) =='_')) {
			ret.delete(ret.length() - 56, ret.length() - 4);
			ret.insert(ret.length() - 4, "...");
		}
		return ret.toString();
	}

	public static String getEntityProperty(String pageName, String entityID, String propName) {
		BundleContext context = Activator.getContext();
		ServiceReference ref = context.getServiceReference(TopologyHandler.class.getName( ));
		if (ref != null) {
			TopologyHandler handler = (TopologyHandler )context.getService(ref);
			int o = entityID.indexOf(':');
			entityID = entityID.substring(o + 1);
			ComponentData data = handler.getComponentData(entityID);
			if (data != null) {
				String[] val = data.getPreference("notesurl");
				if ((val != null) && (val.length > 0)) {
					o = val[0].lastIndexOf('?');
					if (o >= 0)
						val[0] = val[0].substring(0, o);
					o = val[0].lastIndexOf('/');
					if (o >= 0)
						val[0] = val[0].substring(o + 1);
					return pageName+val[0]+"."+propName;
				}
				val = data.getPreference("com.ibm.rcp.viewId");
				if ((val != null) && (val.length > 0)) {
					o = val[0].lastIndexOf('.');
					if (o >= 0)
						val[0] = val[0].substring(o + 1);
					return pageName+val[0]+"."+propName;
				}
				//DebugUtils.println("No viewID or notesurl!");
			}
			//else
			//DebugUtils.println("No component data for "+entityID+"!");
		}
		//else
		//DebugUtils.println("No topology handler reference!");
		return pageName+compressPackageName(entityID)+"."+propName;
	}

	private static String getTimePrefix() {
		Calendar now = Calendar.getInstance();
		int hh = now.get(Calendar.HOUR_OF_DAY);
		int mm = now.get(Calendar.MINUTE);
		int ss = now.get(Calendar.SECOND);
		StringBuffer sb = new StringBuffer();
		sb.append(hh%12);
		sb.append(":");
		if (mm < 10)
			sb.append("0");
		sb.append(mm);
		sb.append(":");
		if (ss < 10)
			sb.append("0");
		sb.append(ss);
		if (hh < 12)
			sb.append(" AM");
		else
			sb.append(" PM");
		return sb.toString();
	}
}
