package com.quantum.editors;

import java.util.HashMap;

import com.quantum.PluginPreferences;
import com.quantum.QuantumPlugin;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;

public class SQLSourceViewerConfiguration extends SourceViewerConfiguration {
	private PresentationReconciler reconciler = new PresentationReconciler();
	private ColorManager colorManager;
	private HashMap cache = new HashMap();
	private boolean textBold = false;
	private boolean keywordBold = true;
	private boolean stringBold = false;
	private boolean commentBold = false;
	private boolean numericBold = false;
	public SQLSourceViewerConfiguration(ColorManager colorManager) {
		this.colorManager = colorManager;
	}
	public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
		return new String[] {
			IDocument.DEFAULT_CONTENT_TYPE,
			SQLPartitionScanner.SQL_COMMENT,
			SQLPartitionScanner.SQL_KEYWORD,
			SQLPartitionScanner.SQL_IDENTIFIER};
	}

	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		initializeColors();
		return reconciler;
	}
	public void loadPrefs() {
		IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
		textBold = store.getBoolean("quantum.text.bold"); //$NON-NLS-1$
		keywordBold = store.getBoolean("quantum.keyword.bold"); //$NON-NLS-1$
		stringBold = store.getBoolean("quantum.string.bold"); //$NON-NLS-1$
		commentBold = store.getBoolean("quantum.comment.bold"); //$NON-NLS-1$
		numericBold = store.getBoolean("quantum.numeric.bold"); //$NON-NLS-1$
		SQLColorConstants.BACKGROUND = PreferenceConverter.getColor(store, PluginPreferences.BACKGROUND_COLOR); //$NON-NLS-1$
		SQLColorConstants.DEFAULT = PreferenceConverter.getColor(store, PluginPreferences.TEXT_COLOR); //$NON-NLS-1$
		SQLColorConstants.IDENTIFIER = PreferenceConverter.getColor(store, PluginPreferences.TEXT_COLOR); //$NON-NLS-1$
		SQLColorConstants.KEYWORD = PreferenceConverter.getColor(store, PluginPreferences.KEYWORD_COLOR); //$NON-NLS-1$
		SQLColorConstants.STRING = PreferenceConverter.getColor(store, PluginPreferences.STRING_COLOR); //$NON-NLS-1$
		SQLColorConstants.COMMENT = PreferenceConverter.getColor(store, PluginPreferences.COMMENT_COLOR); //$NON-NLS-1$
		SQLColorConstants.NUMERIC = PreferenceConverter.getColor(store, PluginPreferences.NUMERIC_COLOR); //$NON-NLS-1$
	}
	public void initializeColors() {
		setDamageRepairer(getAttr(SQLColorConstants.KEYWORD, keywordBold), SQLPartitionScanner.SQL_KEYWORD);
		setDamageRepairer(getAttr(SQLColorConstants.COMMENT, commentBold), SQLPartitionScanner.SQL_COMMENT);
		setDamageRepairer(getAttr(SQLColorConstants.STRING, stringBold), SQLPartitionScanner.SQL_STRING);
		setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), IDocument.DEFAULT_CONTENT_TYPE);
		setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), SQLPartitionScanner.SQL_SYMBOL);
		setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), SQLPartitionScanner.SQL_IDENTIFIER);
		setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), SQLPartitionScanner.SQL_SEPARATOR);
		setDamageRepairer(getAttr(SQLColorConstants.NUMERIC, numericBold), SQLPartitionScanner.SQL_NUMERIC);
	}
	public TextAttribute getAttr(RGB color, boolean bold) {
		colorManager.getColor(SQLColorConstants.BACKGROUND);
		Color foreground = colorManager.getColor(color);
		TextAttribute attr = new TextAttribute(foreground);
		if (bold) {
			return new TextAttribute(foreground, attr.getBackground(), SWT.BOLD);
		}
		return attr;
	}
	public void setDamageRepairer(TextAttribute attr, String token) {
		NonRuleBasedDamagerRepairer ndr = (NonRuleBasedDamagerRepairer) cache.get(token);
		if (ndr == null) {
			ndr =
				new NonRuleBasedDamagerRepairer(attr);
			reconciler.setDamager(ndr, token);
			reconciler.setRepairer(ndr, token);
			cache.put(token, ndr);
		} else {
			ndr.setTextAttribute(attr);
		}
	}
//	public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
//		ContentAssistant assistant = new ContentAssistant();
//		assistant.setContentAssistProcessor(new SQLContentAssistProcessor("default"), 
//				IDocument.DEFAULT_CONTENT_TYPE);
////		assistant.setContentAssistProcessor(new SQLContentAssistProcessor("comment"), 
////				SQLPartitionScanner.SQL_COMMENT);
////		assistant.setContentAssistProcessor(new SQLContentAssistProcessor("keyword"), 
////				SQLPartitionScanner.SQL_KEYWORD);
////		assistant.setContentAssistProcessor(new SQLContentAssistProcessor("identifier"), 
////				SQLPartitionScanner.SQL_IDENTIFIER);
//		
//		// everybody else is doin' it...
//		assistant.enableAutoActivation(true);
//		assistant.setAutoActivationDelay(500);
//		
//		assistant.setProposalPopupOrientation(ContentAssistant.CONTEXT_INFO_BELOW);
//		assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_BELOW);
//		return assistant;
//	}
}