package com.bares.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.bares.service.MesaService;
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.MesaDTO;
import com.bares.service.dto.MesaCriteria;
import com.bares.service.MesaQueryService;
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 Mesa.
 */
@RestController
@RequestMapping("/api")
public class MesaResource {

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

    private static final String ENTITY_NAME = "mesa";

    private final MesaService mesaService;

    private final MesaQueryService mesaQueryService;

    public MesaResource(MesaService mesaService, MesaQueryService mesaQueryService) {
        this.mesaService = mesaService;
        this.mesaQueryService = mesaQueryService;
    }

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

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

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

    /**
     * GET  /mesas/:id : get the "id" mesa.
     *
     * @param id the id of the mesaDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the mesaDTO, or with status 404 (Not Found)
     */
    @GetMapping("/mesas/{id}")
    @Timed
    public ResponseEntity<MesaDTO> getMesa(@PathVariable Long id) {
        log.debug("REST request to get Mesa : {}", id);
        Optional<MesaDTO> mesaDTO = mesaService.findOne(id);
        return ResponseUtil.wrapOrNotFound(mesaDTO);
    }

    /**
     * DELETE  /mesas/:id : delete the "id" mesa.
     *
     * @param id the id of the mesaDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/mesas/{id}")
    @Timed
    public ResponseEntity<Void> deleteMesa(@PathVariable Long id) {
        log.debug("REST request to delete Mesa : {}", id);
        mesaService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
