package it.reply.orchestrator.service.commands;

import it.reply.orchestrator.dto.RankCloudProvidersMessage;
import it.reply.orchestrator.dto.ranker.CloudProviderRankerRequest;
import it.reply.orchestrator.dto.ranker.Monitoring;
import it.reply.orchestrator.dto.ranker.RankedCloudProvider;
import it.reply.orchestrator.service.CloudProviderRankerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class GetProvidersRank extends BaseRankCloudProvidersCommand {

  @Autowired
  private CloudProviderRankerService cloudProviderRankerService;

  @Override
  protected RankCloudProvidersMessage
      customExecute(RankCloudProvidersMessage rankCloudProvidersMessage) {

    // Prepare Ranker's request
    List<Monitoring> monitoring =
        rankCloudProvidersMessage.getCloudProvidersMonitoringData().entrySet().stream()
            .map(e -> new Monitoring(e.getKey(), e.getValue())).collect(Collectors.toList());

    CloudProviderRankerRequest cprr =
        new CloudProviderRankerRequest()
            .withPreferences(rankCloudProvidersMessage.getSlamPreferences().getPreferences().get(0)
                .getPreferences())
            .withSla(rankCloudProvidersMessage.getSlamPreferences().getSla())
            .withMonitoring(monitoring);

    // FIXME Remove once the ranker is available
    // Get provider rank and save in message
    try {
      rankCloudProvidersMessage
          .setRankedCloudProviders(cloudProviderRankerService.getProviderRanking(cprr));
    } catch (Exception e2) {
      rankCloudProvidersMessage.setRankedCloudProviders(
          Arrays.asList(new RankedCloudProvider("Name1", 1, true, ""), new RankedCloudProvider(
              "STUB PROVIDER NAME -> Remove once the ranker is available", 0, true, "Error msg")));
    }

    return rankCloudProvidersMessage;
  }

}

@@ -231,70 +231,8 @@
 g;%0D%0A
-import it.reply.orchestrator.dto.ranker.RankedCloudProvider;%0D%0A
 impo
@@ -415,34 +415,8 @@
 %0D%0A%0D%0A
-import java.util.Arrays;%0D%0A
 impo
@@ -1370,58 +1370,8 @@
 %0D%0A%0D%0A
-    // FIXME Remove once the ranker is available%0D%0A
     
@@ -1416,21 +1416,8 @@
 ge%0D%0A
-    try %7B%0D%0A  
     
@@ -1439,26 +1439,24 @@
 ersMessage%0D%0A
-  
         .set
@@ -1534,301 +1534,8 @@
 ));%0D
-%0A    %7D catch (Exception e2) %7B%0D%0A      rankCloudProvidersMessage.setRankedCloudProviders(%0D%0A          Arrays.asList(new RankedCloudProvider(%22Name1%22, 1, true, %22%22), new RankedCloudProvider(%0D%0A              %22STUB PROVIDER NAME -%3E Remove once the ranker is available%22, 0, true, %22Error msg%22)));%0D%0A    %7D%0D
 %0A%0D%0A 

Remove CPRanker stub