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

import org.gcube.portlets.user.reportgenerator.client.ReportConstants;
import org.gcube.portlets.user.reportgenerator.client.dialog.ImporterDialog;
import org.gcube.portlets.user.reportgenerator.client.model.TemplateModel;
import org.gcube.portlets.widgets.lighttree.client.ItemType;
import org.gcube.portlets.widgets.lighttree.client.event.PopupEvent;
import org.gcube.portlets.widgets.lighttree.client.event.PopupHandler;
import org.gcube.portlets.widgets.lighttree.client.load.WorkspaceLightTreeLoadPopup;
import org.gcube.portlets.widgets.lighttree.client.save.WorkspaceLightTreeSavePopup;

import com.extjs.gxt.ui.client.event.ColorPaletteEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ColorPalette;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.PopupPanel;



/**
 * * 
 * /**
 * <code> CommonCommands </code> class contains the menu commands for the UI
 *
 * @author Massimiliano Assante, ISTI-CNR - massimiliano.assante@isti.cnr.it
 */
public class CommonCommands {
	/**
	 * 
	 */
	public Command openTemplate;
	/**
	 * 
	 */
	public Command openReport;
	
	public Command newTemplate;
	/**
	 * 
	 */
	public Command importTemplateCommand;
	/**
	 * 
	 */
	public Command insertImage;
	/**
	 * 
	 */
	public Command saveTemplate;
	/**
	 * 
	 */
	public Command pickColor;

	private Presenter presenter;

	/**
	 * 
	 * @param presenter .
	 */
	public CommonCommands(final Presenter presenter) {	
		this.presenter = presenter;
		
		newTemplate = new Command() {
			public void execute() {
				presenter.changeTemplateName(TemplateModel.DEFAULT_NAME);
				presenter.cleanAll();
				presenter.showOpenOptions();
			}
		};
	
		
		openTemplate = new Command() {	
			public void execute() {			
				final int left = presenter.getHeader().getMainLayout().getAbsoluteLeft() + 50;
				final int top = presenter.getHeader().getMainLayout().getAbsoluteTop() + 25;

				GWT.runAsync(WorkspaceLightTreeLoadPopup.class, new RunAsyncCallback() {
					public void onSuccess() {
						WorkspaceLightTreeLoadPopup wpTreepopup = new WorkspaceLightTreeLoadPopup("Select a Template to open", true, true);
						wpTreepopup.setWidth("450px");
						wpTreepopup.setShowableTypes(ItemType.REPORT_TEMPLATE);
						wpTreepopup.setSelectableTypes(ItemType.REPORT_TEMPLATE);


						wpTreepopup.addPopupHandler(new PopupHandler() {
							public void onPopup(PopupEvent event) {
								if (! event.isCanceled()) {
									if (event.getSelectedItem() != null) {
										presenter.openTemplate(event.getSelectedItem().getName(), event.getSelectedItem().getId(), true);				
									}			
								}

							}
						});

						wpTreepopup.setPopupPosition(left, top);
						wpTreepopup.show();
					}

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

		openReport = new Command() {	
			public void execute() {

				GWT.runAsync(WorkspaceLightTreeLoadPopup.class, new RunAsyncCallback() {
					public void onSuccess() {						
						int left = presenter.getHeader().getMainLayout().getAbsoluteLeft() + 50;
						int top = presenter.getHeader().getMainLayout().getAbsoluteTop() + 25;

						WorkspaceLightTreeLoadPopup wpTreepopup = new WorkspaceLightTreeLoadPopup("Select a Report to open", true, true);
						wpTreepopup.setWidth("450px");
						wpTreepopup.setShowableTypes(ItemType.REPORT);
						wpTreepopup.setSelectableTypes(ItemType.REPORT);
						wpTreepopup.addPopupHandler(new PopupHandler() {
							public void onPopup(PopupEvent event) {
								if (! event.isCanceled()) {
									if (event.getSelectedItem() != null) {
										presenter.openTemplate(event.getSelectedItem().getName(), event.getSelectedItem().getId(), false);		
									}	
									presenter.getHeader().enableExports();			
								}

							}
						});
						wpTreepopup.setPopupPosition(left, top);
						wpTreepopup.show();
					}

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

		pickColor = new Command() {	
			public void execute() {				
				int left = presenter.getHeader().getMainLayout().getAbsoluteLeft() + 600;
				int top = presenter.getHeader().getMainLayout().getAbsoluteTop() + 50;
				final PopupPanel pp = new PopupPanel(true);
		
				ColorPalette colorPalette = new ColorPalette(); 
				colorPalette.addListener(Events.Select, new Listener<ColorPaletteEvent>() {
					@SuppressWarnings("deprecation")
					public void handleEvent(ColorPaletteEvent be) {
						presenter.getCurrentSelected().getExtendedFormatter().setForeColor(be.getColor());
						pp.hide();
					}
				});
				pp.add(colorPalette);
				pp.setAnimationEnabled(false);
				pp.setPopupPosition(left, top);
				pp.show();
			}
		};



		importTemplateCommand  = new Command() {			
			public void execute() {
				if (! ReportConstants.isDeployed) {
					int left = presenter.getHeader().getMainLayout().getAbsoluteLeft() + 50;
					int top = presenter.getHeader().getMainLayout().getAbsoluteTop() + 25;
					ImporterDialog dlg = new ImporterDialog(null, presenter);
					dlg.setPopupPosition(left, top);
					dlg.setAnimationEnabled(true);
					dlg.show();
				}
				else {
					GWT.runAsync(WorkspaceLightTreeLoadPopup.class, new RunAsyncCallback() {
						public void onSuccess() {
							WorkspaceLightTreeLoadPopup wpTreepopup = new WorkspaceLightTreeLoadPopup("Pick the item you want to import from", true, true);
							wpTreepopup.setShowableTypes(ItemType.REPORT_TEMPLATE);			
							wpTreepopup.setWidth("450px");

							wpTreepopup.addPopupHandler(new PopupHandler() {
								public void onPopup(PopupEvent event) {
									if (! event.isCanceled()) {
										if (event.getSelectedItem() != null) {
											int left = presenter.getHeader().getMainLayout().getAbsoluteLeft() + 50;
											int top = presenter.getHeader().getMainLayout().getAbsoluteTop() + 25;
											ImporterDialog dlg = new ImporterDialog(event.getSelectedItem(), presenter);
											dlg.setPopupPosition(left, top);
											dlg.setAnimationEnabled(true);
											dlg.show();			
										}			
									}

								}
							});
							int left = presenter.getHeader().getMainLayout().getAbsoluteLeft() + 50;
							int top = presenter.getHeader().getMainLayout().getAbsoluteTop() + 25;
							wpTreepopup.setPopupPosition(left, top);
							wpTreepopup.show();

						}

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

		saveTemplate = new Command() {			
			public void execute() {
				if (presenter.getModel().getTemplateName().endsWith("d4sR"))
					presenter.saveReport();
				else
					saveReportAsDialog();
			}
		};

	} //end constructor

	public void saveReportAsDialog() {
		final int left = presenter.getHeader().getMainLayout().getAbsoluteLeft() + 50;
		final int top = presenter.getHeader().getMainLayout().getAbsoluteTop() + 25;

		GWT.runAsync(WorkspaceLightTreeSavePopup.class, new RunAsyncCallback() {
			public void onSuccess() {
				WorkspaceLightTreeSavePopup wpTreepopup = new WorkspaceLightTreeSavePopup("Save Template, choose folder please:", true);
				wpTreepopup.setWidth("450px");
				wpTreepopup.setSelectableTypes( ItemType.FOLDER, ItemType.ROOT);
				wpTreepopup.setShowEmptyFolders(true);

				wpTreepopup.addPopupHandler(new PopupHandler() {
					public void onPopup(PopupEvent event) {

						//checking user input
						String inputUser = event.getName();
						String newTemplateName = inputUser;
						if (presenter.getModel().getTemplateName().compareTo(newTemplateName) != 0) {
							newTemplateName = newTemplateName.trim();
							presenter.getModel().setTemplateName(newTemplateName+".d4sR");
						}
						presenter.changeTemplateName(newTemplateName);
						presenter.saveReport(event.getSelectedItem().getId(), newTemplateName);
					}
				});
				wpTreepopup.setPopupPosition(left, top);
				wpTreepopup.show();				
			}

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


	}
}
