// 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.gerrit.extensions.api.changes.HashtagsInput;
import com.google.gerrit.extensions.common.AccountInfo;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.change.HashtagsUtil;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gerrit.server.validators.ValidationException;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

import java.io.IOException;
import java.util.HashSet;

class AddHashtagsStep {

  interface Factory {
    AddHashtagsStep create(Change change, ChangeInfo changeInfo, boolean resume);
  }

  private final HashtagsUtil hashtagsUtil;
  private final IdentifiedUser.GenericFactory genericUserFactory;
  private final ChangeControl.GenericFactory changeControlFactory;
  private final Change change;
  private final ChangeInfo changeInfo;
  private final boolean resume;

  @Inject
  AddHashtagsStep(HashtagsUtil hashtagsUtil,
      IdentifiedUser.GenericFactory genericUserFactory,
      ChangeControl.GenericFactory changeControlFactory,
      @Assisted Change change,
      @Assisted ChangeInfo changeInfo,
      @Assisted boolean resume) {
    this.hashtagsUtil = hashtagsUtil;
    this.change = change;
    this.changeInfo = changeInfo;
    this.genericUserFactory = genericUserFactory;
    this.changeControlFactory = changeControlFactory;
    this.resume = resume;
  }

  void add() throws IllegalArgumentException, AuthException, IOException,
      ValidationException, OrmException, NoSuchChangeException {
    ChangeControl ctrl = control(change, changeInfo.owner);

    if (resume) {
      HashtagsInput input = new HashtagsInput();
      input.remove = ctrl.getNotes().load().getHashtags();
      hashtagsUtil.setHashtags(ctrl, input, false, false);
    }

    HashtagsInput input = new HashtagsInput();
    input.add = new HashSet<>(changeInfo.hashtags);
    hashtagsUtil.setHashtags(ctrl, input, false, false);
  }

  private ChangeControl control(Change change, AccountInfo acc)
      throws NoSuchChangeException {
    return control(change, new Account.Id(acc._accountId));
  }

  private ChangeControl control(Change change, Account.Id id)
      throws NoSuchChangeException {
    return changeControlFactory.controlFor(change,
        genericUserFactory.create(id));
  }
}

@@ -720,64 +720,8 @@
 ut;%0A
-import com.google.gerrit.extensions.common.AccountInfo;%0A
 impo
@@ -771,16 +771,16 @@
 geInfo;%0A
+
 import c
@@ -834,58 +834,8 @@
 on;%0A
-import com.google.gerrit.reviewdb.client.Account;%0A
 impo
@@ -911,26 +911,23 @@
 .server.
-Identified
+Current
 User;%0Aim
@@ -1541,56 +1541,31 @@
 nal 
-IdentifiedUser.GenericFactory genericUserFactory
+CurrentUser currentUser
 ;%0A  
@@ -1797,56 +1797,31 @@
     
-IdentifiedUser.GenericFactory genericUserFactory
+CurrentUser currentUser
 ,%0A  
@@ -2090,47 +2090,33 @@
 his.
-genericUserFactory = genericUserFactory
+currentUser = currentUser
 ;%0A  
@@ -2362,23 +2362,47 @@
  ctrl = 
+changeControlFactory.
 control
+For
 (change,
@@ -2407,21 +2407,16 @@
 e, c
-hangeInfo.own
+urrentUs
 er);
@@ -2708,16 +2708,16 @@
 htags);%0A
+
     hash
@@ -2772,368 +2772,7 @@
 %0A  %7D
-%0A%0A  private ChangeControl control(Change change, AccountInfo acc)%0A      throws NoSuchChangeException %7B%0A    return control(change, new Account.Id(acc._accountId));%0A  %7D%0A%0A  private ChangeControl control(Change change, Account.Id id)%0A      throws NoSuchChangeException %7B%0A    return changeControlFactory.controlFor(change,%0A        genericUserFactory.create(id));%0A  %7D
 %0A%7D%0A

Use current user to add hashtags