package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBWaterTypeService;
import vn.azteam.tpf.web.rest.errors.BadRequestAlertException;
import vn.azteam.tpf.web.rest.util.HeaderUtil;
import vn.azteam.tpf.web.rest.util.PaginationUtil;
import vn.azteam.tpf.service.dto.TBWaterTypeDTO;
import vn.azteam.tpf.service.dto.TBWaterTypeCriteria;
import vn.azteam.tpf.service.TBWaterTypeQueryService;
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 java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

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

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

    private static final String ENTITY_NAME = "tBWaterType";

    private final TBWaterTypeService tBWaterTypeService;

    private final TBWaterTypeQueryService tBWaterTypeQueryService;

    public TBWaterTypeResource(TBWaterTypeService tBWaterTypeService, TBWaterTypeQueryService tBWaterTypeQueryService) {
        this.tBWaterTypeService = tBWaterTypeService;
        this.tBWaterTypeQueryService = tBWaterTypeQueryService;
    }

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

    /**
     * PUT  /tb-water-types : Updates an existing tBWaterType.
     *
     * @param tBWaterTypeDTO the tBWaterTypeDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated tBWaterTypeDTO,
     * or with status 400 (Bad Request) if the tBWaterTypeDTO is not valid,
     * or with status 500 (Internal Server Error) if the tBWaterTypeDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/tb-water-types")
    @Timed
    public ResponseEntity<TBWaterTypeDTO> updateTBWaterType(@RequestBody TBWaterTypeDTO tBWaterTypeDTO) throws URISyntaxException {
        log.debug("REST request to update TBWaterType : {}", tBWaterTypeDTO);
        if (tBWaterTypeDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        TBWaterTypeDTO result = tBWaterTypeService.save(tBWaterTypeDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, tBWaterTypeDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /tb-water-types : get all the tBWaterTypes.
     *
     * @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 tBWaterTypes in body
     */
    @GetMapping("/tb-water-types")
    @Timed
    public ResponseEntity<List<TBWaterTypeDTO>> getAllTBWaterTypes(TBWaterTypeCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBWaterTypes by criteria: {}", criteria);
        Page<TBWaterTypeDTO> page = tBWaterTypeQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-water-types");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-water-types/count : count all the tBWaterTypes.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-water-types/count")
    @Timed
    public ResponseEntity<Long> countTBWaterTypes(TBWaterTypeCriteria criteria) {
        log.debug("REST request to count TBWaterTypes by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBWaterTypeQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-water-types/:id : get the "id" tBWaterType.
     *
     * @param id the id of the tBWaterTypeDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBWaterTypeDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-water-types/{id}")
    @Timed
    public ResponseEntity<TBWaterTypeDTO> getTBWaterType(@PathVariable Long id) {
        log.debug("REST request to get TBWaterType : {}", id);
        Optional<TBWaterTypeDTO> tBWaterTypeDTO = tBWaterTypeService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBWaterTypeDTO);
    }

    /**
     * DELETE  /tb-water-types/:id : delete the "id" tBWaterType.
     *
     * @param id the id of the tBWaterTypeDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-water-types/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBWaterType(@PathVariable Long id) {
        log.debug("REST request to delete TBWaterType : {}", id);
        tBWaterTypeService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/tb-water-types?query=:query : search for the tBWaterType corresponding
     * to the query.
     *
     * @param query the query of the tBWaterType search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/tb-water-types")
    @Timed
    public ResponseEntity<List<TBWaterTypeDTO>> searchTBWaterTypes(@RequestParam String query, Pageable pageable) {
        log.debug("REST request to search for a page of TBWaterTypes for query {}", query);
        Page<TBWaterTypeDTO> page = tBWaterTypeService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/tb-water-types");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
