package it.micegroup.voila2.toolbox.handlers;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.FileInfoMatcherDescription;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

public class CommitSyncAction implements IRunnableWithProgress {

	private ExecutionEvent event;

	public CommitSyncAction(ExecutionEvent event) {
		this.event = event;
	}

	@Override
	public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
		addFilters(monitor);
	}

	private void addFilters(IProgressMonitor monitor) {
//		if (StaticResources.getProjectsToFilter().isEmpty()) {
		try {
			for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
//				project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
				project.createFilter(26, new FileInfoMatcherDescription("org.eclipse.ui.ide.multiFilter",
						"1.0-name-matches-false-false-modules"), IResource.BACKGROUND_REFRESH, monitor);
				project.createFilter(26, new FileInfoMatcherDescription("org.eclipse.ui.ide.multiFilter",
						"1.0-name-matches-false-false-.syncFolder"), IResource.BACKGROUND_REFRESH, monitor);
				project.createFilter(26, new FileInfoMatcherDescription("org.eclipse.ui.ide.multiFilter",
						"1.0-name-matches-false-false-.target"), IResource.BACKGROUND_REFRESH, monitor);
			}
		} catch (CoreException e) {
			e.printStackTrace();
		}
//		} else {
//			for (IProject project : StaticResources.getProjectsToFilter()) {
//				try {
//					project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
//					project.createFilter(26,
//							new FileInfoMatcherDescription("org.eclipse.ui.ide.multiFilter",
//									"1.0-name-matches-false-false-modules"),
//							IResource.BACKGROUND_REFRESH, monitor);
//					project.createFilter(26,
//							new FileInfoMatcherDescription("org.eclipse.ui.ide.multiFilter",
//									"1.0-name-matches-false-false-.syncFolder"),
//							IResource.BACKGROUND_REFRESH, monitor);
//					project.createFilter(26,
//							new FileInfoMatcherDescription("org.eclipse.ui.ide.multiFilter",
//									"1.0-name-matches-false-false-.target"),
//							IResource.BACKGROUND_REFRESH, monitor);
//				} catch (CoreException e) {
//					e.printStackTrace();
//				}
//			}
//		}		
		try {
			IWorkbenchWindow window;
			window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
			MessageDialog.openInformation(window.getShell(), "Success!", "La sincronizzazione è stata ultimata");
		} catch (

		ExecutionException e) {
			e.printStackTrace();
		}
	}
}
