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

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

import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
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.Event;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;


/**
 * <code> D4sRichTextArea </code> class is a template component that goes into the workspace
 *
 * @author Massimiliano Assante, ISTI-CNR - massimiliano.assante@isti.cnr.it
 * @version October 2008 (0.2) 
 */
public class D4sRichTextarea extends ReportTextArea {

	/**
	 * the text area 
	 */
	private RichTextArea area = new RichTextArea();
	/**
	 * a flag
	 */
	boolean firstClick = true;

	int currHeight = 0;

	VerticalPanel myPanel;
	/**
	 * 
	 *
	 */
	public D4sRichTextarea() {}

	private TextTableImage owner;

	/**
	 * 
	 * @param presenter .
	 * @param left left
	 * @param top top 
	 * @param width .
	 * @param height . 
	 * @param type .
	 */
	public D4sRichTextarea(ComponentType type, final Presenter presenter, int left, int top, final int width,  
			final int height, boolean hasComments, boolean showClose, TextTableImage owner) {
		super(type, presenter, left, top, width, height, hasComments, showClose);
		this.owner = owner;
		currHeight = height;
		myPanel = getResizablePanel();

		area.setPixelSize(width-6, height-2);
		area.setStyleName("d4sRichTextArea");

		switch (type) {
		case BODY:
			area.addStyleName("bodyArea");
			break;				
		default:
			break;
		}


		myPanel.add(area);
		//repositionMyPanel(0, 15);
		myPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
		myPanel.setPixelSize(width, height-15);
		setStyleName("d4sFrame");

		area.addMouseDownHandler(new MouseDownHandler() {
			public void onMouseDown(MouseDownEvent event) {
				if (event.getNativeButton() == Event.BUTTON_RIGHT) {
					event.preventDefault();
					event.stopPropagation();
					MessageBox.alert("Warning","Please, use CTRL+V (CMD+V) for pasting into this area", null);
				}
				if (firstClick) {
					presenter.enableTextToolBar(area);
					presenter.enableBiblioEntry(area);
					firstClick = false;
					ReportGenerator.get().getDivHidden().setPixelSize(width, -1);
				}		
				HTML div = ReportGenerator.get().getDivHidden();
				div.setHTML(area.getHTML());
				int newHeight = div.getOffsetHeight()+20 ;
				if (newHeight > height-10 && newHeight != currHeight) {
					presenter.shiftComponentsByTextArea(myInstance.top, newHeight - currHeight);
					resizePanel(width, div.getOffsetHeight());
					//Window.alert("myInstance.top: " + myInstance.top);
					presenter.resizeTemplateComponentInModel(myInstance, width, newHeight);				
				}
			}			
		});

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

		});

		area.addKeyUpHandler(new KeyUpHandler() {
			public void onKeyUp(KeyUpEvent event) {
				HTML div = ReportGenerator.get().getDivHidden();
				div.setHTML(area.getHTML());
				int newHeight = div.getOffsetHeight()+20 ;
				if (newHeight > height-10 && newHeight != currHeight) {
					presenter.shiftComponentsByTextArea(myInstance.top, newHeight - currHeight);
					resizePanel(myPanel.getOffsetWidth(), div.getOffsetHeight());
					//Window.alert("myInstance.top: " + myInstance.top);
					presenter.resizeTemplateComponentInModel(myInstance, myPanel.getOffsetWidth()+5, newHeight);
				}	
			}			
		});
	}

	/**
	 * used to resize the panel
	 * @param width w
	 * @param height h
	 */
	@Override
	public void resizePanel(int width, int height) {
		if (height > 15 && width > 15) {
			mainPanel.setPixelSize(width, height+20);			
			resizablePanel.setPixelSize(width, height+20);
			mainPanel.setWidgetPosition(topPanel, width-30 , 0);
			area.setPixelSize(width, height+20);
			currHeight = height + 20;
		}
	}

	/**
	 * 
	 * @return .
	 */
	public String getHTML() {
		return area.getHTML();
	}

	/**
	 * @param html the html
	 */
	public void setHTML(String html) {
		area.setHTML(html);
	}
	/**
	 * 
	 * @return .
	 */
	public RichTextArea getArea() {
		return area;
	}

	/**
	 * 
	 * @param firstClick .
	 */
	public void setFirstClick(boolean firstClick) {
		this.firstClick = firstClick;
	}

	@Override
	public void removeTemplateComponent() {
		if (owner != null) {
			owner.removeFromParent(this);
			removeFromParent();	
		}
	}
}
