package com.bares.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.bares.service.ComandaService;
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.ComandaDTO;
import com.bares.service.dto.ComandaCriteria;
import com.bares.service.ComandaQueryService;
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 Comanda.
 */
@RestController
@RequestMapping("/api")
public class ComandaResource {

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

    private static final String ENTITY_NAME = "comanda";

    private final ComandaService comandaService;

    private final ComandaQueryService comandaQueryService;

    public ComandaResource(ComandaService comandaService, ComandaQueryService comandaQueryService) {
        this.comandaService = comandaService;
        this.comandaQueryService = comandaQueryService;
    }

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

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

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

    /**
     * GET  /comandas/:id : get the "id" comanda.
     *
     * @param id the id of the comandaDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the comandaDTO, or with status 404 (Not Found)
     */
    @GetMapping("/comandas/{id}")
    @Timed
    public ResponseEntity<ComandaDTO> getComanda(@PathVariable Long id) {
        log.debug("REST request to get Comanda : {}", id);
        Optional<ComandaDTO> comandaDTO = comandaService.findOne(id);
        return ResponseUtil.wrapOrNotFound(comandaDTO);
    }

    /**
     * DELETE  /comandas/:id : delete the "id" comanda.
     *
     * @param id the id of the comandaDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/comandas/{id}")
    @Timed
    public ResponseEntity<Void> deleteComanda(@PathVariable Long id) {
        log.debug("REST request to delete Comanda : {}", id);
        comandaService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
