package com.bares.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.bares.service.PropietarioService;
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.PropietarioDTO;
import com.bares.service.dto.PropietarioCriteria;
import com.bares.service.PropietarioQueryService;
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 Propietario.
 */
@RestController
@RequestMapping("/api")
public class PropietarioResource {

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

    private static final String ENTITY_NAME = "propietario";

    private final PropietarioService propietarioService;

    private final PropietarioQueryService propietarioQueryService;

    public PropietarioResource(PropietarioService propietarioService, PropietarioQueryService propietarioQueryService) {
        this.propietarioService = propietarioService;
        this.propietarioQueryService = propietarioQueryService;
    }

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

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

    /**
     * GET  /propietarios : get all the propietarios.
     *
     * @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 propietarios in body
     */
    @GetMapping("/propietarios")
    @Timed
    public ResponseEntity<List<PropietarioDTO>> getAllPropietarios(PropietarioCriteria criteria, Pageable pageable) {
        log.debug("REST request to get Propietarios by criteria: {}", criteria);
        Page<PropietarioDTO> page = propietarioQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/propietarios");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /propietarios/:id : get the "id" propietario.
     *
     * @param id the id of the propietarioDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the propietarioDTO, or with status 404 (Not Found)
     */
    @GetMapping("/propietarios/{id}")
    @Timed
    public ResponseEntity<PropietarioDTO> getPropietario(@PathVariable Long id) {
        log.debug("REST request to get Propietario : {}", id);
        Optional<PropietarioDTO> propietarioDTO = propietarioService.findOne(id);
        return ResponseUtil.wrapOrNotFound(propietarioDTO);
    }

    /**
     * DELETE  /propietarios/:id : delete the "id" propietario.
     *
     * @param id the id of the propietarioDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/propietarios/{id}")
    @Timed
    public ResponseEntity<Void> deletePropietario(@PathVariable Long id) {
        log.debug("REST request to delete Propietario : {}", id);
        propietarioService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
