package com.addicks.sendash.admin.api.rest;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.StandardServletEnvironment;

import com.addicks.sendash.admin.domain.Client;
import com.addicks.sendash.admin.service.IClientService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;

@RestController
@RequestMapping(value = ClientController.REQUEST_MAPPING)
@Api(value = "client", description = "CRUD client management.")
public class ClientController extends AbstractRestHandler {

  public static final String REQUEST_MAPPING = "/client";

  private static final Logger log = LoggerFactory.getLogger(ClientController.class);

  @Autowired
  private IClientService clientService;

  @Autowired
  private StandardServletEnvironment environment;

  public ClientController() {
    log.error("***************");
    log.error("Properties: ", environment.getSystemProperties());
    log.error("Environment", environment.getSystemEnvironment());
    log.error("PORT", environment.getProperty("PORT"));
    log.error("port", environment.getProperty("port"));
    log.error("***************");
  }

  @RequestMapping(value = "", method = RequestMethod.POST, consumes = { "application/json",
      "application/xml" }, produces = { "application/json", "application/xml" })
  @ResponseStatus(HttpStatus.CREATED)
  @ApiOperation(value = "Create a hotel resource.", notes = "Returns the URL of the new resource in the Location header.")
  public void createHotel(@RequestBody Client client, HttpServletRequest request,
      HttpServletResponse response) {
    Client createdClient = clientService.create(client);
    response.setHeader("Location",
        request.getRequestURL().append("/").append(createdClient.getId()).toString());
  }

  @RequestMapping(value = "", method = RequestMethod.GET, produces = { "application/json",
      "application/xml" })
  @ResponseStatus(HttpStatus.OK)
  @ApiOperation(value = "Get a paginated list of all users.", notes = "The list is paginated. You can provide a page number (default 0) and a page size (default 100)")
  public @ResponseBody List<Client> getAllCLients(
      @ApiParam(value = "The page number (zero-based)", required = true) @RequestParam(value = "_page", required = true, defaultValue = DEFAULT_PAGE_NUM) Integer page,
      @ApiParam(value = "Tha page size", required = true) @RequestParam(value = "_perPage", required = true, defaultValue = DEFAULT_PAGE_SIZE) Integer size,
      @ApiParam(value = "Tha page size", required = true) @RequestParam(value = "_sortDir", required = true, defaultValue = DEFAULT_SORT) String sortDir,
      @ApiParam(value = "Tha page size", required = true) @RequestParam(value = "_sortField", required = true, defaultValue = "email") String sortField,
      HttpServletRequest request, HttpServletResponse response) {

    Page<Client> clientPage = clientService.getAll(page, size);
    response.addHeader("X-Total-Count", "" + clientPage.getNumberOfElements());
    return clientPage.getContent();
  }

  @ApiResponses(value = {
      // @ApiResponse(code = 400, message = "Invalid ID supplied",
      // responseHeader = @ResponseHeader(name = "X-Rack-Cache", description =
      // "Explains whether or not a cache was used", response = Boolean.class)
      // ),
      @ApiResponse(code = 404, message = "Client not found") })
  @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = { "application/json",
      "application/xml" })
  @ResponseStatus(HttpStatus.NO_CONTENT)
  @ApiOperation(value = "Delete a client.", notes = "You have to provide a valid hotel ID in the URL. Once deleted the resource can not be recovered.")
  public void deleteClient(
      @ApiParam(value = "The ID of the existing hotel resource.", required = true) @PathVariable("id") Long id,
      HttpServletRequest request, HttpServletResponse response) {
    checkResourceFound(clientService.findById(id));
    clientService.delete(id);
  }
}

@@ -862,83 +862,8 @@
 ler;
-%0Aimport org.springframework.web.context.support.StandardServletEnvironment;
 %0A%0Aim
@@ -1619,419 +1619,8 @@
 e;%0A%0A
-  @Autowired%0A  private StandardServletEnvironment environment;%0A%0A  public ClientController() %7B%0A    log.error(%22***************%22);%0A    log.error(%22Properties: %22, environment.getSystemProperties());%0A    log.error(%22Environment%22, environment.getSystemEnvironment());%0A    log.error(%22PORT%22, environment.getProperty(%22PORT%22));%0A    log.error(%22port%22, environment.getProperty(%22port%22));%0A    log.error(%22***************%22);%0A  %7D%0A%0A
   @R

revert test