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

import org.gcube.portlets.widgets.guidedtour.client.GCUBEGuidedTour;
import org.gcube.portlets.widgets.guidedtour.client.steps.GCUBETemplate1Text1Image;
import org.gcube.portlets.widgets.guidedtour.client.steps.GCUBETemplate2Text2Image;
import org.gcube.portlets.widgets.guidedtour.client.steps.TourStep;
import org.gcube.portlets.widgets.guidedtour.client.types.VerticalAlignment;
import org.gcube.portlets.user.reportgenerator.client.Presenter.Presenter;
import org.gcube.portlets.user.reportgenerator.client.toursteps.Intro;
import org.gcube.portlets.user.workspace.client.AppControllerExplorer;
import org.gcube.portlets.user.workspace.client.rpc.GWTWorkspaceServiceAsync;
import org.gcube.portlets.widgets.lighttree.client.ItemType;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CellPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;



/**
 * Entry point classes define <code>onModuleLoad()</code>.
 * 
 * 
 * <code> ReportGeneretor </code> class is the Entry point class, defines the main layout of the UI
 *
 * @author Massimiliano Assante, ISTI-CNR - massimiliano.assante@isti.cnr.it

 */
public class ReportGenerator implements EntryPoint {

	/**
	 * 
	 */
	public static final int HEADER_HEIGHT = 25;
	/**
	 * 
	 */
	public static final int PAGE_LEFT = 5;
	/**
	 * 
	 */
	public static final int TOOLBOX_LEFT = 15;
	/**
	 * 
	 */
	public static final int TEMPLATE_LEFT = 230;
	/**
	 * 
	 */
	public static final int TEMPLATE_TOP = HEADER_HEIGHT + 60;
	/**
	 * 
	 */	
	public static ReportGenerator singleton = null;
	/**
	 * 
	 * @return .
	 */
	public static ReportGenerator get() {
		return singleton;
	}
	/**
	 * the controller
	 */
	private Presenter presenter;

	private VerticalPanel mainLayout = new VerticalPanel();

	private ToolboxPanel toolBoxPanel;

	private Headerbar header;

	private TitleBar titlebar;

	private HorizontalPanel exportResultsPanel = new HorizontalPanel();
	
	private WorkspacePanel workSpacePanel;

	private VerticalPanel eastPanel = new VerticalPanel();

	private VerticalPanel toolbarPanel = new VerticalPanel();

	private ScrollPanel bottomScrollerPanel = new ScrollPanel();
	
	private AppControllerExplorer treeController;

	private HTML divHidden = new HTML(); 
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
				singleton = this;
				presenter = new Presenter();
				treeController = new AppControllerExplorer();
				mainLayout.setWidth("100%");
				mainLayout.setHeight("100%");
				//
				workSpacePanel = new WorkspacePanel(presenter);
				titlebar = new TitleBar(presenter);
				header = new Headerbar(presenter);
				toolBoxPanel = new ToolboxPanel(treeController);
		
				presenter.setHeader(header);
				presenter.setTitleBar(titlebar);
				presenter.setWp(workSpacePanel);
				presenter.setToolBoxPanel(toolBoxPanel);
				presenter.setExportsPanel(exportResultsPanel);
		
				mainLayout.add(titlebar);
				mainLayout.add(header);
				mainLayout.add(toolbarPanel);
		
				toolbarPanel.setWidth("100%");
				toolbarPanel.setHeight("40");
				
				CellPanel cellPanel = new HorizontalPanel();
				cellPanel.setStyleName("cella");
				cellPanel.add(toolBoxPanel);
				cellPanel.add(bottomScrollerPanel);
		
				cellPanel.setCellWidth(toolBoxPanel, "230");
				
				mainLayout.add(cellPanel);
		
				divHidden.setStyleName("d4sFrame");
				divHidden.setWidth("750px");
				divHidden.setStyleName("d4sRichTextArea");
				divHidden.addStyleName("hasRichTextToolbar");
				divHidden.addStyleName("setVisibilityOff");
		
				eastPanel.add(exportResultsPanel);
				eastPanel.add(workSpacePanel);
				eastPanel.add(divHidden);
				bottomScrollerPanel.add(eastPanel);
				presenter.addTextToolBar();
		
				// Add image and button to the RootPanel
				RootPanel.get("ReportGeneratorDIV").add(mainLayout);
		
				int scrollerWidth = Window.getClientWidth()- TEMPLATE_LEFT - 30;
				bottomScrollerPanel.setPixelSize(scrollerWidth, 1000);
		
				if ( Window.getClientWidth() < 1070 ) {
					int scrollerWidth2 = Window.getClientWidth()- TEMPLATE_LEFT;
					bottomScrollerPanel.setPixelSize(scrollerWidth2, 1000);
				}
		
				Window.addResizeHandler(new ResizeHandler() {			
					public void onResize(ResizeEvent event) {
						int scrollerHeight = event.getHeight() - bottomScrollerPanel.getAbsoluteTop();
						if (scrollerHeight < toolBoxPanel.getTreePanelHeight())
							scrollerHeight = toolBoxPanel.getTreePanelHeight(); 
						int scrollerWidth = Window.getClientWidth()- TEMPLATE_LEFT - 30;
						bottomScrollerPanel.setPixelSize(scrollerWidth , 1000);	
		
		
						if ( Window.getClientWidth() < 1060 ) {
							int scrollerWidth2 = Window.getClientWidth()- TEMPLATE_LEFT;
							bottomScrollerPanel.setPixelSize(scrollerWidth2, 1000);
						}
					}
		
				});			
				//presenter.showExportPanel("", "", null, "");
		 //showGuidedTour() ;
				
	}
	private void showGuidedTour() {		

		GWT.runAsync(GCUBEGuidedTour.class, new RunAsyncCallback() {
			public void onSuccess() {
				TourStep step1 = new GCUBETemplate1Text1Image(true) {

					@Override
					public String setStepTitle() {
						return "gCube Reporting";
					}

					@Override
					public String setStepImage() {
						return "images/tour/tour1.jpg";
					}

					@Override
					public String setStepBody() {
						return new Intro().getHTML();
					}
				};

				TourStep step2 = new GCUBETemplate1Text1Image(false) {

					@Override
					public String setStepTitle() {
						return "Structure View";
					}

					@Override
					public String setStepImage() {
						return "images/tour/tour2.jpg";
					}
					@Override
					public String setStepBody() {
						return "<div style=\"line-height: 19px; font-size: 16px; padding: 10px;\">" +
								"<div style=\"padding-bottom: 90px;\">" +
								"Use the <b>Report Structure View</b> to see your report structure. (View > Structure)" +
								"</div>" +
								"<div style=\"padding-bottom: 80px;\">" +
								"Use the <b>Report Structure View</b> to navigate through your report." +
								"</div>" +
								"</div>";
					}
				};
				TourStep step3 = new GCUBETemplate2Text2Image(false) {

					@Override
					public String setStepTitle() {
						return "User Comments";
					}

					@Override
					public String setStepImage() {
						return "images/tour/tourComment.jpg";
					}

					@Override
					public String setStepBody() {
						return "<div style=\"line-height: 19px; padding: 10px; font-size: 16px; \">" +
								"<div style=\"padding-bottom: 10px;\">" +
								"Use comments to collaborate with your colleagues." +
								"</div>" +
								"</div>";
					}

					@Override
					public String setStepOtherImage() {
						return "images/tour/tourFormat.jpg";
					}

					@Override
					public String setStepOtherBody() {
						return "<div style=\"line-height: 19px; padding: 10px; font-size: 16px; \">" +
								"<div style=\"padding-bottom: 10px;\">" +
								"<b>Format text</b> as you would in a word processor using the Formatting Bar." +
								"</div>" +
								"</div>";
					}
				};
				TourStep step4 = new GCUBETemplate1Text1Image(false) {

					@Override
					public String setStepTitle() {
						return "Bibliography";
					}

					@Override
					public String setStepImage() {
						return "images/tour/tourBiblio.jpg";
					}

					@Override
					public String setStepBody() {
						return "<div style=\"line-height: 19px; padding: 10px; font-size: 16px; \">" +
								"<div style=\"padding-bottom: 80px;\">" +
								"Use the <b>Bibliography to add references</b>" +
								"</div>" +
								"</div>";
					}			
				};

				TourStep step5 = new GCUBETemplate1Text1Image(false) {

					@Override
					public String setStepTitle() {
						return "Export";
					}

					@Override
					public String setStepImage() {
						return "images/tour/tourExports.jpg";
					}

					@Override
					public String setStepBody() {
						return "<div style=\"line-height: 19px; padding: 10px; font-size: 16px; \">" +
								"<div style=\"padding-bottom: 50px;\">" +
								"Generate <b>different	export formats</b> such as OpenXML (docx) or HTML." +
								"</div>" +
								"</div>";	
					}
				};
				//step1.setTextVerticalAlignment(VerticalAlignment.ALIGN_MIDDLE);
				step2.setTextVerticalAlignment(VerticalAlignment.ALIGN_MIDDLE);
				step3.setTextVerticalAlignment(VerticalAlignment.ALIGN_MIDDLE);
				step4.setTextVerticalAlignment(VerticalAlignment.ALIGN_MIDDLE);
				step5.setTextVerticalAlignment(VerticalAlignment.ALIGN_MIDDLE);
				String guideUrl = "https://gcube.wiki.gcube-system.org/gcube/index.php/Common_Functionality#Report_Generation";
				GCUBEGuidedTour gt = new GCUBEGuidedTour("gCube Reports Generator", ReportGenerator.class.getName(), guideUrl, 780, 450, false);
				gt.addStep(step1);
				gt.addStep(step2);
				gt.addStep(step3);
				gt.addStep(step4);
				gt.addStep(step5);
				gt.openTour();
			}

			public void onFailure(Throwable reason) {
				Window.alert("There are networks problem, please check your connection.");              
			}
		});
	
	}

	/**
	 * 
	 * @return .
	 */
	public VerticalPanel getMainLayout() {
		return mainLayout;
	}

	/**
	 * 
	 * @return .
	 */
	public Headerbar getHeader() {
		return header;
	}
	/**
	 * 
	 * @return .
	 */
	public WorkspacePanel getWorkSpacePanel() {
		return workSpacePanel;
	}

	/**
	 * 
	 * @return .
	 */
	public ToolboxPanel getToolBoxPanel() {
		return toolBoxPanel;
	}
	
	public  GWTWorkspaceServiceAsync getWSTreeService() {
		return treeController.getRpcWorkspaceService();
	}


	/**
	 * 
	 * @return .
	 */
	public VerticalPanel getToolbarPanel() {
		return toolbarPanel;
	}

	/**
	 * 
	 * @return .
	 */
	public TitleBar getTitleHeader() {
		return titlebar;
	}

	/**
	 * 
	 * @return .
	 */
	public HTML getDivHidden() {
		return divHidden;
	}

	/**
	 * 
	 * @param divHidden .
	 */
	public void setDivHidden(HTML divHidden) {
		this.divHidden = divHidden;
	}

	public ScrollPanel getScrollerPanel() {
		return bottomScrollerPanel;
	}
}
