

package alluxio.cli.fs.command;

import alluxio.AlluxioURI;
import alluxio.Constants;
import alluxio.cli.CommandUtils;
import alluxio.client.file.FileInStream;
import alluxio.client.file.FileSystem;
import alluxio.client.file.URIStatus;
import alluxio.client.file.options.OpenFileOptions;
import alluxio.exception.AlluxioException;
import alluxio.exception.ExceptionMessage;
import alluxio.exception.status.InvalidArgumentException;
import alluxio.util.FormatUtils;

import com.google.common.base.Preconditions;
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;

/**
 * Prints the file's first n bytes (by default, 1KB) to the console.
 */
@ThreadSafe
public final class HeadCommand extends AbstractFileSystemCommand {
  private static final Option BYTES_OPTION = Option.builder("c")
      .required(false)
      .numberOfArgs(1)
      .desc("number of bytes (e.g., 1024, 4KB)")
      .build();

  private int mNumOfBytes;

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

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

  @Override
  public void validateArgs(CommandLine cl) throws InvalidArgumentException {
    CommandUtils.checkNumOfArgsEquals(this, cl, 1);
  }

  @Override
  protected void runPlainPath(AlluxioURI plainPath, CommandLine cl)
      throws AlluxioException, IOException {
    URIStatus status = mFileSystem.getStatus(plainPath);

    if (status.isFolder()) {
      throw new IOException(ExceptionMessage.PATH_MUST_BE_FILE.getMessage(plainPath));
    }
    OpenFileOptions options = OpenFileOptions.defaults();
    try (FileInStream is = mFileSystem.openFile(plainPath, options)) {
      long bytesToRead;
      if (status.getLength() > mNumOfBytes) {
        bytesToRead = mNumOfBytes;
      } else {
        bytesToRead = status.getLength();
      }

      byte[] buf = new byte[(int) bytesToRead];
      int read = is.read(buf);
      if (read != -1) {
        System.out.write(buf, 0, read);
      }
    }
  }

  @Override
  public int run(CommandLine cl) throws AlluxioException, IOException {
    String[] args = cl.getArgs();
    mNumOfBytes = Constants.KB;
    if (cl.hasOption('c')) {
      mNumOfBytes = (int) FormatUtils.parseSpaceSize(cl.getOptionValue('c'));
      Preconditions.checkArgument(mNumOfBytes > 0, "specified bytes must be > 0");
    }
    AlluxioURI path = new AlluxioURI(args[0]);
    runWildCardCmd(path, cl);

    return 0;
  }

  @Override
  public String getUsage() {
    return "head [-c <bytes>] <path>";
  }

  @Override
  public String getDescription() {
    return "Prints the file's first n bytes (by default, 1KB) to the console.";
  }

  @Override
  public Options getOptions() {
    return new Options().addOption(BYTES_OPTION);
  }
}
