// Copyright (C) 2012 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.google.gerrit.server.change;

import static com.google.gerrit.reviewdb.client.ApprovalCategoryValue.formatValue;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gerrit.common.data.ApprovalType;
import com.google.gerrit.common.data.ApprovalTypes;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.common.data.PermissionRange;
import com.google.gerrit.common.data.SubmitRecord;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gerrit.server.workflow.CategoryFunction;
import com.google.gerrit.server.workflow.FunctionState;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public class ReviewerJson {
  private final Provider<ReviewDb> db;
  private final IdentifiedUser.GenericFactory userFactory;
  private final ApprovalTypes approvalTypes;
  private final FunctionState.Factory functionState;
  private final AccountInfo.Loader.Factory accountLoaderFactory;

  @Inject
  ReviewerJson(Provider<ReviewDb> db,
      IdentifiedUser.GenericFactory userFactory,
      ApprovalTypes approvalTypes,
      FunctionState.Factory functionState,
      AccountInfo.Loader.Factory accountLoaderFactory) {
    this.db = db;
    this.userFactory = userFactory;
    this.approvalTypes = approvalTypes;
    this.functionState = functionState;
    this.accountLoaderFactory = accountLoaderFactory;
  }

  public List<ReviewerInfo> format(Collection<ReviewerResource> rsrcs)
      throws OrmException {
    List<ReviewerInfo> infos = Lists.newArrayListWithCapacity(rsrcs.size());
    AccountInfo.Loader loader = accountLoaderFactory.create(true);
    for (ReviewerResource rsrc : rsrcs) {
      ReviewerInfo info = format(rsrc, null);
      loader.put(info);
      infos.add(info);
    }
    loader.fill();
    return infos;
  }

  public List<ReviewerInfo> format(ReviewerResource rsrc) throws OrmException {
    return format(ImmutableList.<ReviewerResource> of(rsrc));
  }

  public ReviewerInfo format(ReviewerInfo out, ChangeControl ctl,
      List<PatchSetApproval> approvals) throws OrmException {
    PatchSet.Id psId = ctl.getChange().currentPatchSetId();

    if (approvals == null) {
      approvals = db.get().patchSetApprovals()
          .byPatchSetUser(psId, out._id).toList();
    }

    FunctionState fs = functionState.create(ctl, psId, approvals);
    for (ApprovalType at : approvalTypes.getApprovalTypes()) {
      CategoryFunction.forCategory(at.getCategory()).run(at, fs);
    }

    out.approvals = Maps.newHashMapWithExpectedSize(approvals.size());
    for (PatchSetApproval ca : approvals) {
      for (PermissionRange pr : ctl.getLabelRanges()) {
        if (!pr.isEmpty()) {
          // TODO: Support arbitrary labels.
          ApprovalType at = approvalTypes.byId(ca.getCategoryId());
          if (at != null) {
            out.approvals.put(at.getCategory().getLabelName(),
                formatValue(ca.getValue())); }
        }
      }
    }

    // Add dummy approvals for all permitted labels for the user even if they
    // do not exist in the DB.
    ChangeData cd = new ChangeData(ctl);
    PatchSet ps = cd.currentPatchSet(db);
    if (ps != null) {
      for (SubmitRecord rec : ctl.canSubmit(db.get(), ps, cd, true, false)) {
        for (SubmitRecord.Label label : rec.labels) {
          String name = label.label;
          if (!out.approvals.containsKey(name)
              && !ctl.getRange(Permission.forLabel(name)).isEmpty()) {
            out.approvals.put(name, formatValue((short) 0));
          }
        }
      }
    }

    if (out.approvals.isEmpty()) {
      out.approvals = null;
    }

    return out;
  }

  private ReviewerInfo format(ReviewerResource rsrc,
      List<PatchSetApproval> approvals) throws OrmException {
    return format(new ReviewerInfo(rsrc.getUser().getAccountId()),
        rsrc.getUserControl(), approvals);
  }

  public static class ReviewerInfo extends AccountInfo {
    final String kind = "gerritcodereview#reviewer";
    Map<String, String> approvals;

    protected ReviewerInfo(Account.Id id) {
      super(id);
    }
  }

  public static class PostResult {
    public List<ReviewerInfo> reviewers;
    public String error;
    Boolean confirm;
  }
}

@@ -1327,56 +1327,8 @@
 Db;%0A
-import com.google.gerrit.server.IdentifiedUser;%0A
 impo
@@ -1378,16 +1378,16 @@
 ontrol;%0A
+
 import c
@@ -1811,67 +1811,8 @@
 db;%0A
-  private final IdentifiedUser.GenericFactory userFactory;%0A
   pr
@@ -2023,57 +2023,8 @@
 db,%0A
-      IdentifiedUser.GenericFactory userFactory,%0A
     
@@ -2150,24 +2150,24 @@
 rFactory) %7B%0A
+
     this.db 
@@ -2176,44 +2176,8 @@
 db;%0A
-    this.userFactory = userFactory;%0A
     

Remove unused userFactory member variable in ReviewerJson