package com.netflix.spinnaker.gate.controllers;

import com.netflix.spinnaker.gate.services.CleanupService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/cleanup")
public class CleanupController {

  private CleanupService cleanupService;

  @Autowired
  public CleanupController(CleanupService cleanupService) {
    this.cleanupService = cleanupService;
  }

  @ApiOperation(value = "Opt out of clean up for a marked resource.", response = String.class)
  @RequestMapping(method = RequestMethod.GET,
                  value = "/resources/{namespace}/{resourceId}/optOut",
                  produces = "text/html")
  String optOut(@PathVariable String namespace,
                @PathVariable String resourceId) {
    Map markedResource = cleanupService.optOut(namespace, resourceId);
    if (markedResource.isEmpty()) {
      return getHtmlWithMessage(namespace, resourceId, "Resource does not exist. Please try a valid resource.");
    }

    return getHtmlWithMessage(namespace, resourceId, "Resource has been restored, and opted out of future deletion!");
  }

  @ApiOperation(value = "Get information about a marked resource.", response = String.class)
  @RequestMapping(method = RequestMethod.GET,
                  value = "/resources/{namespace}/{resourceId}",
                  produces = "text/html")
  String getMarkedResource(@PathVariable String namespace,
                        @PathVariable String resourceId) {
    Map markedResource = cleanupService.get(namespace, resourceId);
    if (markedResource.isEmpty()) {
      return getHtmlWithMessage(namespace, resourceId, "Resource does not exist. Please try a valid resource.");
    }
    return markedResource.toString();
  }

  // todo eb: expose once AWS gives us soft delete
//  @ApiOperation(value = "Restore a resource that has been soft deleted.", response = HashMap.class)
//  @RequestMapping(method = RequestMethod.PUT,
//                  value = "/resources/{namespace}/{resourceId}/restore",
//                  produces= "text/html")
  String restore(String namespace, String resourceId) {
    String status = cleanupService.restore(namespace, resourceId);
    if (status.equals("404")) {
      return getHtmlWithMessage(namespace, resourceId, "Resource does not exist. Please try a valid resource.");
    }

    return getHtmlWithMessage(namespace, resourceId, "Resource has been opted out!");
  }

  private String getHtmlWithMessage(String namespace, String resourceId, String message) {
    return "<body>\n" +
      "Swabbie Resource: [namespace=" + namespace + ", resourceId=" + resourceId + "] <br>\n" +
      "Message: " + message + "\n" +
      "</body>";
  }
}

@@ -791,36 +791,40 @@
 uces = %22
-text/html%22)%0A  String
+application/json%22)%0A  Map
  optOut(
@@ -1021,36 +1021,36 @@
       return get
-Html
+Json
 WithMessage(name
@@ -1139,36 +1139,36 @@
 %0A%0A    return get
-Html
+Json
 WithMessage(name
@@ -1491,36 +1491,40 @@
 uces = %22
-text/html%22)%0A  String
+application/json%22)%0A  Map
  getMark
@@ -1737,36 +1737,36 @@
       return get
-Html
+Json
 WithMessage(name
@@ -1877,19 +1877,8 @@
 urce
-.toString()
 ;%0A  
@@ -2201,22 +2201,19 @@
 tml%22)%0A  
-String
+Map
  restore
@@ -2359,36 +2359,36 @@
       return get
-Html
+Json
 WithMessage(name
@@ -2481,28 +2481,28 @@
   return get
-Html
+Json
 WithMessage(
@@ -2576,22 +2576,35 @@
 ate 
+Map%3C
 String
- getHtml
+, String%3E getJson
 With
@@ -2674,63 +2674,81 @@
     
-return %22%3Cbody%3E%5Cn%22 +%0A      %22Swabbie Resource: %5Bn
+Map%3CString, String%3E json = new HashMap();%0A%0A    json.put(%22swabbieN
 amespace
 =%22 +
@@ -2743,20 +2743,18 @@
 amespace
-=%22 +
+%22,
  namespa
@@ -2759,15 +2759,33 @@
 pace
- + %22, r
+);%0A    json.put(%22swabbieR
 esou
@@ -2793,12 +2793,10 @@
 ceId
-=%22 +
+%22,
  res
@@ -2806,51 +2806,42 @@
 ceId
- + %22%5D %3Cbr%3E%5Cn%22 +%0A      %22M
+);%0A    json.put(%22m
 essage
-: %22 +
+%22,
  message
  + %22
@@ -2840,33 +2840,26 @@
 sage
- + %22%5Cn%22 +%0A      %22%3C/body%3E%22
+);%0A    return json
 ;%0A  

return json not html (#666)