//
//  PostHelper.java
//  blogwt
//
//  Created by William Shakour (billy1380) on 30 May 2015.
//  Copyright © 2015 WillShex Limited. All rights reserved.
//
package com.willshex.blogwt.client.helper;

import java.util.List;
import java.util.Map;

import org.markdown4j.Plugin;
import org.markdown4j.client.IncludePlugin;
import org.markdown4j.client.MarkdownProcessor;
import org.markdown4j.client.event.PluginContentReadyEventHandler;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.willshex.blogwt.client.markdown.Processor;
import com.willshex.blogwt.client.markdown.plugin.FormPlugin;
import com.willshex.blogwt.client.markdown.plugin.MapPlugin;
import com.willshex.blogwt.client.markdown.plugin.part.FormPart;

/**
 * @author William Shakour (billy1380)
 *
 */
public class PostHelper extends com.willshex.blogwt.shared.helper.PostHelper {

	private static MarkdownProcessor processor;
	private static final PluginContentReadyEventHandler DEFAULT_PLUGIN_READY_HANDLER = new PluginContentReadyEventHandler() {

		@Override
		public void ready (PluginContentReadyEvent event, Plugin plugin,
				List<String> lines, Map<String, String> params, String id,
				String content) {
			Element el = Document.get().getElementById(id);

			if (plugin instanceof IncludePlugin) {
				if (el != null && content != null) {
					el.setInnerHTML(content);
				}
			} else if (plugin instanceof MapPlugin) {

				if (el != null) {
					MapHelper.showMap(el, lines, params);
				}
			} else if (plugin instanceof FormPlugin) {
				if (el != null && content != null) {
					Composite form = new FormPart();
					el.removeAllChildren();
					el.appendChild(form.getElement());
				}
			}
		}
	};

	private static MarkdownProcessor processor () {
		if (processor == null) {
			processor = new Processor();
		}

		return processor;
	}

	public static String makeMarkup (String value) {
		return processor().process(value);
	}

	public static String makeHeading (String value) {
		if (!value.startsWith("#")) {
			value = "#" + value;
		}

		return makeMarkup(value);
	}

	public static String makeHeading2 (String value) {
		if (!value.startsWith("##")) {
			value = "##" + value;
		}

		return makeMarkup(value);
	}

	/**
	 * @return
	 */
	public static HandlerRegistration handlePluginContentReady () {
		return processor().addPluginContentReadyHandler(
				DEFAULT_PLUGIN_READY_HANDLER);
	}

	public static void resetEmojiTheme () {
		((Processor) processor()).resetEmojiTheme();
	}

}

@@ -621,16 +621,64 @@
 posite;%0A
+import com.google.gwt.user.client.ui.RootPanel;%0A
 import c
@@ -1747,24 +1747,54 @@
  != null) %7B%0A
+%09%09%09%09%09el.removeAllChildren();%0A%0A
 %09%09%09%09%09Composi
@@ -1824,37 +1824,40 @@
 );%0A%09%09%09%09%09
-el.removeAllChildren(
+RootPanel.get().add(form
 );%0A%09%09%09%09%09

attach to root panel so the form gets gwt events