package org.carlspring.strongbox.controllers;

import org.carlspring.strongbox.authentication.registry.AuthenticatorsRegistry;
import org.carlspring.strongbox.authentication.registry.support.AuthenticatorsScanner;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author Przemyslaw Fusik
 * @author Pablo Tirado
 */
@RestController
@PreAuthorize("hasAuthority('ADMIN')")
@RequestMapping(value = "/configuration/authenticators")
@Api(value = "/configuration/authenticators")
public class AuthenticatorsConfigController
        extends BaseController
{

    static final String SUCCESSFUL_REORDER = "Reorder succeeded.";
    static final String FAILED_REORDER = "Could not reorder authenticators registry.";

    static final String SUCCESSFUL_RELOAD = "Reload succeeded.";
    static final String FAILED_RELOAD = "Could not reload authenticators registry.";

    private final AuthenticatorsRegistry authenticatorsRegistry;

    private final AuthenticatorsScanner authenticatorsScanner;

    public AuthenticatorsConfigController(AuthenticatorsRegistry authenticatorsRegistry,
                                          AuthenticatorsScanner authenticatorsScanner)
    {
        this.authenticatorsRegistry = authenticatorsRegistry;
        this.authenticatorsScanner = authenticatorsScanner;
    }

    @ApiOperation(value = "Enumerates ordered collection of authenticators with order number and name")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "The list was returned successfully.") })
    @GetMapping(value = "/",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity list()
    {
        return ResponseEntity.ok(authenticatorsRegistry);
    }

    @ApiOperation(value = "Reorders authenticators by their indexes")
    @ApiResponses(value = { @ApiResponse(code = 200, message = SUCCESSFUL_REORDER),
                            @ApiResponse(code = 400, message = FAILED_REORDER) })
    @PutMapping(value = "/reorder/{first}/{second}",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = { MediaType.TEXT_PLAIN_VALUE,
                         MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity reorder(@PathVariable int first,
                                  @PathVariable int second,
                                  @RequestHeader(HttpHeaders.ACCEPT) String acceptHeader)
    {
        try
        {
            authenticatorsRegistry.reorder(first, second);
            return getSuccessfulResponseEntity(SUCCESSFUL_REORDER, acceptHeader);
        }
        catch (Exception e)
        {
            return getBadRequestResponseEntity(FAILED_REORDER, acceptHeader);
        }
    }

    @ApiOperation(value = "Reloads authenticators registry")
    @ApiResponses(value = { @ApiResponse(code = 200, message = SUCCESSFUL_RELOAD),
                            @ApiResponse(code = 400, message = FAILED_RELOAD) })
    @PutMapping(value = "/reload",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = { MediaType.TEXT_PLAIN_VALUE,
                         MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity reload(@RequestHeader(HttpHeaders.ACCEPT) String acceptHeader)
    {
        try
        {
            authenticatorsScanner.scanAndReloadRegistry();
            return getSuccessfulResponseEntity(SUCCESSFUL_RELOAD, acceptHeader);
        }
        catch (Exception e)
        {
            return getBadRequestResponseEntity(FAILED_RELOAD, acceptHeader);
        }
    }
}

@@ -419,16 +419,60 @@
 eaders;%0A
+import org.springframework.http.HttpStatus;%0A
 import o
@@ -1955,32 +1955,36 @@
 /%22,%0A            
+    
 consumes = Media
@@ -2004,32 +2004,36 @@
 ION_JSON_VALUE,%0A
+    
             prod
@@ -2470,32 +2470,36 @@
 %7D%22,%0A            
+    
 consumes = Media
@@ -2531,32 +2531,36 @@
 UE,%0A            
+    
 produces = %7B Med
@@ -2576,32 +2576,36 @@
 XT_PLAIN_VALUE,%0A
+    
                 
@@ -3093,34 +3093,33 @@
   return get
-BadRequest
+Exception
 ResponseEnti
@@ -3113,32 +3113,56 @@
 nResponseEntity(
+HttpStatus.BAD_REQUEST, 
 FAILED_REORDER, 
@@ -3152,32 +3152,35 @@
  FAILED_REORDER,
+ e,
  acceptHeader);%0A
@@ -3380,33 +3380,33 @@
 Response(code = 
-4
+5
 00, message = FA
@@ -3460,32 +3460,36 @@
 d%22,%0A            
+    
 consumes = Media
@@ -3521,32 +3521,36 @@
 UE,%0A            
+ 
+   
 produces = %7B Med
@@ -3566,32 +3566,36 @@
 XT_PLAIN_VALUE,%0A
+    
                 
@@ -3970,18 +3970,17 @@
  get
-BadRequest
+Exception
 Resp
@@ -3990,16 +3990,50 @@
 eEntity(
+HttpStatus.INTERNAL_SERVER_ERROR, 
 FAILED_R
@@ -4030,32 +4030,35 @@
 , FAILED_RELOAD,
+ e,
  acceptHeader);%0A

Fix cosmetic changes.