package org.reldb.dbrowser.ui.html;

import java.util.ArrayList;

import org.eclipse.swt.graphics.FontData;
import org.reldb.dbrowser.ui.preferences.PreferencePageCmd;
import org.reldb.dbrowser.ui.preferences.Preferences;

public class Style {
	
	private int sizeAdjustment;
	
	public Style(int sizeAdjustment) {
		this.sizeAdjustment = sizeAdjustment;
	}
	
	private static final String[] formattedStyle = {
		"body {margin-top: 0px; margin-left: 0px; margin-right: 0px; margin-bottom: 0px}\n",
		"table {border-style: none; border-width: 0px;}\n",
		"td, tr, th {border-style: solid; border-width: 1px;}\n",
		".ok {color: green;}\n",
	    ".bad {color: red;}\n",
	    ".note {color: blue;}\n",
	    ".user {color: gray;}\n",
	    ".warn {color: gold;}\n",
	    ".notice {color: black;}\n",
	};
	
	private String getBodyFontStyleString() {
		FontData[] data = Preferences.getPreferenceFont(PreferencePageCmd.CMD_FONT);
		FontData datum = data[0];
		// eliminate leading '.', if there is one
		String fontName = (datum.getName().startsWith(".") ? datum.getName().substring(1) : datum.getName());
		int fullSize = datum.getHeight() + sizeAdjustment;
		int smaller = (int)((double)fullSize * 0.75);
		return 
				"body, p, td, th {font-family: " + fontName + ", sans-serif; font-size: " + fullSize + "pt;}\n" +
				"small {font-family: " + fontName + ", sans-serif; font-size: " + smaller + "pt;}\n";
	}

	private String getHTMLStyle() {
		String out = "";
		for (String styleLine: formattedStyle)
			out += styleLine;
		out += getBodyFontStyleString();
		return out;
	}

	/** If window.status is prefixed with this string, it is almost certainly a selection obtained via JavaScript obtainSel() */
	public static String getSelectionIndicator() {
		return "f94kdjsfdjk3048slekf";
	}
	
	public String getHTMLDocument(String content) {
		return	 
			"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
			"<html xmlns=\"http://www.w3.org/1999/xhtml\" dir=\"ltr\" lang=\"en-gb\" xml:lang=\"en-gb\">\n" +
			"<script type=\"text/javascript\">" +
			"function selectAll() {" +
			"    document.execCommand(\"selectall\", null, false);" +
			"}" +
			"function getSelectionHtml() {" +
			"    var html = \"\";" +
			"    if (typeof window.getSelection != \"undefined\") {" +
			"        var sel = window.getSelection();" +
			"        if (sel.rangeCount) {" +
			"            var container = document.createElement(\"div\");" +
			"            for (var i = 0, len = sel.rangeCount; i < len; ++i) {" +
			"                container.appendChild(sel.getRangeAt(i).cloneContents());" +
			"            }" +
			"            html = container.innerHTML;" +
			"        }" +
			"    } else if (typeof document.selection != \"undefined\") {" +
			"        if (document.selection.type == \"Text\") {" +
			"            html = document.selection.createRange().htmlText;" +
			"        }" +
			"    }" +
			"    return html;" +
			"}" +
			"function obtainSel() {" +
			"    window.status = \"" + getSelectionIndicator() + "\" + getSelectionHtml();" +
			"}" +
			"</script>" +
			"<head>\n" +
			"<style type=\"text/css\">\n" +
			"<!--\n" +
			getHTMLStyle() +
			"-->\n" +
			"</style>\n" +
			"</head>\n" +
			"<body id=\"body\">\n" +
			content +
			"</body>\n" +
			"</html>";
	}
	
	public String getEmptyHTMLDocument() {
		return getHTMLDocument("");
	}
	
	public String[] getFormattedStyle() {
		ArrayList<String> style = new ArrayList<String>();
		style.add(getBodyFontStyleString());
		for (String styleLine: formattedStyle)
			style.add(styleLine);
		return style.toArray(new String[0]);
	}

}

@@ -260,96 +260,277 @@
 size
-Adjustment;%0A%09%0A%09public Style(int sizeAdjustment) %7B%0A%09%09this.sizeAdjustment = sizeAdjustment
+Offset;%0A%09private double sizeMultiplier = 1.0;%0A%09%0A%09public Style(int sizeOffset) %7B%0A%09%09this.sizeOffset = sizeOffset;%0A%09%09this.sizeMultiplier = 1;%0A%09%7D%0A%09%0A%09public Style(int sizeOffset, double sizeMultiplier) %7B%0A%09%09this.sizeOffset = sizeOffset;%0A%09%09this.sizeMultiplier = sizeMultiplier
 ;%0A%09%7D
@@ -1287,16 +1287,31 @@
 lSize = 
+(int)((double)(
 datum.ge
@@ -1330,18 +1330,33 @@
 size
-Adjustment
+Offset) * sizeMultiplier)
 ;%0A%09%09

Add font size scaling, in addition to offset.