// Copyright (C) 2009 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.mail;

import com.google.gerrit.common.errors.EmailException;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

public class RegisterNewEmailSender extends OutgoingEmail {
  public interface Factory {
    public RegisterNewEmailSender create(String address);
  }

  private final String addr;

  @Inject
  public RegisterNewEmailSender(EmailArguments ea,
      @Assisted final String address) {
    super(ea, "registernewemail");
    addr = address;
  }

  @Override
  protected void init() throws EmailException {
    super.init();
    setHeader("Subject", "[Gerrit Code Review] Email Verification");
    add(RecipientType.TO, new Address(addr));
  }

  @Override
  protected boolean shouldSendMessage() {
    return true;
  }

  @Override
  protected void format() throws EmailException {
    appendText(velocifyFile("RegisterNewEmail.vm"));
  }
}

@@ -697,16 +697,64 @@
 eption;%0A
+import com.google.gerrit.server.IdentifiedUser;%0A
 import c
@@ -1001,81 +1001,261 @@
 nal 
-String addr;%0A%0A  @Inject%0A  public RegisterNewEmailSender(EmailArguments ea
+EmailTokenVerifier tokenVerifier;%0A  private final IdentifiedUser user;%0A  private final String addr;%0A  private String emailToken;%0A%0A  @Inject%0A  public RegisterNewEmailSender(EmailArguments ea,%0A      EmailTokenVerifier etv,%0A      IdentifiedUser callingUser
 ,%0A  
@@ -1323,24 +1323,73 @@
 newemail%22);%0A
+    tokenVerifier = etv;%0A    user = callingUser;%0A
     addr = a
@@ -1793,11 +1793,667 @@
 %22));%0A  %7D
+%0A%0A  public String getUserNameEmail() %7B%0A    String name = user.getAccount().getFullName();%0A    String email = user.getAccount().getPreferredEmail();%0A%0A    if (name != null && email != null) %7B%0A      return name + %22 %3C%22 + email + %22%3E%22;%0A    %7D else if (email != null) %7B%0A      return email;%0A    %7D else if (name != null) %7B%0A      return name;%0A    %7D else %7B%0A      String username = user.getUserName();%0A      if (username != null) %7B%0A        return username;%0A      %7D%0A    %7D%0A    return null;%0A  %7D%0A%0A  public String getEmailRegistrationToken() %7B%0A    if (emailToken == null) %7B%0A      emailToken = tokenVerifier.encode(user.getAccountId(), addr);%0A    %7D%0A    return emailToken;%0A  %7D
 %0A%7D%0A

Revert "Remove unused methods from RegisterNewEmailSender"