/*
Copyright 2014 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.google.cloud.genomics.api.client;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.MissingCommandException;
import com.beust.jcommander.internal.Lists;
import com.google.api.client.repackaged.com.google.common.base.Strings;
import com.google.api.client.util.Maps;
import com.google.cloud.genomics.api.client.commands.*;
import com.google.cloud.genomics.api.client.commands.align.AlignBamsCommand;
import com.google.cloud.genomics.api.client.commands.align.AlignInterleavedFastqsCommand;
import com.google.cloud.genomics.api.client.commands.align.AlignPairedFastqsCommand;

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

/**
 * Command line options handler for GenomicsSample
 */
class CommandLine {

  private JCommander parser;
  private BaseCommand command;
  private Map<String, BaseCommand> registeredCommands = Maps.newLinkedHashMap();

  public CommandLine() {
    parser = new JCommander();
    parser.setProgramName("genomics-tools-client-java");

    // The ordering of these commands is preserved in help messages
    addCommand("listdatasets", new ListDatasetsCommand());
    addCommand("getdataset", new GetDatasetsCommand());
    addCommand("createdataset", new CreateDatasetCommand());
    addCommand("updatedataset", new UpdateDatasetCommand());

    addCommand("searchreadgroupsets", new SearchReadGroupSetsCommand());
    addCommand("getreadgroupset", new GetReadGroupSetsCommand());

    addCommand("searchreads", new SearchReadsCommand());
    addCommand("importreads", new ImportReadsCommand());
    addCommand("exportreads", new ExportReadsCommand());

    addCommand("alignbam", new AlignBamsCommand());
    addCommand("alignfastq", new AlignInterleavedFastqsCommand());
    addCommand("alignpairedfastq", new AlignPairedFastqsCommand());
    addCommand("callvariants", new CallVariantsCommand());

    addCommand("getvariantset", new GetVariantSetsCommand());

    addCommand("searchvariants", new SearchVariantsCommand());
    addCommand("getvariant", new GetVariantsCommand());
    addCommand("importvariants", new ImportVariantsCommand());
    addCommand("exportvariants", new ExportVariantsCommand());

    addCommand("searchcallsets", new SearchCallSetsCommand());
    addCommand("updatecallset", new UpdateCallSetCommand());

    addCommand("listjobs", new ListJobsCommand());
    addCommand("getjob", new GetJobsCommand());
    addCommand("canceljob", new CancelJobCommand());

    // Custom escape hatch
    addCommand("custom", new CustomCommand());
  }

  private void addCommand(String name, BaseCommand command) {
    registeredCommands.put(name, command);
    parser.addCommand(name, command);
  }

  public BaseCommand getCommand() {
    return command;
  }

  public void setArgs(String[] args) {
    parser.parse(args);

    if (Strings.isNullOrEmpty(parser.getParsedCommand())) {
      throw new MissingCommandException("A command is required");
    } else {
      command = registeredCommands.get(parser.getParsedCommand());
    }
  }

  public void printHelp(String headline, Appendable out) throws IOException {
    out.append(headline).append("\n");
    if (Strings.isNullOrEmpty(parser.getParsedCommand())) {
      out.append("Valid commands are:\n");
      describeCommands(out);
      out.append("\n");
    } else {
      StringBuilder sb = new StringBuilder();
      parser.usage(parser.getParsedCommand(), sb);
      out.append(sb.toString());
    }
  }

  private void describeCommands(Appendable out) throws IOException {
    List<String> commands = Lists.newArrayList(registeredCommands.keySet());

    for (String command : commands) {
      out.append(String.format("%-20s%s%n", command, parser.getCommandDescription(command)));
    }
  }
}

@@ -1329,16 +1329,119 @@
 ndLine %7B
+%0A  private static final String JAR_COMMAND%0A      = %22java -jar genomics-tools-client-java-v1beta2.jar %22;
 %0A%0A  priv
@@ -1635,65 +1635,8 @@
 r();
-%0A    parser.setProgramName(%22genomics-tools-client-java%22);
 %0A%0A  
@@ -3779,63 +3779,198 @@
     
-if (Strings.isNullOrEmpty(parser.getParsedCommand())) %7B
+String parsedCommand = parser.getParsedCommand();%0A    if (Strings.isNullOrEmpty(parsedCommand)) %7B%0A      out.append(%22Usage: %22 + JAR_COMMAND + %22%3Ccommand%3E %3Coptions%3E%22)%0A          .append(%22%5Cn%5Cn%22);
 %0A   
@@ -4138,25 +4138,106 @@
 ser.
-usage(parser.getP
+getCommands().get(parsedCommand).setProgramName(JAR_COMMAND + parsedCommand);%0A      parser.usage(p
 arse
@@ -4244,18 +4244,16 @@
 dCommand
-()
 , sb);%0A 

Improve help messages with explicit usage