package com.hubspot.blazar.listener;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.hubspot.blazar.base.BuildTrigger;
import com.hubspot.blazar.base.CommitInfo;
import com.hubspot.blazar.base.RepositoryBuild;
import com.hubspot.blazar.base.visitor.RepositoryBuildVisitor;
import com.hubspot.blazar.config.BlazarConfiguration;
import com.hubspot.blazar.config.BlazarSlackConfiguration;
import com.hubspot.blazar.util.SlackUtils;
import com.ullink.slack.simpleslackapi.SlackSession;
import com.ullink.slack.simpleslackapi.SlackUser;

public class SlackImNotificationVisitor implements RepositoryBuildVisitor {
  private static final Logger LOG = LoggerFactory.getLogger(SlackImNotificationVisitor.class);
  private final BlazarSlackConfiguration blazarSlackConfig;

  private Optional<SlackSession> slackSession;
  private SlackUtils slackUtils;

  @Inject
  public SlackImNotificationVisitor (Optional<SlackSession> slackSession,
                                     BlazarConfiguration blazarConfiguration,
                                     SlackUtils slackUtils) {
    this.slackSession = slackSession;
    this.slackUtils = slackUtils;
    this.blazarSlackConfig = blazarConfiguration.getSlackConfiguration().get();
  }

  @Override
  public void visit(RepositoryBuild build) throws Exception {
    if (!build.getCommitInfo().isPresent() || !blazarSlackConfig.getImWhitelist().contains(build.getCommitInfo().get().getCurrent().getAuthor().getEmail())) {
      LOG.info("No commitinfo present, or user is not in whitelist");
      return;
    }
    boolean wasPush = build.getBuildTrigger().getType() == BuildTrigger.Type.PUSH;
    boolean wasBranchCreation = build.getBuildTrigger().getType() == BuildTrigger.Type.BRANCH_CREATION;
    if (!(wasPush || wasBranchCreation)) {
      LOG.info("Not sending messages for triggers other than push or branch creation at this time.");
      return;
    }

    if (!build.getState().isComplete() || build.getState().equals(RepositoryBuild.State.SUCCEEDED) || build.getState().equals(RepositoryBuild.State.CANCELLED) ) {
      LOG.info("Not sending notifications for builds in non-terminal states or on success / cancellation");
      return;
    }
    if (slackSession.isPresent()) {
      LOG.info("Sending slack notification for repo build {}", build.getId().get());
      CommitInfo commitInfo = build.getCommitInfo().get();
      SlackUser user = slackSession.get().findUserByEmail(commitInfo.getCurrent().getAuthor().getEmail());
      slackSession.get().sendMessageToUser(user, "", slackUtils.buildSlackAttachment(build));
    }
  }
}

@@ -2466,24 +2466,33 @@
 et();%0A      
+Optional%3C
 SlackUser us
@@ -2492,16 +2492,17 @@
 User
+%3E
  user = 
 slac
@@ -2497,16 +2497,38 @@
  user = 
+Optional.fromNullable(
 slackSes
@@ -2601,18 +2601,51 @@
 Email())
+)
 ;%0A
+      if (user.isPresent()) %7B%0A  
       sl
@@ -2683,16 +2683,22 @@
 ser(user
+.get()
 , %22%22, sl
@@ -2732,24 +2732,193 @@
 nt(build));%0A
+      %7D else %7B%0A        LOG.error(%22Could not find user %7B%7D for message about repo build %7B%7D%22, commitInfo.getCurrent().getAuthor().getEmail(), build.getId().get());%0A      %7D%0A
     %7D%0A  %7D%0A%7D%0A

make this safer against weird slack stuff