package com.smartgwt.sample.client.tinymce;

import com.google.gwt.core.client.JavaScriptObject;


/**
 * TinyMCE utility class
 */
public class TinyMCE {
	
	public static void initMCE() {
		init(mceInitInstanceCallback(), mceChangeCallback());
	}

	/**
	 * Unload an MCE editor instance from memory.
	 */
	protected static native void unload(String id) /*-{
		$wnd.tinyMCE.execCommand('mceRemoveControl', false, id);
	}-*/;

	/**
	 * Update the editor content
	 * 
	 * @param id The TinyMCE instance ID
	 * @param newContent The String of new content
	 */
	protected static native void updateContent(String id, String newContent) /*-{
		$wnd.tinyMCE.selectedInstance = $wnd.tinyMCE.getInstanceById(id);
		try {
			selectedInstance.setContent(newContent);
		} catch (e) {
		}
	}-*/;

	/**
	 * getTextArea() -
	 * 
	 */
	protected native void getTextData() /*-{
		$wnd.tinyMCE.triggerSave();
	}-*/;

	/**
	 * encodeURIComponent() -
	 * 
	 * Wrapper for the native URL encoding methods
	 * 
	 * @param text
	 *            - the text to encode
	 * @return the encoded text
	 */
	protected native String encodeURIComponent(String text) /*-{
		return encodeURIComponent(text);
	}-*/;

	private static native JavaScriptObject mceChangeCallback() /*-{
		return function (instance) { 
			var newValue = instance.getContent(); 
			@com.smartgwt.sample.client.tinymce.TinyMCECanvasItem::updateValue(Ljava/lang/String;Ljava/lang/String;)(instance.id,newValue);
		};
	}-*/;
	private static native JavaScriptObject mceInitInstanceCallback() /*-{
		return function (instance) { 
			var initValue = @com.smartgwt.sample.client.tinymce.TinyMCECanvasItem::getCurrentValue(Ljava/lang/String;)(instance.id);
//			alert("Setting instance "+instance.id+" to "+initValue); 
			instance.setContent(initValue);
		};
	}-*/;

	/**
	 * Initialize Tiny MCE editor
	 */
	protected static native void init(JavaScriptObject mceInitInstanceCallback, JavaScriptObject mceChangeCallback) /*-{
		$wnd.tinyMCE.init({
			onchange_callback : mceChangeCallback,
			init_instance_callback : mceInitInstanceCallback,
			width : "700px",
			height : "200px",
	        theme : "advanced",
	        inline_styles : false,
	        skin : "default",
	        mode : "none",
	        plugins : "lists,spellchecker,pagebreak,table,inlinepopups,preview,media,searchreplace,contextmenu,paste,fullscreen,nonbreaking",
	        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,bullist,numlist,separator,sup,sub,|,undo,redo,separator,cut,copy,paste,|,fontselect,fontsizeselect,forecolor,backcolor",
	        theme_advanced_buttons2 : "",
	        theme_advanced_buttons3 : "",
	        theme_advanced_toolbar_location : "top",
	        theme_advanced_toolbar_align : "left",
	        theme_advanced_styles : "Code=codeStyle;Quote=quoteStyle;PHP Code=phpCodeStyle",
	        entity_encoding : "raw",
	        add_unload_trigger : false,
	        remove_linebreaks : false,
	        button_tile_map : true,
	        theme_advanced_font_sizes: "8=8pt,10=10pt,12=12pt,14=14pt,18=18pt,24=24pt,36=36pt",
	        theme_advanced_fonts : "Andale Mono=andale mono,times;Arial=arial;Arial Black=arial black;Book Antiqua=book antiqua;Comic Sans MS=comic sans ms;Courier New=courier new;Impact=impact;Symbol=symbol;Tahoma=tahoma,arial;Terminal=terminal;Times New Roman=times new roman;Verdana=verdana;Webdings=webdings;Wingdings=wingdings",
	        font_size_style_values: "8,10,12,14,18,24,36",
	        formats : {
		        bold : {inline : 'strong'},
		        italic : {inline : 'em'},
		        underline : {inline : 'u'},
		        strikethrough : {inline : 's'}
			}	
		});
	}-*/;
	
	public static native void initInstance(String elementId) /*-{
		$wnd.tinyMCE.execCommand("mceAddControl", true, elementId);
    }-*/;
	
}
