/*
 * DesktopFileDialogs.java
 *
 * Copyright (C) 2009-11 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
package org.rstudio.studio.client.common.impl;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.inject.Inject;
import org.rstudio.core.client.StringUtil;
import org.rstudio.core.client.files.FileSystemContext;
import org.rstudio.core.client.files.FileSystemItem;
import org.rstudio.core.client.widget.ProgressIndicator;
import org.rstudio.core.client.widget.ProgressOperationWithInput;
import org.rstudio.studio.client.RStudioGinjector;
import org.rstudio.studio.client.application.Desktop;
import org.rstudio.studio.client.common.FileDialogs;
import org.rstudio.studio.client.common.GlobalDisplay;
import org.rstudio.studio.client.server.ServerError;
import org.rstudio.studio.client.server.ServerRequestCallback;
import org.rstudio.studio.client.workbench.views.files.model.FilesServerOperations;

public class DesktopFileDialogs implements FileDialogs
{
   private class NullProgress implements ProgressIndicator
   {
      public void onProgress(String message)
      {
      }

      public void onCompleted()
      {
      }

      public void onError(String message)
      {
         RStudioGinjector.INSTANCE.getGlobalDisplay().showErrorMessage("Error",
                                                                       message);
      }
   }

   private abstract class FileDialogOperation
   {
      abstract String operation(String caption, String dir);

      protected boolean shouldUpdateDetails()
      {
         return false;
      }

      public void execute(
            final String caption,
            FileSystemContext fsContext,
            FileSystemItem initialFilePath,
            final ProgressOperationWithInput<FileSystemItem> operation)
      {
         final String dir = initialFilePath == null
                            ? fsContext.pwd()
                            : initialFilePath.getPath();

         Scheduler.get().scheduleDeferred(new ScheduledCommand()
         {
            public void execute()
            {
               String file = operation(caption, dir);

               FileSystemItem item =
                     StringUtil.isNullOrEmpty(file)
                     ? null
                     : FileSystemItem.createFile(file);

               if (item != null && shouldUpdateDetails())
               {
                  server_.stat(item.getPath(), new ServerRequestCallback<FileSystemItem>()
                  {
                     @Override
                     public void onResponseReceived(FileSystemItem response)
                     {
                        operation.execute(response, new NullProgress());
                     }

                     @Override
                     public void onError(ServerError error)
                     {
                        globalDisplay_.showErrorMessage("Error",
                                                        error.getUserMessage());
                        operation.execute(null, new NullProgress());
                     }
                  });
               }
               else
               {
                  operation.execute(item, new NullProgress());
               }
            }
         });
      }
   }

   public DesktopFileDialogs()
   {
      RStudioGinjector.INSTANCE.injectMembers(this);
   }

   @Inject
   public void initialize(FilesServerOperations server,
                          GlobalDisplay globalDisplay)
   {
      server_ = server;
      globalDisplay_ = globalDisplay;
   }

   public void openFile(final String caption,
                        final FileSystemContext fsContext,
                        final FileSystemItem initialFilePath,
                        final ProgressOperationWithInput<FileSystemItem> operation)
   {
      new FileDialogOperation()
      {
         @Override
         protected boolean shouldUpdateDetails()
         {
            return true;
         }

         @Override
         String operation(String caption, String dir)
         {
            String fileName = Desktop.getFrame().getOpenFileName(caption, dir);
            if (fileName != null)
            {
               updateWorkingDirectory(fileName, fsContext);
            }
            return fileName;
         }
      }.execute(caption, fsContext, initialFilePath, operation);
   }

   public void saveFile(final String caption,
                        final FileSystemContext fsContext,
                        final FileSystemItem initialFilePath,
                        final String defaultExtension,
                        final ProgressOperationWithInput<FileSystemItem> operation)
   {
      new FileDialogOperation()
      {
         @Override
         String operation(String caption, String dir)
         {
            String fileName = Desktop.getFrame().getSaveFileName(
                  caption, dir, defaultExtension);

            if (fileName != null)
            {
               updateWorkingDirectory(fileName, fsContext);
            }
            return fileName;
         }
      }.execute(caption,
                fsContext,
                initialFilePath,
                operation);
   }

   public void chooseFolder(String caption,
                            FileSystemContext fsContext,
                            final FileSystemItem initialDir,
                            ProgressOperationWithInput<FileSystemItem> operation)
   {
      new FileDialogOperation()
      {
         @Override
         String operation(String caption, String dir)
         {
            return Desktop.getFrame().getExistingDirectory(
                  caption,
                  initialDir != null ? initialDir.getPath() : null);
         }
      }.execute(caption, fsContext, null, operation);
   }

   private void updateWorkingDirectory(String fileName,
                                       FileSystemContext fsContext)
   {
      if (fileName != null)
      {
         String parentPath =
               FileSystemItem.createFile(fileName).getParentPathString();
         if (!StringUtil.isNullOrEmpty(parentPath))
            fsContext.cd(parentPath);
      }
   }

   private FilesServerOperations server_;
   private GlobalDisplay globalDisplay_;
}

@@ -600,41 +600,8 @@
 nd;%0A
-import com.google.inject.Inject;%0A
 impo
@@ -1032,263 +1032,8 @@
 ogs;
-%0Aimport org.rstudio.studio.client.common.GlobalDisplay;%0Aimport org.rstudio.studio.client.server.ServerError;%0Aimport org.rstudio.studio.client.server.ServerRequestCallback;%0Aimport org.rstudio.studio.client.workbench.views.files.model.FilesServerOperations;
 %0A%0Apu
@@ -1604,94 +1604,8 @@
 );%0A%0A
-      protected boolean shouldUpdateDetails()%0A      %7B%0A         return false;%0A      %7D%0A%0A
     
@@ -2339,853 +2339,8 @@
 );%0A%0A
-               if (item != null && shouldUpdateDetails())%0A               %7B%0A                  server_.stat(item.getPath(), new ServerRequestCallback%3CFileSystemItem%3E()%0A                  %7B%0A                     @Override%0A                     public void onResponseReceived(FileSystemItem response)%0A                     %7B%0A                        operation.execute(response, new NullProgress());%0A                     %7D%0A%0A                     @Override%0A                     public void onError(ServerError error)%0A                     %7B%0A                        globalDisplay_.showErrorMessage(%22Error%22,%0A                                                        error.getUserMessage());%0A                        operation.execute(null, new NullProgress());%0A                     %7D%0A                  %7D);%0A               %7D%0A               else%0A               %7B%0A   
     
@@ -2399,25 +2399,8 @@
 ));%0A
-               %7D%0A
     
@@ -2440,298 +2440,8 @@
  %7D%0A%0A
-   public DesktopFileDialogs()%0A   %7B%0A      RStudioGinjector.INSTANCE.injectMembers(this);%0A   %7D%0A%0A   @Inject%0A   public void initialize(FilesServerOperations server,%0A                          GlobalDisplay globalDisplay)%0A   %7B%0A      server_ = server;%0A      globalDisplay_ = globalDisplay;%0A   %7D%0A%0A
    p
@@ -2736,124 +2736,8 @@
   %7B%0A
-         @Override%0A         protected boolean shouldUpdateDetails()%0A         %7B%0A            return true;%0A         %7D%0A%0A
     
@@ -4937,91 +4937,7 @@
    %7D
-%0A%0A   private FilesServerOperations server_;%0A   private GlobalDisplay globalDisplay_;
 %0A%7D%0A

Fix unresponsive desktop workbench