package com.bares.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.bares.service.ArticuloService;
import com.bares.web.rest.errors.BadRequestAlertException;
import com.bares.web.rest.util.HeaderUtil;
import com.bares.web.rest.util.PaginationUtil;
import com.bares.service.dto.ArticuloDTO;
import com.bares.service.dto.ArticuloCriteria;
import com.bares.service.ArticuloQueryService;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Articulo.
 */
@RestController
@RequestMapping("/api")
public class ArticuloResource {

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

    private static final String ENTITY_NAME = "articulo";

    private final ArticuloService articuloService;

    private final ArticuloQueryService articuloQueryService;

    public ArticuloResource(ArticuloService articuloService, ArticuloQueryService articuloQueryService) {
        this.articuloService = articuloService;
        this.articuloQueryService = articuloQueryService;
    }

    /**
     * POST  /articulos : Create a new articulo.
     *
     * @param articuloDTO the articuloDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new articuloDTO, or with status 400 (Bad Request) if the articulo has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/articulos")
    @Timed
    public ResponseEntity<ArticuloDTO> createArticulo(@Valid @RequestBody ArticuloDTO articuloDTO) throws URISyntaxException {
        log.debug("REST request to save Articulo : {}", articuloDTO);
        if (articuloDTO.getId() != null) {
            throw new BadRequestAlertException("A new articulo cannot already have an ID", ENTITY_NAME, "idexists");
        }
        ArticuloDTO result = articuloService.save(articuloDTO);
        return ResponseEntity.created(new URI("/api/articulos/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /articulos : Updates an existing articulo.
     *
     * @param articuloDTO the articuloDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated articuloDTO,
     * or with status 400 (Bad Request) if the articuloDTO is not valid,
     * or with status 500 (Internal Server Error) if the articuloDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/articulos")
    @Timed
    public ResponseEntity<ArticuloDTO> updateArticulo(@Valid @RequestBody ArticuloDTO articuloDTO) throws URISyntaxException {
        log.debug("REST request to update Articulo : {}", articuloDTO);
        if (articuloDTO.getId() == null) {
            return createArticulo(articuloDTO);
        }
        ArticuloDTO result = articuloService.save(articuloDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, articuloDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /articulos : get all the articulos.
     *
     * @param pageable the pagination information
     * @param criteria the criterias which the requested entities should match
     * @return the ResponseEntity with status 200 (OK) and the list of articulos in body
     */
    @GetMapping("/articulos")
    @Timed
    public ResponseEntity<List<ArticuloDTO>> getAllArticulos(ArticuloCriteria criteria, Pageable pageable) {
        log.debug("REST request to get Articulos by criteria: {}", criteria);
        Page<ArticuloDTO> page = articuloQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/articulos");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /articulos/:id : get the "id" articulo.
     *
     * @param id the id of the articuloDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the articuloDTO, or with status 404 (Not Found)
     */
    @GetMapping("/articulos/{id}")
    @Timed
    public ResponseEntity<ArticuloDTO> getArticulo(@PathVariable Long id) {
        log.debug("REST request to get Articulo : {}", id);
        Optional<ArticuloDTO> articuloDTO = articuloService.findOne(id);
        return ResponseUtil.wrapOrNotFound(articuloDTO);
    }

    /**
     * DELETE  /articulos/:id : delete the "id" articulo.
     *
     * @param id the id of the articuloDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/articulos/{id}")
    @Timed
    public ResponseEntity<Void> deleteArticulo(@PathVariable Long id) {
        log.debug("REST request to delete Articulo : {}", id);
        articuloService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
