package com.faforever.api.featuredmods;

import com.faforever.api.data.domain.FeaturedMod;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.yahoo.elide.jsonapi.models.Data;
import com.yahoo.elide.jsonapi.models.JsonApiDocument;
import com.yahoo.elide.jsonapi.models.Resource;
import io.swagger.annotations.ApiOperation;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping(path = "/featuredMods")
public class FeaturedModsController {

  private final FeaturedModService featuredModService;

  public FeaturedModsController(FeaturedModService featuredModService) {
    this.featuredModService = featuredModService;
  }

  @Async
  @RequestMapping(path = "/{modId}/files/{version}")
  @ApiOperation("Lists the required files for a specific featured mod version")
  public CompletableFuture<JsonApiDocument> getFiles(@PathVariable("modId") int modId,
                                                     @PathVariable("version") String version,
                                                     @RequestParam("page[number]") Integer page) {
    Integer innerPage = Optional.ofNullable(page).orElse(0);
    if(innerPage > 1) {
      return CompletableFuture.completedFuture(new JsonApiDocument(new Data<>(Collections.emptyList())));
    }

    ImmutableMap<Integer, FeaturedMod> mods = Maps.uniqueIndex(featuredModService.getFeaturedMods(), FeaturedMod::getId);
    FeaturedMod featuredMod = mods.get(modId);

    Integer innerVersion = "latest".equals(version) ? null : Integer.valueOf(version);

    List<Resource> values = featuredModService.getFiles(featuredMod.getTechnicalName(), innerVersion).stream()
        .map(modFileMapper())
        .collect(Collectors.toList());

    return CompletableFuture.completedFuture(new JsonApiDocument(new Data<>(values)));
  }

  private Function<FeaturedModFile, Resource> modFileMapper() {
    return file -> new Resource("featuredModFile", String.valueOf(file.getId()),
        ImmutableMap.<String, Object>of(
            "group", file.getGroup(),
            "md5", file.getMd5(),
            "name", file.getName(),
            "url", file.getUrl(),
            "version", file.getVersion()
        ), null, null, null);
  }
}

@@ -1535,16 +1535,24 @@
 stParam(
+value = 
 %22page%5Bnu
@@ -1557,16 +1557,34 @@
 number%5D%22
+, required = false
 ) Intege
@@ -1660,16 +1660,17 @@
 ;%0A    if
+ 
 (innerPa
@@ -2167,18 +2167,16 @@
 )%0A      
-  
 .map(mod
@@ -2189,18 +2189,16 @@
 pper())%0A
-  
       .c
@@ -2470,18 +2470,16 @@
 ,%0A      
-  
 Immutabl
@@ -2503,20 +2503,16 @@
 ect%3Eof(%0A
-    
         
@@ -2545,20 +2545,16 @@
         
-    
 %22md5%22, f
@@ -2575,20 +2575,16 @@
         
-  
-  
 %22name%22, 
@@ -2595,28 +2595,24 @@
 .getName(),%0A
-    
         %22url
@@ -2637,20 +2637,16 @@
         
-    
 %22version
@@ -2666,18 +2666,16 @@
 rsion()%0A
-  
       ),

Make page[number] in /featuredMods optional