package com.hubspot.singularity.scheduler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.google.common.base.Optional;
import com.hubspot.baragon.models.UpstreamInfo;
import com.hubspot.singularity.LoadBalancerRequestType;
import com.hubspot.singularity.LoadBalancerRequestType.LoadBalancerRequestId;
import com.hubspot.singularity.SingularityRequest;
import com.hubspot.singularity.SingularityTask;
import com.hubspot.singularity.SingularityTaskId;
import com.hubspot.singularity.data.RequestManager;
import com.hubspot.singularity.data.TaskManager;
import com.hubspot.singularity.hooks.LoadBalancerClientImpl;

@Singleton
public class SingularityUpstreamChecker {

  private final LoadBalancerClientImpl lbClient;
  private final TaskManager taskManager;
  private final RequestManager requestManager;

  @Inject
  public SingularityUpstreamChecker(LoadBalancerClientImpl lbClient, TaskManager taskManager, RequestManager requestManager) {
    this.lbClient = lbClient;
    this.taskManager = taskManager;
    this.requestManager = requestManager;
  }

  private List<SingularityTask> getActiveSingularityTasksForRequest (String requestId) {
    List<SingularityTask> activeSingularityTasksForRequest = new ArrayList<>();
    for (SingularityTaskId taskId: taskManager.getActiveTaskIdsForRequest(requestId)){
      Optional<SingularityTask> maybeTask = taskManager.getTask(taskId);
      if (maybeTask.isPresent()) {
        activeSingularityTasksForRequest.add(maybeTask.get());
      }
    }
    return activeSingularityTasksForRequest;
  }

  private Collection<UpstreamInfo> getUpstreamsFromActiveTasks(String requestId, Optional<String> loadBalancerUpstreamGroup) {
    return lbClient.tasksToUpstreams(getActiveSingularityTasksForRequest(requestId), requestId, loadBalancerUpstreamGroup);
  }

  private void syncUpstreamsForService(SingularityRequest request, Optional<String> loadBalancerUpstreamGroup) {
    String requestId = request.getId();
    Collection<UpstreamInfo> upstreamsInBaragonForRequest = lbClient.getBaragonUpstreamsForRequest(requestId);
    Collection<UpstreamInfo> upstreamsInSingularityForRequest = getUpstreamsFromActiveTasks(requestId, loadBalancerUpstreamGroup);
    upstreamsInBaragonForRequest.removeAll(upstreamsInSingularityForRequest);
    LoadBalancerRequestId loadBalancerRequestId = new LoadBalancerRequestId(requestId, LoadBalancerRequestType.REMOVE, Optional.absent());
    for (UpstreamInfo upstream: upstreamsInBaragonForRequest){
      //TODO: remove the upstream from Baragon
    }
  }

  public void syncUpstreams() {
    // TODO: check through the active requests and run the method above

  }
}

@@ -447,24 +447,84 @@
 ityRequest;%0A
+import com.hubspot.singularity.SingularityRequestWithState;%0A
 import com.h
@@ -2005,33 +2005,21 @@
 ce(S
+tr
 ing
-ularityRequest
  request
 , Op
@@ -2014,16 +2014,18 @@
  request
+Id
 , Option
@@ -2068,48 +2068,8 @@
 ) %7B%0A
-    String requestId = request.getId();%0A
     
@@ -2747,16 +2747,529 @@
 od above
+%0A    for (SingularityRequestWithState singularityRequestWithState: requestManager.getActiveRequests())%7B%0A      SingularityRequest request = singularityRequestWithState.getRequest();%0A      if (request.isLoadBalanced()) %7B%0A        //TODO: lock on the requestId%0A        String requestId = singularityRequestWithState.getRequest().getId();%0A//        Optional%3CString%3E loadBalancerUpstreamGroup = request. TODO: get the loadBalancerUpstreamGroup%0A        syncUpstreamsForService(requestId,Optional.absent());%0A      %7D%0A    %7D
 %0A%0A  %7D%0A%7D%0A

add syncUpstreams to iterate through active requests