/**
 * $id:  $
 */
package org.jvnet.lookout.google.desktop.preference;

import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.jvnet.lookout.core.util.Utilities;
import org.jvnet.lookout.google.desktop.Plugin;

public class DesktopPreferenceInitializer extends AbstractPreferenceInitializer {
	
	private Preferences sourcePreferences;

	//REGISTRY
    //This command is assumed to be in windows machines where the 
    //Google Desktop Search is supported
    private static final String regCommand 
    = "reg query \"HKCU\\Software\\Google\\Google Desktop\\API\\\" /v search_url";
    private static final String httpProtocol = "http://";
    private static final String searchParameter = "search&s=";
    private static final String querySubParameter = "?q=";
	
	public DesktopPreferenceInitializer() {
		super();
		sourcePreferences = obtainSourcePreference();
	}

	protected Preferences obtainSourcePreference() {
		return Plugin.getDefault().getPluginPreferences();		
	}

	final protected Preferences getSourcePreference() {
		return sourcePreferences;
	}

	public void initializeDefaultPreferences() {
		
		////////////////////////////////////
		//What resource types to search...
		////////////////////////////////////
		getSourcePreference().setDefault
		(org.jvnet.lookout.google.desktop.preference.Preferences.ResourceTypeToSearch,
				org.jvnet.lookout.google.desktop.preference.Preferences.SearchAll);

		////////////////////////////////////
		//What file type to search...
		////////////////////////////////////
		getSourcePreference().setDefault
		(org.jvnet.lookout.google.desktop.preference.Preferences.FileTypeToSearch,
				"doc");

		////////////////////////////////////
		//Client specific

		String commandOutput = Utilities.executeSystemCommand(regCommand);

		////////////////////////////////////
		//Server URL...
		////////////////////////////////////
		getSourcePreference().setDefault
		(org.jvnet.lookout.google.desktop.preference.Preferences.ServerUrl,
				extractClientServerURL(commandOutput));

		////////////////////////////////////
		//Client security token...
		////////////////////////////////////
		getSourcePreference().setDefault
		(org.jvnet.lookout.google.desktop.preference.Preferences.ClientSecurityToken,
				extractClientSecurityToken(commandOutput));		
	}
	
	private String extractClientServerURL(String keyRawValue) {
		
		String value = null;
		
		int protocolIndex = keyRawValue.indexOf(httpProtocol);
		int securityKeyStartIndex = keyRawValue.indexOf(searchParameter);
		
		value = keyRawValue.substring(protocolIndex, securityKeyStartIndex);
		
		return value;
	}

	private String extractClientSecurityToken(String keyRawValue) {
		String value = null;
		
		int securityKeyStartIndex = keyRawValue.indexOf(searchParameter);
		int securityKeyEndIndex = keyRawValue.indexOf(querySubParameter);
		
		value = keyRawValue.substring(securityKeyStartIndex + searchParameter.length(), securityKeyEndIndex);
		
		return value;
	}
}
