package top.quantic.sentry.health;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
import top.quantic.sentry.domain.GameServer;
import top.quantic.sentry.service.GameServerService;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class GameServerHealthIndicator implements HealthIndicator {

    private final GameServerService gameServerService;

    @Autowired
    public GameServerHealthIndicator(GameServerService gameServerService) {
        this.gameServerService = gameServerService;
    }

    @Override
    public Health health() {
        /*
        Perform a series of checks:
        - Servers must be up (0 < delay < 1000 and State == GOOD)
        - Servers must have the latest version
        - Servers must have a valid RCON password (non-null)
         */
        Map<String, Object> details = new LinkedHashMap<>();
        List<GameServer> unresponsive = gameServerService.findUnhealthyServers();
        if (!unresponsive.isEmpty()) {
            details.put("unresponsive", unresponsive.stream()
                .map(GameServer::toString)
                .collect(Collectors.toList()));
        }
        List<GameServer> outdated = gameServerService.findOutdatedServers();
        if (!outdated.isEmpty()) {
            details.put("outdated", outdated.stream()
                .map(GameServer::toString)
                .collect(Collectors.toList()));
        }
        List<GameServer> missingRcon = gameServerService.findServersWithoutRcon();
        if (!missingRcon.isEmpty()) {
            details.put("missingRcon", missingRcon.stream()
                .map(GameServer::toString)
                .collect(Collectors.toList()));
        }
        if (details.isEmpty()) {
            Health.Builder builder = Health.up();
            gameServerService.getSummary().forEach(builder::withDetail);
            return builder.build();
        } else {
            Health.Builder builder = Health.down();
            details.forEach(builder::withDetail);
            return builder.build();
        }
    }
}

@@ -1921,32 +1921,64 @@
 ()));%0A        %7D%0A
+        Health.Builder builder;%0A
         if (deta
@@ -2002,39 +2002,24 @@
             
-Health.Builder 
 builder = He
@@ -2106,44 +2106,8 @@
 l);%0A
-            return builder.build();%0A
     
@@ -2127,39 +2127,24 @@
             
-Health.Builder 
 builder = He
@@ -2152,12 +2152,20 @@
 lth.
-down
+outOfService
 ();%0A
@@ -2214,24 +2214,30 @@
 etail);%0A    
+    %7D%0A
         retu
@@ -2256,26 +2256,16 @@
 uild();%0A
-        %7D%0A
     %7D%0A%7D%0A

Update health indicator