/**
 * $id:  $
 */
package org.jvnet.lookout.ui.core.preference;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.jvnet.lookout.core.adapter.SearchAdapter;
import org.jvnet.lookout.core.preference.Preferences;
import org.jvnet.lookout.core.util.Utilities;
import org.jvnet.lookout.ui.core.Plugin;

public class CommonPreferenceInitializer extends SynchronizedPreferenceInitializer {

	public CommonPreferenceInitializer() {
		super();

		//Need to cleanup some things...
		Plugin.getDefault().addCleanable(this);
	}
	
	protected IPreferenceStore obtainPreferenceStore() {
		return Plugin.getDefault().getPreferenceStore();
	}
	
	protected org.eclipse.core.runtime.Preferences obtainSourcePreference() {
		return org.jvnet.lookout.core.Plugin.getDefault().getPluginPreferences();
	}
	
	public void actuallyInitializeDefaultPreferences() {
		/////////////////////////
		//SYNCHED PROPERTIES
		
		//Number of results....		
		synchPropertyWithSource(Preferences.NumberOfResults, Integer.class);
		
		/////////////////////////
		//UI PROPERTIES
		getPreferenceStore().setDefault(org.jvnet.lookout.ui.core.preference.Preferences.GroupingMode, 
				org.jvnet.lookout.ui.core.preference.Preferences.SearchEngineMode);
		
		/////////////////////////
		//Active Search Engines
		//DEFAULTS
		org.jvnet.lookout.core.Plugin coreSearchPlugin = org.jvnet.lookout.core.Plugin.getDefault();
		
		//Extract enabled search adapter info 		
		List<SearchAdapter> searchAdapters = coreSearchPlugin.getSearchAdapters();
		List<String> allIds = new ArrayList<String>(); 
		
		for (Iterator iter = searchAdapters.iterator(); iter.hasNext();) {
			SearchAdapter adapter = (SearchAdapter) iter.next();
			String id = coreSearchPlugin.getSearchAdapterId(adapter);
			
			allIds.add(id);
			
			//Save the default for the preference store..
			getPreferenceStore().setDefault(id, false);	
		}

		////////////////////////////////////////////////////////////
		//ACTUAL VALUE
		////////////////////////////////////////////////////////////
		
		String searchableAdaptersIds 
		= coreSearchPlugin.getPluginPreferences().getString(Preferences.SearchableSearchAdapters);

		List<String> activeIds = Utilities.createList(searchableAdaptersIds, ",");
		
		//Find the inactive IDs
		List<String> inactiveIds = new ArrayList<String>(allIds);
		inactiveIds.removeAll(activeIds);
			
		//De or activate them...
		for (int j = 0; j < allIds.size(); j++) {
			String id = allIds.get(j);
			getPreferenceStore().setValue(id, !inactiveIds.contains(id));			
		}
	}
	
	public void propertyChange(PropertyChangeEvent event) {
		String property = event.getProperty();
		if(org.jvnet.lookout.core.preference.Preferences.NumberOfResults.equals(property)) {
			//This is an integer...
			getSourcePreference().setValue(property, (Integer)event.getNewValue());			
		} 
	}
	
	public void clean() {
		//Need to remove this preference as it is used only to enable the 
		//use of PreferenceStores to create the UI...
		getPreferenceStore().setToDefault(Preferences.NumberOfResults);
		
		//Remove the autogened prefs...
		org.jvnet.lookout.core.Plugin coreSearchPlugin = org.jvnet.lookout.core.Plugin.getDefault();
		List<SearchAdapter> searchAdapters = coreSearchPlugin.getSearchAdapters();
		
		for (Iterator iter = searchAdapters.iterator(); iter.hasNext();) {
			SearchAdapter adapter = (SearchAdapter) iter.next();
			
			String adapterId = coreSearchPlugin.getSearchAdapterId(adapter);
			
			if(!getPreferenceStore().isDefault(adapterId)) {
				getPreferenceStore().setToDefault(adapterId);
			}		
		}  		
	}
}
