// Copyright (C) 2015 The Android Open Source Project
//
// 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.googlesource.gerrit.plugins.importer;

import com.google.common.collect.Iterables;
import com.google.gerrit.extensions.client.ListChangesOption;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.CommentInfo;
import com.google.gerrit.extensions.common.GroupInfo;
import com.google.gerrit.extensions.common.ProjectInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.TopLevelResource;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.server.account.AccountResource;
import com.google.gerrit.server.account.AccountsCollection;
import com.google.gerrit.server.account.GetSshKeys;
import com.google.gerrit.server.account.GetSshKeys.SshKeyInfo;
import com.google.gerrit.server.change.ChangeResource;
import com.google.gerrit.server.change.ChangesCollection;
import com.google.gerrit.server.change.ListRevisionComments;
import com.google.gerrit.server.change.RevisionResource;
import com.google.gerrit.server.change.Revisions;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;

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

public class LocalApi implements GerritApi {
  private final com.google.gerrit.extensions.api.GerritApi gApi;
  private final ChangesCollection changes;
  private final Revisions revisions;
  private final ListRevisionComments listComments;
  private final AccountsCollection accounts;
  private final GetSshKeys getSshKeys;

  @Inject
  LocalApi(
      com.google.gerrit.extensions.api.GerritApi gApi,
      ChangesCollection changes,
      Revisions revisions,
      ListRevisionComments listComments,
      AccountsCollection accounts,
      GetSshKeys getSshKeys) {
    this.gApi = gApi;
    this.changes = changes;
    this.revisions = revisions;
    this.listComments = listComments;
    this.accounts = accounts;
    this.getSshKeys = getSshKeys;
  }

  @Override
  public ProjectInfo getProject(String projectName) throws IOException,
      BadRequestException {
    try {
      return gApi.projects().name(projectName).get();
    } catch (RestApiException e) {
      throw new BadRequestException(e.getMessage());
    }
  }

  @Override
  public List<ChangeInfo> queryChanges(String projectName) throws IOException,
      BadRequestException {
    try {
      return gApi.changes()
          .query("project:" + projectName)
          .withOptions(
              ListChangesOption.DETAILED_LABELS,
              ListChangesOption.DETAILED_ACCOUNTS,
              ListChangesOption.MESSAGES,
              ListChangesOption.CURRENT_REVISION,
              ListChangesOption.ALL_REVISIONS,
              ListChangesOption.ALL_COMMITS)
          .get();
    } catch (RestApiException e) {
      throw new BadRequestException(e.getMessage());
    }
  }

  @Override
  public GroupInfo getGroup(String groupName) throws IOException,
      BadRequestException, OrmException {
    try {
      return gApi.groups().id(groupName).get();
    } catch (RestApiException e) {
      throw new BadRequestException(e.getMessage());
    }
  }

  @Override
  public Iterable<CommentInfo> getComments(int changeId, String rev)
      throws IOException, OrmException, BadRequestException {
    try {
      ChangeResource changeRsrc = changes.parse(new Change.Id(changeId));
      RevisionResource revRsrc =
          revisions.parse(changeRsrc, IdString.fromDecoded(rev));
      Map<String, List<CommentInfo>> result = listComments.apply(revRsrc);

      for (Map.Entry<String, List<CommentInfo>> e : result.entrySet()) {
        for (CommentInfo i : e.getValue()) {
          i.path = e.getKey();
        }
      }

      return Iterables.concat(result.values());
    } catch (ResourceNotFoundException e) {
      return null;
    } catch (AuthException e) {
      throw new BadRequestException(e.getMessage());
    }
  }

  @Override
  public List<SshKeyInfo> getSshKeys(String userId) throws BadRequestException,
      IOException, OrmException {
    try {
      AccountResource rsrc =
          accounts.parse(TopLevelResource.INSTANCE,
              IdString.fromDecoded(userId));
      return getSshKeys.apply(rsrc);
    } catch (ResourceNotFoundException | AuthException e) {
      throw new BadRequestException(e.getMessage());
    }
  }

  @Override
  public Version getVersion() throws BadRequestException, IOException {
    return new Version(com.google.gerrit.common.Version.getVersion());
  }
}

@@ -1357,57 +1357,8 @@
 ce;%0A
-import com.google.gerrit.reviewdb.client.Change;%0A
 impo
@@ -1589,289 +1589,8 @@
 fo;%0A
-import com.google.gerrit.server.change.ChangeResource;%0Aimport com.google.gerrit.server.change.ChangesCollection;%0Aimport com.google.gerrit.server.change.ListRevisionComments;%0Aimport com.google.gerrit.server.change.RevisionResource;%0Aimport com.google.gerrit.server.change.Revisions;%0A
 impo
@@ -1853,139 +1853,8 @@
 pi;%0A
-  private final ChangesCollection changes;%0A  private final Revisions revisions;%0A  private final ListRevisionComments listComments;%0A
   pr
@@ -2021,287 +2021,88 @@
     
-ChangesCollection changes,%0A      Revisions revisions,%0A      ListRevisionComments listComments,%0A      AccountsCollection accounts,%0A      GetSshKeys getSshKeys) %7B%0A    this.gApi = gApi;%0A    this.changes = changes;%0A    this.revisions = revisions;%0A    this.listComments = listComments
+AccountsCollection accounts,%0A      GetSshKeys getSshKeys) %7B%0A    this.gApi = gApi
 ;%0A  
@@ -3508,247 +3508,109 @@
     
-ChangeResource changeRsrc = changes.parse(new Change.Id(changeId));%0A      RevisionResource revRsrc =%0A          revisions.parse(changeRsrc, IdString.fromDecoded(rev));%0A      Map%3CString, List%3CCommentInfo%3E%3E result = listComments.apply(revRsrc
+Map%3CString, List%3CCommentInfo%3E%3E result =%0A          gApi.changes().id(changeId).revision(rev).comments(
 );%0A%0A
@@ -3845,72 +3845,12 @@
 (Res
-ourceNotFoundException e) %7B%0A      return null;%0A    %7D catch (Auth
+tApi
 Exce

Use RevisionApi to retrieve comments