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

import org.gcube.portlets.widgets.exporter.shared.TypeExporter;
import org.gcube.portlets.user.reportgenerator.client.ReportServiceAsync;
import org.gcube.portlets.user.reportgenerator.client.ToolboxPanel;
import org.gcube.portlets.user.reportgenerator.client.Presenter.Presenter;
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.save.WorkspaceLightTreeSavePopup;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;

public class ExportOptions extends Composite {

	private static ExportOptionsUiBinder uiBinder = GWT
			.create(ExportOptionsUiBinder.class);

	interface ExportOptionsUiBinder extends UiBinder<Widget, ExportOptions> {
	}
	enum ExportMode {SAVE_OPEN, SAVE, SAVE_AS }

	ToolboxPanel tbp;
	@UiField HTML saveOpen;
	@UiField HTML save;
	@UiField HTML saveAs;
	@UiField HTML close;
	@UiField HTMLPanel myPanel;

	private String tempFileId;
	private String filePath;
	private String itemName;

	private  TypeExporter type;
	private ReportServiceAsync rpc;
	private Presenter p;

	public ExportOptions(Presenter p, ToolboxPanel tbp, final String filePath, final String itemName, final TypeExporter type, ReportServiceAsync rpc,  String tempFileId) {
		initWidget(uiBinder.createAndBindUi(this));
		this.tbp = tbp;
		this.filePath = filePath;
		this.itemName = itemName;
		this.type= type;
		this.rpc = rpc;
		this.p = p;
		this.tempFileId = tempFileId;
	}

	@UiHandler("close")
	void onSaveCancel(ClickEvent e) {
		myPanel.removeStyleName("exportPanel-show");
		//needed for applying the css3 transition effect
		final Timer t = new Timer() {
			@Override
			public void run() {
				p.clearExportPanel();
			}
		};
		t.schedule(500);		
	}

	@UiHandler("saveOpen")
	void onSaveOpenClick(ClickEvent e) {
		GWT.log("SaveOPen");
		doCallBack(ExportMode.SAVE_OPEN);
	}

	@UiHandler("save")
	void onSaveClick(ClickEvent e) {
		doCallBack(ExportMode.SAVE);
	}

	@UiHandler("saveAs")
	void onSaveAs(ClickEvent e) {
		doCallBack(ExportMode.SAVE_AS);
	}


	private void doCallBack(ExportMode mode) {
		switch(mode) {
		case SAVE:
			rpc.save(filePath, null, itemName, type, true, new AsyncCallback<String>() {			
				@Override
				public void onSuccess(String createdItemId) {
					p.clearExportPanel();
					tbp.refreshRoot();
				}

				@Override
				public void onFailure(Throwable caught) {		
					Window.alert("Error: " + caught.getMessage());
				}
			});
			break;
		case SAVE_AS:
			WorkspaceLightTreeSavePopup wpTree = new WorkspaceLightTreeSavePopup("Save Report, select the folder:", true);
			wpTree.setSelectableTypes(ItemType.FOLDER, ItemType.ROOT);
			wpTree.setShowEmptyFolders(true);
			wpTree.addPopupHandler(new PopupHandler(){

				@Override
				public void onPopup(PopupEvent event) {
					rpc.save(filePath, event.getSelectedItem().getId(), event.getName(), type, true, new AsyncCallback<String>() {			
						@Override
						public void onSuccess(String createdItemId) {
							p.clearExportPanel();
							tbp.refreshRoot();	
						}

						@Override
						public void onFailure(Throwable caught) {		
							Window.alert("Error: " + caught.getMessage());
						}
					});									
				}});	
			wpTree.show();
			wpTree.center();
			break;
		case
		SAVE_OPEN:
			/*
			 * Smart popup blockers (Chrome and Safari have them) will allow a popup if it is directly associated to a user’s action. 
			 * If it’s delayed in anyway, there’s a good chance it’s going to get blocked. 
			 * So the exported File needs to be there when clicking open, and I've saved a temp version previously.
			 */
			tbp.showExportedVersion(tempFileId, itemName);
			rpc.save(filePath, null, itemName, type, true, new AsyncCallback<String>() {			
				@Override
				public void onSuccess(String createdItemId) {
					p.clearExportPanel();
					tbp.refreshRoot();
				}

				@Override
				public void onFailure(Throwable caught) {		
					Window.alert("Error: " + caught.getMessage());
				}
			});
			break;	
		}
	}

	public HTMLPanel getMainPanel() {
		return myPanel;
	}

}
