package tutorial.rest.mvc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tutorial.core.models.entities.ActiveIngredientRecomendation;
import tutorial.core.services.ActiveIngredientRecomendationService;
import tutorial.rest.resources.ActiveIngredientRecomendationResource;
import tutorial.rest.resources.asm.ActiveIngredientRecomendationResourceAsm;


/**
 * 
 * @author Xavier
 */
@Controller
@RequestMapping("/rest/activeIngredient-recomendations")
public class ActiveIngredientRecomendationController {
    private ActiveIngredientRecomendationService service;

    /**
     *
     * @param service
     */
    @Autowired
    public ActiveIngredientRecomendationController(ActiveIngredientRecomendationService service)
    {
        this.service = service;
    }

    /**
     * Returns the activeIngredientRecomendation with the given Id
     * @param activeIngredientRecomendationId the given Id
     * @return an activeIngredientRecomendation or HttpStatus.NOT_FOUND
     */
    @RequestMapping(value="/{activeIngredientRecomendationId}",
            method = RequestMethod.GET)
    @PreAuthorize("permitAll")
    public ResponseEntity<ActiveIngredientRecomendationResource> getActiveIngredientRecomendation(
            @PathVariable Long activeIngredientRecomendationId) {
        ActiveIngredientRecomendation recomendation = service.findActiveIngredientRecomendation(activeIngredientRecomendationId);
        if(recomendation != null)
        {
            ActiveIngredientRecomendationResource res = new ActiveIngredientRecomendationResourceAsm().toResource(recomendation);
            return new ResponseEntity<ActiveIngredientRecomendationResource>(res, HttpStatus.OK);
        } else {
            return new ResponseEntity<ActiveIngredientRecomendationResource>(HttpStatus.NOT_FOUND);
        }
    }

    /**
     * Deletes the activeIngredientRecomendation with the given Id
     * @param activeIngredientRecomendationId the given Id
     * @return 
     */
    /*@RequestMapping(value="/{activeIngredientRecomendationId}",
            method = RequestMethod.DELETE)
    @PreAuthorize("permitAll")
    public ResponseEntity<ActiveIngredientRecomendationResource> deleteActiveIngredientRecomendation(
            @PathVariable Long activeIngredientRecomendationId) {
        ActiveIngredientRecomendation recomendation = service.deleteActiveIngredientRecomendation(activeIngredientRecomendationId);
        if(recomendation != null)
        {
            ActiveIngredientRecomendationResource res = new ActiveIngredientRecomendationResourceAsm().toResource(recomendation);
            return new ResponseEntity<ActiveIngredientRecomendationResource>(res, HttpStatus.OK);
        } else {
            return new ResponseEntity<ActiveIngredientRecomendationResource>(HttpStatus.NOT_FOUND);
        }
    }*/

    /**
     * Updates a activeIngredientRecomendation with the new information sent in the request
     * @param activeIngredientRecomendationId the given Id
     * @param sentActiveIngredientRecomendation the new information for the activeIngredientRecomendation that has to be updated
     * @return the updated information for this activeIngredientRecomendation or HttpStatus.NOT_FOUND if there is no activeIngredientRecomendation for the given Id
     */
    @RequestMapping(value="/{activeIngredientRecomendationId}",
            method = RequestMethod.PUT)
    @PreAuthorize("permitAll")
    public ResponseEntity<ActiveIngredientRecomendationResource> updateActiveIngredientRecomendation(
            @PathVariable Long activeIngredientRecomendationId, @RequestBody ActiveIngredientRecomendationResource sentActiveIngredientRecomendation) {
        ActiveIngredientRecomendation updatedRecomendation = service.updateActiveIngredientRecomendation(activeIngredientRecomendationId, sentActiveIngredientRecomendation.toActiveIngredientRecomendation());
        if(updatedRecomendation != null)
        {
            ActiveIngredientRecomendationResource res = new ActiveIngredientRecomendationResourceAsm().toResource(updatedRecomendation);
            return new ResponseEntity<ActiveIngredientRecomendationResource>(res, HttpStatus.OK);
        } else {
            return new ResponseEntity<ActiveIngredientRecomendationResource>(HttpStatus.NOT_FOUND);
        }
    }
}
