package org.gcube.portlets.user.reportgenerator.client.targets;

import org.gcube.portlets.d4sreporting.common.shared.ComponentType;
import org.gcube.portlets.user.reportgenerator.client.UUID;
import org.gcube.portlets.user.reportgenerator.client.Presenter.Presenter;

import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
/**
 * <code> BasicTextArea </code> is a resizable textArea
 *
 * @author Massimiliano Assante, ISTI-CNR - massimiliano.assante@isti.cnr.it
 * @version July 2013 
 */
public class BasicTextArea extends TextArea {
	private String myId;

	public BasicTextArea(ComponentType type, final Presenter presenter, int left, int top, final int width,  final int height, boolean hasComments, boolean showClose) {

		myId = UUID.uuid(16);
		this.setPixelSize(width, height);

		this.addMouseOutHandler(new MouseOutHandler() {

			public void onMouseOut(MouseOutEvent event) {
				presenter.storeChangeInSession((Widget) event.getSource());				
			}
		});

		this.setStyleName("report-ui-component");
		this.addStyleName("d4sFrame");
		this.getElement().setId(myId);
		
		switch (type) {

		case BODY_NOT_FORMATTED:
			this.addStyleName("simpleText");
			break;
		default:
			break;
		}		

		addMouseOutHandler(new MouseOutHandler() {
			public void onMouseOut(MouseOutEvent event) {
				presenter.storeChangeInSession((Widget) event.getSource());
			}
		});

		this.addKeyUpHandler(new KeyUpHandler() {
			public void onKeyUp(KeyUpEvent event) {
				//presenter.resizeTemplateComponentInModel(myInstance, width, newHeight);
			}			
		});

		addFocusHandler(new FocusHandler() {			
			@Override
			public void onFocus(FocusEvent event) {
				autoSizeIt(myId, height);				
			}
		});
	}


	public RichTextArea getRichTextArea() {
		return null;
	}

	/**
	 * This method actually makes resizable the textArea
	 * @param id
	 */
	public static native void autoSizeIt(String id, int height) /*-{
	    var text = $doc.getElementById(id);
     	var observe;
		if (window.attachEvent) {
	    	observe = function (element, event, handler) {
	        	element.attachEvent('on'+event, handler);
	    	};
		}
		else {
	    	observe = function (element, event, handler) {
	        	element.addEventListener(event, handler, false);
	    	};
		}

	    function resize () {
	        text.style.height = height+'px';
	        text.style.height = text.scrollHeight+'px';
	    }

	    function delayedResize () {
	        window.setTimeout(resize, 0);
	    }

	    observe(text, 'change',  resize);
	    observe(text, 'cut',     delayedResize);
	    observe(text, 'paste',   delayedResize);
	    observe(text, 'drop',    delayedResize);
	    observe(text, 'keydown', delayedResize);

	    text.focus();
	    text.select();
	    resize();
	}-*/;

}
