package com.sbs.jpm.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.FlowPanel;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.layout.VLayout;

public class TestCase implements EntryPoint {

	private Panel panel;

	public void onModuleLoad() {

		SC.showConsole();

		panel = new Panel();
		panel.setWidth100();
		panel.setHeight100();

		panel.draw();
		panel.paintSvg();
	}

	public class Panel extends VLayout {

		private FlowPanel svgWrapper;
		private SvgMouseManager svgMouseManager;

		public Panel() {

			svgWrapper = new FlowPanel();
			svgWrapper.getElement().setId("TestID");
			svgWrapper.setWidth("500px");
			svgWrapper.setHeight("500px");
			
			svgMouseManager = new SvgMouseManager();

			this.addMember(svgWrapper);
		}
		
		public void paintSvg() {
			paintSvgNative("TestID", getHeaderHtml()+getContent()+getFooterHtml());
		}

		private native void paintSvgNative(String parentId, String content) /*-{
			var parent = $doc.getElementById(parentId);
			if (parent !== null) {
				parent.innerHTML = content;
			}
		}-*/;

		private String getHeaderHtml() {
			return "<svg id\"demo\" width=\"500\" height=\"500\"" + svgMouseManager.getSvgMouseManagerString() + ">";
		}
		
		private String getContent() {
			return  "<text style=\"fill:black; stroke-width:1; font-size:11;\" x=\"100\" y=\"100\">Press me!</text>";
		}
		
		private String getFooterHtml() {
			return "</svg>";
		}
	}

	public class SvgMouseManager {

		public SvgMouseManager() {
			bindJsNative(this);
		}

		private native void bindJsNative(SvgMouseManager instance) /*-{

			$wnd.onSvgMouseDown = $entry(function(evt) {
				console.log("native mouse down .... ");
			});

			$wnd.onSvgMouseMove = $entry(function(evt) {
				console.log("native mouse move ....  ");
			});

			$wnd.onSvgMouseUp = $entry(function(evt) {
				console.log("native mouse up .... ");
			});
		}-*/;

		public String getSvgMouseManagerString() {
			return " onmousemove=\"onSvgMouseMove(evt)\"" + " onmousedown=\"onSvgMouseDown(evt)\""
					+ " onmouseup=\"onSvgMouseUp(evt)\"";
		}
	}
}
