/*
 * 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.shell.command;

import alluxio.AlluxioURI;
import alluxio.client.block.AlluxioBlockStore;
import alluxio.client.file.FileSystem;
import alluxio.client.file.URIStatus;
import alluxio.exception.AlluxioException;

import org.apache.commons.cli.CommandLine;

import java.io.IOException;
import java.util.List;

import javax.annotation.concurrent.ThreadSafe;

/**
 * Displays the path's info.
 * If path is a directory, it displays the directory's info and all direct children's info.
 * If path is a file, it displays the file's all blocks info.
 */
@ThreadSafe
public final class FileInfoCommand extends WithWildCardPathCommand {
  /**
   * @param fs the filesystem of Alluxio
   */
  public FileInfoCommand(FileSystem fs) {
    super(fs);
  }

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

  /**
   * @param status the URIStatus to be print
   * @throws IOException if an I/O error occurs
   */
  public void printFileInfo(URIStatus status) throws IOException {
    System.out.println(status);
    System.out.println("Containing the following blocks: ");
    AlluxioBlockStore blockStore = AlluxioBlockStore.create();
    for (long blockId : status.getBlockIds()) {
      System.out.println(blockStore.getInfo(blockId));
    }
  }

  @Override
  protected void runCommand(AlluxioURI path, CommandLine cl) throws AlluxioException, IOException {
    URIStatus status = mFileSystem.getStatus(path);
    if (status.isFolder()) {
      List<URIStatus> childrens = mFileSystem.listStatus(path);
      System.out.println(path + " is a directory and have " + childrens.size() + " child");
      System.out.println("The directory's information as following: ");
      System.out.println(status);
      if (childrens.size() > 0) {
        System.out.println("The childrens' information as following: ");
        for (int i = 0; i < childrens.size(); i++) {
          System.out.println("");
          System.out.println("child " + (i + 1) + ": ");
          printFileInfo(childrens.get(i));
        }
      }
    } else {
      System.out.println(path + " is a file and the information as following: ");
      printFileInfo(status);
    }
  }

  @Override
  public String getUsage() {
    return "fileInfo <path>";
  }

  @Override
  public String getDescription() {
    return "Displays info for the specified path both file and directory.";
  }
}

@@ -776,16 +776,55 @@
 andLine;
+%0Aimport org.apache.commons.cli.Options;
 %0A%0Aimport
@@ -975,16 +975,33 @@
 irectory
+ and with -l args
 , it dis
@@ -1017,24 +1017,27 @@
  directory's
+%0A *
  info and al
@@ -1399,16 +1399,139 @@
 %22;%0A  %7D%0A%0A
+  @Override%0A  protected Options getOptions() %7B%0A    return new Options()%0A        .addOption(LIST_ALL_CHILDREN_OPTION);%0A  %7D%0A%0A
   /**%0A  
@@ -1630,21 +1630,22 @@
   */%0A  p
-ublic
+rivate
  void pr
@@ -1971,45 +1971,371 @@
 %0A%0A  
-@Override%0A  protec
+/**%0A   * Displays information for all the path with specified args.%0A   *%0A   * @param path The %7B@link AlluxioURI%7D path as the input of the command%0A   * @param listAllChildren Whether list all children of a directory%0A   * @throws AlluxioException when Alluxio exception occurs%0A   * @throws IOException when non-Alluxio exception occurs%0A   */%0A  priva
 te
-d
  void 
-runCommand
+fileInfo
 (All
@@ -2352,22 +2352,31 @@
 th, 
-CommandLine cl
+boolean listAllChildren
 ) th
@@ -2397,16 +2397,22 @@
 ception,
+%0A     
  IOExcep
@@ -2495,16 +2495,35 @@
 Folder()
+ && listAllChildren
 ) %7B%0A    
@@ -3105,16 +3105,189 @@
  else %7B%0A
+      if (status.isFolder()) %7B%0A        System.out.println(path + %22 is a directory and the information as following: %22);%0A        System.out.println(status);%0A      %7D else %7B%0A  
       Sy
@@ -3362,24 +3362,26 @@
 : %22);%0A      
+  
 printFileInf
@@ -3391,25 +3391,189 @@
 tatus);%0A    
-%7D
+  %7D%0A    %7D%0A  %7D%0A%0A  @Override%0A  protected void runCommand(AlluxioURI path, CommandLine cl) throws AlluxioException, IOException %7B%0A    fileInfo(path, cl.hasOption(%22l%22));
 %0A  %7D%0A%0A  @Ove
@@ -3627,16 +3627,21 @@
 fileInfo
+ %5B-l%5D
  %3Cpath%3E%22
@@ -3768,16 +3768,82 @@
 ectory.%22
+%0A        + %22 Specify -l to display directory's all children info.%22
 ;%0A  %7D%0A%7D%0A

Add -l options for user to displays dir's info