package org.pmiops.workbench.mailchimp;


import com.ecwid.maleorang.MailchimpClient;
import com.ecwid.maleorang.MailchimpException;
import com.ecwid.maleorang.MailchimpMethod;
import com.ecwid.maleorang.MailchimpObject;
import com.ecwid.maleorang.method.v3_0.lists.members.DeleteMemberMethod;
import com.ecwid.maleorang.method.v3_0.lists.members.EditMemberMethod.Create;
import com.ecwid.maleorang.method.v3_0.lists.members.GetMemberMethod;
import java.io.IOException;
import javax.inject.Provider;
import org.pmiops.workbench.google.CloudStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Encapsulate mailchimp API interaction details and provide a simple/mockable interface
 * for internal use.
 */
@Service
public class MailChimpServiceImpl implements MailChimpService {

  private final Provider<CloudStorageService> cloudStorageServiceProvider;
  private String apiKey = null;
  private String listId = null;

  @Autowired
  public MailChimpServiceImpl(Provider<CloudStorageService> cloudStorageServiceProvider) {
    this.cloudStorageServiceProvider = cloudStorageServiceProvider;
  }

  @Override
  public String addUserContactEmail(String contactEmail) {
    String userId;
    Create createRequest = new Create(getListId(), contactEmail);
    createRequest.status = MailChimpService.MAILCHIMP_PENDING;
    userId = executeMailChimpRequest(createRequest)
          .mapping.get(MailChimpService.MAILCHIMP_KEY_ID).toString();
    return userId;
  }

  @Override
  public void deleteUserContactEmail(String contactEmail) {
    DeleteMemberMethod delete = new DeleteMemberMethod(getListId(), contactEmail);
    executeMailChimpRequest(delete);
  }

  @Override
  public String getMember(String contactEmail) {
    String status;
    GetMemberMethod getMember = new GetMemberMethod(getListId(), contactEmail);
    status = executeMailChimpRequest(getMember)
        .mapping.get(MailChimpService.MAILCHIMP_KEY_STATUS).toString();
    return status;
  }

  // general function that handles MailChimp client creation and error handling
  private MailchimpObject executeMailChimpRequest(MailchimpMethod method) {
    if (apiKey == null) {
      apiKey = cloudStorageServiceProvider.get().readMailChimpApiKey();
    }
    MailchimpClient client = new MailchimpClient(apiKey);
    MailchimpObject result;

    try {
      result = client.execute(method);
    } catch (IOException | MailchimpException e) {
      throw new RuntimeException(e);
    }

    return result;
  }

  private String getListId() {
    if (listId == null) {
      listId = cloudStorageServiceProvider.get().readMailChimpListId();
    }
    return listId;
  }
}

@@ -2126,16 +2126,27 @@
 private 
+%3CR extends 
 Mailchim
@@ -2152,16 +2152,39 @@
 mpObject
+%3E MailchimpObject%0A     
  execute
@@ -2215,16 +2215,19 @@
 mpMethod
+%3CR%3E
  method)

Fix unchecked warning (#637)