package org.tigris.subversion.subclipse.ui.subscriber;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.compare.structuremergeviewer.IDiffElement;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.team.core.synchronize.FastSyncInfoFilter;
import org.eclipse.team.core.synchronize.SyncInfo;
import org.eclipse.team.core.synchronize.FastSyncInfoFilter.SyncInfoDirectionFilter;
import org.eclipse.team.ui.synchronize.ISynchronizeModelElement;
import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
import org.eclipse.team.ui.synchronize.SynchronizeModelAction;
import org.eclipse.team.ui.synchronize.SynchronizeModelOperation;
import org.tigris.subversion.subclipse.core.ISVNLocalResource;
import org.tigris.subversion.subclipse.core.SVNException;
import org.tigris.subversion.subclipse.core.commands.GetStatusCommand;
import org.tigris.subversion.subclipse.core.resources.SVNWorkspaceRoot;
import org.tigris.subversion.subclipse.core.util.Util;
import org.tigris.subversion.svnclientadapter.ISVNStatus;
import org.tigris.subversion.svnclientadapter.SVNStatusUtils;

public class RevertSynchronizeAction extends SynchronizeModelAction {
	private String url;

    public RevertSynchronizeAction(String text, ISynchronizePageConfiguration configuration) {
        super(text, configuration);
    }
    
	/* (non-Javadoc)
	 * @see org.eclipse.team.ui.synchronize.SynchronizeModelAction#getSyncInfoFilter()
	 */
	protected FastSyncInfoFilter getSyncInfoFilter() {
		return new SyncInfoDirectionFilter(new int[] {SyncInfo.OUTGOING, SyncInfo.CONFLICTING});
	}

    protected SynchronizeModelOperation getSubscriberOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements) {
		url = null;
	    IStructuredSelection selection = getStructuredSelection();
	    if (selection.size() == 1) {
	        ISynchronizeModelElement element = (ISynchronizeModelElement)selection.getFirstElement();
		    IResource resource = element.getResource();
		    ISVNLocalResource svnResource = SVNWorkspaceRoot.getSVNResourceFor(resource);
            try {
                url = svnResource.getStatus().getUrlString();
                if ((url == null) || (resource.getType() == IResource.FILE)) url = Util.getParentUrl(svnResource);
            } catch (SVNException e) {
                e.printStackTrace();
            }	    
	    }
	    ArrayList selectedElements = new ArrayList();
	    Iterator iter = selection.iterator();
		while (iter.hasNext()) {
			ISynchronizeModelElement synchronizeModelElement = (ISynchronizeModelElement)iter.next();
			IResource resource = synchronizeModelElement.getResource();
			selectedElements.add(resource);
		}
		IResource[] resources = new IResource[selectedElements.size()];
		selectedElements.toArray(resources); 
		IResource[] modifiedResources = null;
		try {
			modifiedResources = getModifiedResources(resources, new NullProgressMonitor());					
		} catch (SVNException e) {
            
        }
		return new RevertSynchronizeOperation(configuration, elements, url, modifiedResources);
    }
    
	private IResource[] getModifiedResources(IResource[] resources, IProgressMonitor iProgressMonitor) throws SVNException {
	    final List modified = new ArrayList();
	    for (int i = 0; i < resources.length; i++) {
			 IResource resource = resources[i];
			 ISVNLocalResource svnResource = SVNWorkspaceRoot.getSVNResourceFor(resource);
			 
			 // if only one resource selected, get url.  Revert dialog displays this.
			 if (resources.length == 1) {
				   url = svnResource.getStatus().getUrlString();
				   if ((url == null) || (resource.getType() == IResource.FILE)) url = Util.getParentUrl(svnResource);
			 }
			 
			 // get adds, deletes, updates and property updates.
			 GetStatusCommand command = new GetStatusCommand(svnResource, true, false);
			 command.run(iProgressMonitor);
			 ISVNStatus[] statuses = command.getStatuses();
			 for (int j = 0; j < statuses.length; j++) {
			     if (SVNStatusUtils.isReadyForRevert(statuses[j])) {
			         IResource currentResource = SVNWorkspaceRoot.getResourceFor(statuses[j]);
			         if (currentResource != null)
			             modified.add(currentResource);
			     }
			 }
		}
	    return (IResource[]) modified.toArray(new IResource[modified.size()]);
	}
	
}

@@ -509,93 +509,8 @@
 fo;%0A
-import org.eclipse.team.core.synchronize.FastSyncInfoFilter.SyncInfoDirectionFilter;%0A
 impo
@@ -1619,88 +1619,918 @@
 new 
-SyncInfoDirectionFilter(new int%5B%5D %7BSyncInfo.OUTGOING, SyncInfo.CONFLICTING%7D);%0A%09%7D
+FastSyncInfoFilter() %7B%0A%09%09%09public boolean select(SyncInfo info) %7B%0A%09%09%09%09SyncInfoDirectionFilter outgoingFilter = new SyncInfoDirectionFilter(new int%5B%5D %7BSyncInfo.OUTGOING, SyncInfo.CONFLICTING%7D);%0A%09%09%09    if (!outgoingFilter.select(info)) return false;%0A%09%09%09    IStructuredSelection selection = getStructuredSelection();%0A%09%09%09    Iterator iter = selection.iterator();%0A%09%09%09    while (iter.hasNext()) %7B%0A%09%09%09    %09ISynchronizeModelElement element = (ISynchronizeModelElement)iter.next();%0A%09%09%09    %09IResource resource = element.getResource();%0A%09%09%09    %09if (resource.isLinked()) return false;%0A%09                ISVNLocalResource svnResource = SVNWorkspaceRoot.getSVNResourceFor(resource);%09%09%09    %0A%09                try %7B%0A%09                %09if (!svnResource.isManaged()) return false;%0A%09                %7D catch (SVNException e) %7B%0A%09                    return false;%0A%09                %7D%0A%09%09%09    %7D%0A                return true;%0A%09%09%09%7D%0A%09%09%7D;%0A%09%7D    
 %0A%0A  

Revert action should not show up in Synch view on new unversioned files.