/**
 * Copyright 2014 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.jboss.forge.addon.shell.command;

import java.awt.Desktop;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.inject.Inject;

import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.resource.Resource;
import org.jboss.forge.addon.resource.ResourceFactory;
import org.jboss.forge.addon.resource.util.ResourcePathResolver;
import org.jboss.forge.addon.shell.ui.AbstractShellCommand;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.input.UIInputMany;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.metadata.WithAttributes;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.result.Results;
import org.jboss.forge.addon.ui.util.Metadata;

/**
 * 
 * @author <a href="ggastald@redhat.com">George Gastaldi</a>
 */
public class EditCommand extends AbstractShellCommand
{
   @Inject
   ResourceFactory resourceFactory;

   @Inject
   @WithAttributes(label = "Arguments", type = InputType.FILE_PICKER)
   private UIInputMany<String> arguments;

   @Override
   public void initializeUI(UIBuilder builder) throws Exception
   {
      builder.add(arguments);
   }

   @Override
   public UICommandMetadata getMetadata(UIContext context)
   {
      return Metadata.from(super.getMetadata(context), getClass()).name("edit")
               .description("Edit files with the default system editor");
   }

   @Override
   public Result execute(UIExecutionContext context) throws Exception
   {
      Resource<?> currentResource = (Resource<?>) context.getUIContext().getInitialSelection().get();
      Iterable<String> value = arguments.getValue();
      Iterator<String> it = value == null ? Collections.<String> emptyIterator() : value.iterator();
      final Result result;
      if (it.hasNext())
      {
         String newPath = it.next();
         final List<Resource<?>> newResource = new ResourcePathResolver(resourceFactory, currentResource, newPath)
                  .resolve();
         if (newResource.isEmpty() || !newResource.get(0).exists())
         {
            result = Results.fail(newPath + ": No such file or directory");
         }
         else
         {
            for (Resource<?> resource : newResource)
            {
               editResource(resource);
            }
            result = Results.success();
         }
      }
      else if (currentResource != null)
      {
         editResource(currentResource);
         result = Results.success();
      }
      else
      {
         result = Results.fail("Resource not found");
      }
      return result;
   }

   private void editResource(Resource<?> resource) throws IOException
   {
      Desktop dt = Desktop.getDesktop();
      FileResource<?> fileResource = resource.reify(FileResource.class);
      if (fileResource != null)
      {
         dt.edit(fileResource.getUnderlyingResourceObject());
      }
   }

}

@@ -1044,16 +1044,65 @@
 ibutes;%0A
+import org.jboss.forge.addon.ui.output.UIOutput;%0A
 import o
@@ -2763,16 +2763,25 @@
 esource(
+context, 
 resource
@@ -2926,16 +2926,25 @@
 esource(
+context, 
 currentR
@@ -3135,16 +3135,44 @@
 esource(
+UIExecutionContext context, 
 Resource
@@ -3345,32 +3345,63 @@
 Resource != null
+ && !fileResource.isDirectory()
 )%0A      %7B%0A      
@@ -3464,16 +3464,215 @@
       %7D%0A
+      else%0A      %7B%0A         UIOutput output = context.getUIContext().getProvider().getOutput();%0A         output.warn(output.err(), %22Cannot edit %5B%22 + resource.getFullyQualifiedName() + %22%5D.%22);%0A      %7D%0A
    %7D%0A%0A%7D%0A

Edit command should skip directories (causes freezes)