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

import java.util.Calendar;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.jvnet.lookout.core.util.Utilities;
import org.jvnet.lookout.google.web.preference.Preferences;
import org.jvnet.lookout.ui.core.preference.Cleanable;
import org.jvnet.lookout.ui.core.preference.ResourceTypeListEditor;
import org.jvnet.lookout.ui.google.web.Plugin;

public class WebPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage, Cleanable  {

	private static enum PredefinedDate 
	{OneDay, OneWeek, TwoWeeks, OneMonth, SixMonths, OneYear, TwoYears, FiveYears, TenYears, BeginingOfTime};
	
	private ResourceTypeListEditor resourceTypeListEditor;
	private BooleanFieldEditor filesSearchScope;
	
	public WebPreferencePage() {
		super(GRID);
		setPreferenceStore(Plugin.getDefault().getPreferenceStore());
		
		Plugin.getDefault().addCleanable(this);
		
		//CXB_EXTERNALIZE
		setDescription("Google Web Search Engine Preferences");
	}
	
	public void createFieldEditors() {
		
		StringFieldEditor webApiLicenseEditor = 
			new StringFieldEditor(
					Preferences.Key,
					//CXB_EXTERNALIZE
					"Web API &Key:", 
					getFieldEditorParent());
		
		addField(webApiLicenseEditor);		

		StringFieldEditor siteEditor = 
			new StringFieldEditor(
					Preferences.SiteToSearch,
					//CXB_EXTERNALIZE
					"&Site to search (only 1 site e.g www.eclipse.org):", 
					getFieldEditorParent());
		
		addField(siteEditor);
		
		RadioGroupFieldEditor partToSearchEditor =
			new RadioGroupFieldEditor(Preferences.PartOfResourceToSearch, 
					//CXB_EXTERNALIZE
					"Document &Part to search:", 5, 
					new String [][] {
					{"&Title:", Preferences.TitlePart},
					{"&Body:", Preferences.BodyPart},
					{"&URL:", Preferences.UrlPart},
					{"&Any:", Preferences.AnyPart}},
					getFieldEditorParent(),
					true
					); 
		addField(partToSearchEditor);	

		//FILE STUFF
		resourceTypeListEditor = 
			new ResourceTypeListEditor(
					Preferences.SearchableFileTypes,
					//CXB_EXTERNALIZE
					"Searchable File Types:", 
					getFieldEditorParent());
		addField(resourceTypeListEditor);
		
		filesSearchScope =
			new BooleanFieldEditor(Preferences.SearchAllFiles, 
					//CXB_EXTERNALIZE
					"Search All Files Types:",
					getFieldEditorParent()
					); 
		addField(filesSearchScope);

		//addSpacing();
		
		//Update initial state
		filesSearchScope.setPreferenceStore(getPreferenceStore());
		filesSearchScope.load();
		resourceTypeListEditor.setEnabled(!isSearchingForAllFiles(),getFieldEditorParent());		
		
		RadioGroupFieldEditor timeRangeEditor =
			new RadioGroupFieldEditor(Preferences.DateToSearchFrom, 
					//CXB_EXTERNALIZE
					"Time range to search:", 3, 
					new String [][] {
					{"1 &Day", getDate(PredefinedDate.OneDay)},
					{"1 &Week", getDate(PredefinedDate.OneWeek)},
					{"2 Week", getDate(PredefinedDate.TwoWeeks)},
					{"1 &Month", getDate(PredefinedDate.OneMonth)},
					{"6 Month", getDate(PredefinedDate.SixMonths)},
					{"1 &Year", getDate(PredefinedDate.OneYear)},
					{"2 Year", getDate(PredefinedDate.TwoYears)},
					{"5 Year", getDate(PredefinedDate.FiveYears)},
					{"10 Year", getDate(PredefinedDate.TenYears)},
					{"&Since Dinosaur's Age", getDate(PredefinedDate.BeginingOfTime)}},
					getFieldEditorParent(),
					true); 
		addField(timeRangeEditor);	
	}
	
	private String getDate(PredefinedDate date) {
		Calendar cal = Calendar.getInstance();
			
		switch (date) {
		case OneDay:
		{
			cal.add(Calendar.DAY_OF_YEAR,-1);	
			break;
		}
		case OneWeek:
		{
			cal.add(Calendar.WEEK_OF_YEAR,-1);	
			break;
		}
		case OneMonth:
		{
			cal.add(Calendar.MONTH,-1);	
			break;
		}
		case OneYear:
		{
			cal.add(Calendar.YEAR,-1);	
			break;
		}
		case TwoWeeks:
		{
			cal.add(Calendar.WEEK_OF_YEAR,-2);	
			break;
		}
		case TwoYears:
		{
			cal.add(Calendar.YEAR,-2);	
			break;
		}
		case FiveYears:
		{
			cal.add(Calendar.YEAR,-5);	
			break;
		}
		case SixMonths:
		{
			cal.add(Calendar.MONTH,-6);	
			break;
		}
		case TenYears:
		{
			cal.add(Calendar.YEAR,-10);
			break;
		}
		case BeginingOfTime:
		{
			cal.setTime(Utilities.getZeroDate());	
			break;
		}
		
		default:
			break;
		}
		
		return ""+Utilities.getJulianDate(cal.getTime());
	}
	
	@Override
	public boolean performOk() {
		
		boolean result = super.performOk();
		
		if(result) {
			adjustStateOfResourceTypeListEditor();
			//Save the plugin properties..
			org.jvnet.lookout.google.web.Plugin.getDefault().savePluginPreferences();
		}
		
		return result;
	}
	@Override
	protected void performDefaults() {
		super.performDefaults();
		adjustStateOfResourceTypeListEditor();
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent event) {
		String property = event.getProperty();
		Object source = event.getSource();
		
		if(FieldEditor.VALUE.equals(property)) {
			
			//Clicked on search files...
			if(filesSearchScope == source) {				
				adjustStateOfResourceTypeListEditor();
			} 
		}
	}

	private void adjustStateOfResourceTypeListEditor() {

		//Adjust type selector 
		resourceTypeListEditor.setEnabled(!isSearchingForAllFiles(), getFieldEditorParent());

	}
	private boolean isSearchingForAllFiles() {
		return filesSearchScope.getBooleanValue();
	}
	public void init(IWorkbench workbench) {
	}	

	public void clean() {		
    	getPreferenceStore().setToDefault(Preferences.SearchAllFiles);	
	}
}