/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.cli.fs.command;

import alluxio.AlluxioURI;
import alluxio.client.file.FileSystem;
import alluxio.client.file.options.DeleteOptions;
import alluxio.exception.AlluxioException;
import alluxio.exception.ExceptionMessage;
import alluxio.exception.FileDoesNotExistException;
import alluxio.exception.status.InvalidArgumentException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

import java.io.IOException;

import javax.annotation.concurrent.ThreadSafe;

/**
 * Removes the file specified by argv.
 */
@ThreadSafe
public final class RmCommand extends WithWildCardPathCommand {

  private static final Option RECURSIVE_OPTION =
      Option.builder("R")
          .required(false)
          .hasArg(false)
          .desc("delete files and subdirectories recursively")
          .build();

  private static final String REMOVE_UNCHECKED_OPTION_CHAR = "U";
  private static final Option REMOVE_UNCHECKED_OPTION =
      Option.builder(REMOVE_UNCHECKED_OPTION_CHAR)
          .required(false)
          .hasArg(false)
          .desc("remove directories without checking UFS contents are in sync")
          .build();

  private static final Option REMOVE_ALLUXIO_ONLY =
      Option.builder("alluxioOnly")
          .required(false)
          .hasArg(false)
          .desc("remove data and metadata from Alluxio space,"
              + " but the data in under storage is not deleted")
          .build();

  /**
   * @param fs the filesystem of Alluxio
   */
  public RmCommand(FileSystem fs) {
    super(fs);
  }

  @Override
  public String getCommandName() {
    return "rm";
  }

  @Override
  protected int getNumOfArgs() {
    return 1;
  }

  @Override
  public Options getOptions() {
    return new Options()
        .addOption(RECURSIVE_OPTION)
        .addOption(REMOVE_UNCHECKED_OPTION)
        .addOption(REMOVE_ALLUXIO_ONLY);
  }

  @Override
  protected void runCommand(AlluxioURI path, CommandLine cl) throws AlluxioException, IOException {
    // TODO(calvin): Remove explicit state checking.
    boolean recursive = cl.hasOption("R");
    if (!mFileSystem.exists(path)) {
      throw new FileDoesNotExistException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(path));
    }
    if (!recursive && mFileSystem.getStatus(path).isFolder()) {
      throw new IOException(
          path.getPath() + " is a directory, to remove it, please use \"rm -R <path>\"");
    }

    DeleteOptions options = DeleteOptions.defaults().setRecursive(recursive);
    if (cl.hasOption(REMOVE_UNCHECKED_OPTION_CHAR)) {
      options.setUnchecked(true);
    }
    boolean isAlluxioOnly = cl.hasOption(REMOVE_ALLUXIO_ONLY.getOpt());
    if (isAlluxioOnly) {
      options.setAlluxioOnly(true);
    }
    mFileSystem.delete(path, options);
    if (!isAlluxioOnly) {
      System.out.println(path + " has been removed");
    } else {
      System.out.println(path + " has been removed from Alluxio space,"
          + " but still exists in the under storage.");
    }
  }

  @Override
  public String getUsage() {
    return "rm [-R] [-U] [-alluxioOnly] <path>";
  }

  @Override
  public String getDescription() {
    return "Removes the specified file. Specify -R to remove file or directory recursively."
        + " Specify -U to remove directories without checking UFS contents are in sync."
        + " Specify -alluxioOnly to remove data and metadata from Alluxio space,"
        + " but the data in under storage is not deleted.";
  }

  @Override
  public void validateArgs(String... args) throws InvalidArgumentException {
    if (args.length < 1) {
      throw new InvalidArgumentException(ExceptionMessage.INVALID_ARGS_NUM_INSUFFICIENT
          .getMessage(getCommandName(), 1, args.length));
    }
  }
}

@@ -1915,73 +1915,14 @@
 pace
-,%22%0A              + %22 but the data in under storage is not deleted
+ only,
 %22)%0A 
@@ -3170,70 +3170,46 @@
     
-if (isAlluxioOnly) %7B%0A      options.setAlluxioOnly(true);%0A    %7D
+options.setAlluxioOnly(isAlluxioOnly);
 %0A   
@@ -3412,62 +3412,8 @@
 ce,%22
-%0A          + %22 but still exists in the under storage.%22
 );%0A 
@@ -3804,33 +3804,33 @@
 d metadata from 
-A
+a
 lluxio space,%22%0A 
@@ -3829,69 +3829,14 @@
 pace
-,%22%0A        + %22 but the data in under storage is not deleted.%22
+ only.
 ;%0A  

address comments change output and style