package eu.bcvsolutions.idm.core.rest.impl;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.hateoas.Resources;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import eu.bcvsolutions.idm.core.api.rest.BaseEntityController;
import eu.bcvsolutions.idm.core.api.service.EntityLookupService;
import eu.bcvsolutions.idm.core.model.domain.CoreGroupPermission;
import eu.bcvsolutions.idm.core.model.domain.IdmScriptCategory;
import eu.bcvsolutions.idm.core.model.dto.filter.ScriptFilter;
import eu.bcvsolutions.idm.core.model.entity.IdmScript;

/**
 * Default controller for scripts, basic methods.
 * 
 * TODO: Secure read !
 * 
 * @author Ondrej Kopr <kopr@xyxy.cz>
 *
 */
@RepositoryRestController
@RequestMapping(value = BaseEntityController.BASE_PATH + "/scripts")
public class IdmScriptController extends DefaultReadWriteEntityController<IdmScript, ScriptFilter> {

	@Autowired
	public IdmScriptController(EntityLookupService entityLookupService) {
		super(entityLookupService);
	}
	
	@Override
	@ResponseBody
	@RequestMapping(method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('" + CoreGroupPermission.SCRIPT_READ + "')")
	public Resources<?> find(MultiValueMap<String, Object> parameters, Pageable pageable,
			PersistentEntityResourceAssembler assembler) {
		return super.find(parameters, pageable, assembler);
	}
	
	@Override
	@ResponseBody
	@RequestMapping(method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('" + CoreGroupPermission.SCRIPT_READ + "')")
	public Resources<?> findQuick(MultiValueMap<String, Object> parameters, Pageable pageable,
			PersistentEntityResourceAssembler assembler) {
		return super.findQuick(parameters, pageable, assembler);
	}
	
	@Override
	@ResponseBody
	@RequestMapping(method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('" + CoreGroupPermission.SCRIPT_READ + "')")
	public ResponseEntity<?> get(String backendId, PersistentEntityResourceAssembler assembler) {
		return super.get(backendId, assembler);
	}

	@Override
	@ResponseBody
	@PreAuthorize("hasAuthority('" + CoreGroupPermission.SCRIPT_CREATE + "') or hasAuthority('" + CoreGroupPermission.SCRIPT_UPDATE + "')")
	public ResponseEntity<?> post(HttpServletRequest nativeRequest, PersistentEntityResourceAssembler assembler)
			throws HttpMessageNotReadableException {
		return super.post(nativeRequest, assembler);
	}

	@Override
	@ResponseBody
	@PreAuthorize("hasAuthority('" + CoreGroupPermission.SCRIPT_DELETE + "')")
	public ResponseEntity<?> delete(@PathVariable @NotNull String backendId) {
		return super.delete(backendId);
	}

	@Override
	@ResponseBody
	@PreAuthorize("hasAuthority('" + CoreGroupPermission.SCRIPT_UPDATE + "')")
	public ResponseEntity<?> put(@PathVariable @NotNull String backendId, HttpServletRequest nativeRequest,
			PersistentEntityResourceAssembler assembler) throws HttpMessageNotReadableException {
		return super.put(backendId, nativeRequest, assembler);
	}

	@Override
	@ResponseBody
	@PreAuthorize("hasAuthority('" + CoreGroupPermission.SCRIPT_UPDATE + "')")
	public ResponseEntity<?> patch(@PathVariable @NotNull String backendId, HttpServletRequest nativeRequest,
			PersistentEntityResourceAssembler assembler) throws HttpMessageNotReadableException {
		return super.patch(backendId, nativeRequest, assembler);
	}

	@Override
	protected ScriptFilter toFilter(MultiValueMap<String, Object> parameters) {
		ScriptFilter filter = new ScriptFilter();
		filter.setText(getParameterConverter().toString(parameters, "text"));
		filter.setCategory(getParameterConverter().toEnum(parameters, "category", IdmScriptCategory.class));
		filter.setDescription(getParameterConverter().toString(parameters, "description"));
		return filter;
	}
}

@@ -387,24 +387,77 @@
 Controller;%0A
+import org.springframework.data.web.PageableDefault;%0A
 import org.s
@@ -490,16 +490,16 @@
 ources;%0A
-
 import o
@@ -911,16 +911,77 @@
 Method;%0A
+import org.springframework.web.bind.annotation.RequestParam;%0A
 import o
@@ -2028,16 +2028,30 @@
 ?%3E find(
+@RequestParam 
 MultiVal
@@ -2075,32 +2075,49 @@
 ect%3E parameters,
+ @PageableDefault
  Pageable pageab
@@ -2264,32 +2264,56 @@
 @RequestMapping(
+value= %22/search/quick%22, 
 method = Request
@@ -2429,16 +2429,30 @@
 ndQuick(
+@RequestParam 
 MultiVal
@@ -2484,16 +2484,33 @@
 ameters,
+ @PageableDefault
  Pageabl
@@ -2665,53 +2665,8 @@
 ody%0A
-%09@RequestMapping(method = RequestMethod.GET)%0A
 %09@Pr
@@ -2727,32 +2727,101 @@
 PT_READ + %22')%22)%0A
+%09@RequestMapping(value = %22/%7BbackendId%7D%22, method = RequestMethod.GET)%0A
 %09public Response
@@ -2834,16 +2834,39 @@
 %3C?%3E get(
+@PathVariable @NotNull 
 String b

Fix request mapping in scripts