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

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.api.services.genomics.Genomics;
import com.google.api.services.genomics.model.Dataset;
import com.google.api.services.genomics.model.SearchVariantsRequest;

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

@Parameters(commandDescription = "Search over variants")
public class SearchVariantsCommand extends SimpleCommand {

  @Parameter(names = "--dataset_id",
      description = "The Genomics API dataset ID to get variants for.",
      required = true)
  public String datasetId;

  @Parameter(names = "--callset_id",
      description = "Only return calls from these callsets.")
  public List<String> callsetIds;

  @Parameter(names = "--page_token",
      description = "The token used to retrieve additional pages in paginated API methods.")
  public String pageToken = "";

  @Parameter(names = { "--sequence_name", "--contig" },
      description = "The sequence name to query over (e.g. 'X', '23')",
      required = true)
  public String sequenceName;

  @Parameter(names = { "--sequence_start", "--start_position" },
      description = "The start position (1-based) of this query.",
      required = true)
  public Long sequenceStart;

  @Parameter(names = {"--sequence_end", "--end_position" },
      description = "The end position (1-based, inclusive) of this query.",
      required = true)
  public Long sequenceEnd;

  @Override
  public void handleRequest(Genomics genomics) throws IOException {
    Dataset dataset = getDataset(genomics, datasetId);
    if (dataset == null) {
      return;
    }
    System.out.println("Getting variants from: " + dataset.getName());

    SearchVariantsRequest request = new SearchVariantsRequest()
        .setDatasetId(datasetId)
        .setPageToken(pageToken)
        .setContig(sequenceName)
        .setStartPosition(sequenceStart)
        .setEndPosition(sequenceEnd);

    if (callsetIds != null) {
      request.setCallsetIds(callsetIds);
    }

    executeAndPrint(genomics.variants().search(request));
  }
}

@@ -707,16 +707,60 @@
 meters;%0A
+import com.beust.jcommander.internal.Lists;%0A
 import c
@@ -851,84 +851,9 @@
 del.
-Dataset;%0Aimport com.google.api.services.genomics.model.SearchVariantsRequest
+*
 ;%0A%0Ai
@@ -1314,16 +1314,210 @@
 etIds;%0A%0A
+  @Parameter(names = %22--callset_name%22,%0A      description = %22An alternative to callset_id, only return calls from callsets %22 +%0A          %22with these names.%22)%0A  public List%3CString%3E callsetNames;%0A%0A
   @Param
@@ -2475,24 +2475,212 @@
 etName());%0A%0A
+    if (callsetNames != null) %7B%0A      getCallsetsByName(genomics);%0A      if (callsetIds.isEmpty()) %7B%0A        // We couldn't find any valid callsets to query%0A        return;%0A      %7D%0A    %7D%0A%0A
     SearchVa
@@ -2984,24 +2984,24 @@
 ds);%0A    %7D%0A%0A
-
     executeA
@@ -3049,11 +3049,745 @@
 t));%0A  %7D
+%0A%0A  private void getCallsetsByName(Genomics genomics) throws IOException %7B%0A    List%3CString%3E datasetIds = Lists.newArrayList();%0A    datasetIds.add(datasetId);%0A%0A    if (callsetIds == null) %7B%0A      callsetIds = Lists.newArrayList();%0A    %7D%0A%0A    for (String name : callsetNames) %7B%0A      SearchCallsetsRequest request = new SearchCallsetsRequest()%0A          .setDatasetIds(datasetIds).setName(name);%0A      List%3CCallset%3E callsets = genomics.callsets().search(request).execute().getCallsets();%0A      if (callsets == null %7C%7C callsets.isEmpty()) %7B%0A        System.out.println(%22No callsets found with the name %22 + name);%0A        continue;%0A      %7D%0A      for (Callset callset : callsets) %7B%0A        callsetIds.add(callset.getId());%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

Add callset lookup by name in searchvariants