package mesfavoris.gdrive.handlers;

import java.io.IOException;
import java.util.Optional;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;

import mesfavoris.BookmarksException;
import mesfavoris.BookmarksPlugin;
import mesfavoris.gdrive.Activator;
import mesfavoris.gdrive.connection.GDriveConnectionManager;
import mesfavoris.gdrive.dialogs.ImportBookmarksFileDialog;
import mesfavoris.gdrive.mappings.BookmarkMappingsStore;
import mesfavoris.gdrive.mappings.IBookmarkMappings;
import mesfavoris.gdrive.operations.ImportBookmarkFileOperation;
import mesfavoris.model.BookmarkFolder;
import mesfavoris.service.IBookmarksService;

public class ImportBookmarksFromGdriveHandler extends AbstractHandler {
	private final GDriveConnectionManager gDriveConnectionManager;
	private final BookmarkMappingsStore bookmarkMappingsStore;
	private final IBookmarksService bookmarksService;
	private final IBookmarkMappings bookmarkMappings;

	public ImportBookmarksFromGdriveHandler() {
		this.gDriveConnectionManager = Activator.getGDriveConnectionManager();
		this.bookmarkMappingsStore = Activator.getBookmarkMappingsStore();
		this.bookmarksService = BookmarksPlugin.getBookmarksService();
		this.bookmarkMappings = Activator.getBookmarkMappingsStore();
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
		BookmarkFolder bookmarkFolder = selection.isEmpty() ? bookmarksService.getBookmarksTree().getRootFolder()
				: (BookmarkFolder) selection.getFirstElement();
		Shell shell = HandlerUtil.getActiveShell(event);
		Drive drive = gDriveConnectionManager.getDrive();
		if (drive == null) {
			return null;
		}
		ImportBookmarksFileDialog dialog = new ImportBookmarksFileDialog(shell, gDriveConnectionManager.getDrive(),
				gDriveConnectionManager.getApplicationFolderId(), bookmarkMappings);
		if (dialog.open() == Dialog.CANCEL) {
			return null;
		}
		File file = dialog.getFile();
		ImportBookmarkFileOperation importBookmarkFileOperation = new ImportBookmarkFileOperation(drive,
				bookmarkMappingsStore, bookmarksService, Optional.of(gDriveConnectionManager.getApplicationFolderId()));
		try {
			importBookmarkFileOperation.importBookmarkFile(bookmarkFolder.getId(), file.getId(),
					new NullProgressMonitor());
		} catch (BookmarksException | IOException e) {
			throw new ExecutionException("Could not import bookmarks file", e);
		}
		return null;
	}

	@Override
	public boolean isEnabled() {
		Drive drive = gDriveConnectionManager.getDrive();
		if (drive == null) {
			return false;
		}
		return true;
	}

}

@@ -54,24 +54,76 @@
 OException;%0A
+import java.lang.reflect.InvocationTargetException;%0A
 import java.
@@ -294,61 +294,8 @@
 on;%0A
-import org.eclipse.core.runtime.NullProgressMonitor;%0A
 impo
@@ -424,16 +424,50 @@
 .Shell;%0A
+import org.eclipse.ui.PlatformUI;%0A
 import o
@@ -501,16 +501,65 @@
 lerUtil;
+%0Aimport org.eclipse.ui.progress.IProgressService;
 %0A%0Aimport
@@ -719,16 +719,48 @@
 Plugin;%0A
+import mesfavoris.StatusHelper;%0A
 import m
@@ -2354,42 +2354,13 @@
 ll, 
-gD
+d
 rive
-ConnectionManager.getDrive()
 ,%0A%09%09
@@ -2522,16 +2522,160 @@
 File();%0A
+%0A%09%09IProgressService progressService = PlatformUI.getWorkbench().getProgressService();%0A%09%09try %7B%0A%09%09%09progressService.busyCursorWhile(monitor -%3E %7B%0A%09%09
 %09%09Import
@@ -2769,16 +2769,18 @@
 ve,%0A%09%09%09%09
+%09%09
 bookmark
@@ -2811,17 +2811,23 @@
 Service,
- 
+%0A%09%09%09%09%09%09
 Optional
@@ -2882,16 +2882,18 @@
 Id()));%0A
+%09%09
 %09%09try %7B%0A
@@ -2895,16 +2895,18 @@
 ry %7B%0A%09%09%09
+%09%09
 importBo
@@ -2991,36 +2991,22 @@
 %09%09%09%09
-new NullProgressM
+%09%09m
 onitor
-()
 );%0A
+%09%09
 %09%09%7D 
@@ -3053,16 +3053,18 @@
 e) %7B%0A%09%09%09
+%09%09
 throw ne
@@ -3069,63 +3069,291 @@
 new 
-ExecutionException(%22Could not import bookmarks file%22, e
+InvocationTargetException(e);%0A%09%09%09%09%7D%0A%0A%09%09%09%7D);%0A%09%09%7D catch (InvocationTargetException e) %7B%0A%09%09%09StatusHelper.showError(%22Could not import bookmarks file%22, e.getCause(), false);%0A%09%09%7D catch (InterruptedException e) %7B%0A%09%09%09throw new ExecutionException(%22Could not import bookmarks file : cancelled%22
 );%0A%09

Use progress service when importing bookmarks from gDrive