package de.nucantus;

import javax.ws.rs.*;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.logging.Logger;

/**
 * Song challenge Web service giving access to challenge data store.
 *
 * @author Andreas Rümpel <ruempel@gmail.com>
 * @since 2017-01-03
 */
@Path("challenges")
public class SongChallengeService {
    private static final Logger LOG = Logger.getLogger(SongChallengeService.class.getName());
    private ChallengeManager cm = ChallengeManager.getInstance();

    @POST
    @Consumes("application/x-www-form-urlencoded")
    public void challengeSong(@FormParam("song") String song, @FormParam("player") String playerName) {
        //TODO check whether song is already challenged before adding new challenge
        LOG.info("Challenge received for " + song + " from player " + playerName);
        cm.getOpenChallenges().add(new Challenge(song, playerName));
    }

    @PUT
    @Consumes("application/x-www-form-urlencoded")
    public void joinSong(@FormParam("song") String song, @FormParam("player") String playerName) {
        LOG.info("Challenge accepted for " + song + " from player " + playerName);
        for (Challenge challenge : cm.getOpenChallenges()) {
            if (challenge.getSong().equals(song)) {
                cm.getAcceptedChallenges().add(new Challenge(challenge, playerName));
                cm.getOpenChallenges().remove(challenge);
                break;
            }
        }
    }

    @GET
    public List<Challenge> getChallenges(@QueryParam("open") String open) {
        return "true".equalsIgnoreCase(open)? cm.getOpenChallenges() : cm.getAcceptedChallenges();
    }

    @DELETE
    @Path("/{song}")
    public void deleteChallenge(@PathParam("song") String song) throws UnsupportedEncodingException {
        String decodedSong = URLDecoder.decode(song, StandardCharsets.UTF_8.name());
        for (Challenge challenge : cm.getAcceptedChallenges()) {
            if (challenge.getSong().equals(decodedSong)) {
                LOG.info("Delete song from accepted list: " + decodedSong);
                cm.getAcceptedChallenges().remove(challenge);
                break;
            }
        }
    }
}
@@ -175,16 +175,43 @@
 l.List;%0A
+import java.util.Optional;%0A
 import j
@@ -235,16 +235,16 @@
 Logger;%0A
-
 %0A/**%0A * 
@@ -1250,37 +1250,41 @@
 rName);%0A        
-for (
+Optional%3C
 Challenge challe
@@ -1268,32 +1268,33 @@
 tional%3CChallenge
+%3E
  challenge : cm.
@@ -1284,25 +1284,25 @@
 %3E challenge 
-:
+=
  cm.getOpenC
@@ -1304,35 +1304,41 @@
 OpenChallenges()
-) %7B
+.stream()
 %0A            if 
@@ -1330,37 +1330,42 @@
             
-if (challenge
+    .filter(c -%3E c
 .getSong().e
@@ -1372,31 +1372,75 @@
 quals(song))
- %7B%0A    
+.findFirst();%0A        if (challenge.isPresent()) %7B%0A
             
@@ -1493,16 +1493,22 @@
 hallenge
+.get()
 , player
@@ -1511,28 +1511,24 @@
 ayerName));%0A
-    
             
@@ -1558,32 +1558,38 @@
 remove(challenge
+.get()
 );%0A             
@@ -1587,36 +1587,62 @@
     
-        break;%0A            %7D
+%7D else %7B%0A            // TODO no open challenge to join
 %0A   
@@ -1784,16 +1784,17 @@
 se(open)
+ 
 ? cm.get
@@ -2079,13 +2079,17 @@
     
-for (
+Optional%3C
 Chal
@@ -2093,16 +2093,17 @@
 hallenge
+%3E
  challen
@@ -2105,17 +2105,17 @@
 allenge 
-:
+=
  cm.getA
@@ -2137,75 +2137,130 @@
 es()
-) %7B%0A            if (challenge.getSong().equals(decodedSong)) %7B%0A    
+.stream()%0A                .filter(c -%3E c.getSong().equals(decodedSong)).findFirst();%0A        if (challenge.isPresent()) %7B%0A
     
@@ -2331,36 +2331,32 @@
 g);%0A            
-    
 cm.getAcceptedCh
@@ -2386,47 +2386,16 @@
 enge
-);%0A                break;%0A            %7D
+.get());
 %0A   
@@ -2407,9 +2407,10 @@
 %7D%0A    %7D%0A
-
 %7D
+%0A

Convert processing to streams