package fr.treeptik.cloudunit.controller;

import java.util.List;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
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.ResponseBody;

import fr.treeptik.cloudunit.dto.CommandRequest;
import fr.treeptik.cloudunit.dto.HttpOk;
import fr.treeptik.cloudunit.dto.JsonResponse;
import fr.treeptik.cloudunit.exception.ServiceException;
import fr.treeptik.cloudunit.model.User;
import fr.treeptik.cloudunit.service.CommandService;
import fr.treeptik.cloudunit.utils.AuthentificationUtils;

@Controller
@RequestMapping("/application")
public class CommandController {

    private final Logger logger = LoggerFactory.getLogger(EnvironmentController.class);

    @Inject
    private AuthentificationUtils authentificationUtils;

    @Inject
    private CommandService commandService;

    @RequestMapping(value = "/{applicationName}/container/{containerId}/command", method = RequestMethod.POST)
    public @ResponseBody JsonResponse addCommand(@PathVariable String applicationName, @PathVariable String containerId,
                                                 @RequestBody CommandRequest commandRequest) throws ServiceException {
        logger.info("Add");
        User user = authentificationUtils.getAuthentificatedUser();
        try {
            commandService.addCommand(commandRequest, containerId, applicationName);
            return new HttpOk();
        } finally {
            authentificationUtils.allowUser(user);
        }
    }

    @RequestMapping(value = "/{applicationName}/container/{containerId}/command/{id}", method = RequestMethod.DELETE)
    public @ResponseBody JsonResponse deleteCommand(@PathVariable String applicationName,
                                                    @PathVariable String containerId, @PathVariable Integer id) throws ServiceException {
        logger.info("Delete");
        User user = authentificationUtils.getAuthentificatedUser();
        try {
            commandService.deleteCommand(id);

            return new HttpOk();
        } finally {
            authentificationUtils.allowUser(user);
        }
    }

    @RequestMapping(value = "/{applicationName}/container/{containerId}/command/{id}", method = RequestMethod.PUT)
    public @ResponseBody JsonResponse updateCommand(@PathVariable String applicationName,
                                                    @PathVariable String containerId, @PathVariable Integer id, @RequestBody CommandRequest commandRequest)
            throws ServiceException {
        logger.info("Update");
        User user = authentificationUtils.getAuthentificatedUser();
        try {
            commandService.updateCommand(commandRequest, containerId, containerId, id);
            return new HttpOk();
        } finally {
            authentificationUtils.allowUser(user);
        }
    }

    @RequestMapping(value = "/{applicationName}/container/{containerId}/command", method = RequestMethod.GET)
    public @ResponseBody List<CommandRequest> listCommandByImage(@PathVariable String applicationName,
                                                                 @PathVariable String containerId) throws ServiceException {
        logger.info("Load by container");
        User user = authentificationUtils.getAuthentificatedUser();
        try {
            List<CommandRequest> commandRequestList = commandService.listCommandByImage(applicationName, containerId);

            return commandRequestList;
        } finally {
            authentificationUtils.allowUser(user);
        }
    }

    @RequestMapping(value = "/{applicationName}/container/{containerId}/command/{id}", method = RequestMethod.POST)
    public @ResponseBody CommandRequest getCommand(@PathVariable String applicationName,
                                                   @PathVariable String containerId, @PathVariable Integer id) throws ServiceException {
        logger.info("Load by id");
        User user = authentificationUtils.getAuthentificatedUser();
        try {
            CommandRequest commandRequest = commandService.getCommand(id);

            return commandRequest;
        } finally {
            authentificationUtils.allowUser(user);
        }
    }
}

@@ -1204,34 +1204,36 @@
 ainer/%7Bcontainer
-Id
+Name
 %7D/command%22, meth
@@ -1374,18 +1374,20 @@
 ontainer
-Id
+Name
 ,%0A      
@@ -1672,18 +1672,20 @@
 ontainer
-Id
+Name
 , applic
@@ -1877,34 +1877,36 @@
 ainer/%7Bcontainer
-Id
+Name
 %7D/command/%7Bid%7D%22,
@@ -2101,34 +2101,36 @@
 String container
-Id
+Name
 , @PathVariable 
@@ -2508,34 +2508,36 @@
 ainer/%7Bcontainer
-Id
+Name
 %7D/command/%7Bid%7D%22,
@@ -2729,34 +2729,36 @@
 String container
-Id
+Name
 , @PathVariable 
@@ -3034,23 +3034,29 @@
 iner
-Id, containerId
+Name, applicationName
 , id
@@ -3239,34 +3239,36 @@
 ainer/%7Bcontainer
-Id
+Name
 %7D/command%22, meth
@@ -3489,18 +3489,20 @@
 ontainer
-Id
+Name
 ) throws
@@ -3759,18 +3759,20 @@
 ontainer
-Id
+Name
 );%0A%0A    
@@ -3962,18 +3962,20 @@
 ontainer
-Id
+Name
 %7D/comman
@@ -4186,10 +4186,12 @@
 iner
-Id
+Name
 , @P
@@ -4554,11 +4554,678 @@
  %7D%0A    %7D
+%0A%0A    @RequestMapping(value = %22/%7BapplicationName%7D/container/%7BcontainerName%7D/command/%7Bid%7D/exec%22, method = RequestMethod.POST)%0A    public @ResponseBody JsonResponse execCommand(@PathVariable String applicationName,%0A                                                  @PathVariable String containerName, @PathVariable Integer id) throws ServiceException %7B%0A        logger.info(%22Execute by id%22);%0A        User user = authentificationUtils.getAuthentificatedUser();%0A        try %7B%0A            commandService.execCommand(id, containerName, applicationName);%0A%0A            return new HttpOk();%0A        %7D finally %7B%0A            authentificationUtils.allowUser(user);%0A        %7D%0A    %7D
 %0A%7D%0A

REPLACE containerId by containerName in command feature