package com.hubspot.blazar.listener;

import com.hubspot.blazar.base.CommitInfo;
import com.hubspot.blazar.base.Module;
import com.hubspot.blazar.base.RepositoryBuild;
import com.hubspot.blazar.base.RepositoryBuild.State;
import com.hubspot.blazar.base.listener.RepositoryBuildListener;
import com.hubspot.blazar.data.service.ModuleBuildService;
import com.hubspot.blazar.data.service.ModuleService;
import com.hubspot.blazar.data.service.RepositoryBuildService;
import com.hubspot.blazar.util.GitHubHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.nio.file.FileSystems;
import java.util.HashSet;
import java.util.Set;

@Singleton
public class LaunchingRepositoryBuildListener implements RepositoryBuildListener {
  private static final Logger LOG = LoggerFactory.getLogger(LaunchingRepositoryBuildListener.class);

  private final RepositoryBuildService repositoryBuildService;
  private final ModuleBuildService moduleBuildService;
  private final ModuleService moduleService;
  private final GitHubHelper gitHubHelper;

  @Inject
  public LaunchingRepositoryBuildListener(RepositoryBuildService repositoryBuildService,
                                          ModuleBuildService moduleBuildService,
                                          ModuleService moduleService,
                                          GitHubHelper gitHubHelper) {
    this.repositoryBuildService = repositoryBuildService;
    this.moduleBuildService = moduleBuildService;
    this.moduleService = moduleService;
    this.gitHubHelper = gitHubHelper;
  }

  @Override
  public void buildChanged(RepositoryBuild build) {
    LOG.info("Going to enqueue module builds for repository build {}", build.getId().get());

    Set<Module> modules = moduleService.getByBranch(build.getBranchId());
    Set<Module> toBuild = findModulesToBuild(build.getCommitInfo().get(), modules);

    for (Module module : toBuild) {
      moduleBuildService.enqueue(build, module);
    }

    repositoryBuildService.update(build.withState(State.IN_PROGRESS));
  }

  private Set<Module> findModulesToBuild(CommitInfo commitInfo, Set<Module> modules) {
    final Set<Module> toBuild = new HashSet<>();
    if (commitInfo.isTruncated()) {
      toBuild.addAll(modules);
    } else {
      for (String path : gitHubHelper.affectedPaths(commitInfo)) {
        for (Module module : modules) {
          if (module.contains(FileSystems.getDefault().getPath(path))) {
            toBuild.add(module);
          }
        }
      }
    }

    return toBuild;
  }
}

@@ -1930,24 +1930,184 @@
  modules);%0A%0A
+    if (toBuild.isEmpty()) %7B%0A      repositoryBuildService.update(build.withState(State.SUCCEEDED).withEndTimestamp(System.currentTimeMillis()));%0A    %7D else %7B%0A  
     for (Mod
@@ -2126,24 +2126,26 @@
  toBuild) %7B%0A
+  
       module
@@ -2181,27 +2181,31 @@
 odule);%0A    
+  
 %7D%0A%0A
+  
     reposito
@@ -2263,16 +2263,22 @@
 RESS));%0A
+    %7D%0A
   %7D%0A%0A  p

Return success if nothing to build