package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBEnvironmentalParameterService;
import vn.azteam.tpf.service.dto.*;
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.TBEnvironmentalParameterQueryService;
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 TBEnvironmentalParameter.
 */
@RestController
@RequestMapping("/api")
public class TBEnvironmentalParameterResource {

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

    private static final String ENTITY_NAME = "tBEnvironmentalParameter";

    private final TBEnvironmentalParameterService tBEnvironmentalParameterService;

    private final TBEnvironmentalParameterQueryService tBEnvironmentalParameterQueryService;

    public TBEnvironmentalParameterResource(TBEnvironmentalParameterService tBEnvironmentalParameterService, TBEnvironmentalParameterQueryService tBEnvironmentalParameterQueryService) {
        this.tBEnvironmentalParameterService = tBEnvironmentalParameterService;
        this.tBEnvironmentalParameterQueryService = tBEnvironmentalParameterQueryService;
    }

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

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

    /**
     * GET  /tb-environmental-parameters : get all the tBEnvironmentalParameters.
     *
     * @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 tBEnvironmentalParameters in body
     */
    @GetMapping("/tb-environmental-parameters")
    @Timed
    public ResponseEntity<List<TBEnvironmentalParameterDTO>> getAllTBEnvironmentalParameters(TBEnvironmentalParameterCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBEnvironmentalParameters by criteria: {}", criteria);
        Page<TBEnvironmentalParameterDTO> page = tBEnvironmentalParameterQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-environmental-parameters");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-environmental-parameters/count : count all the tBEnvironmentalParameters.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-environmental-parameters/count")
    @Timed
    public ResponseEntity<Long> countTBEnvironmentalParameters(TBEnvironmentalParameterCriteria criteria) {
        log.debug("REST request to count TBEnvironmentalParameters by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBEnvironmentalParameterQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-environmental-parameters/:id : get the "id" tBEnvironmentalParameter.
     *
     * @param id the id of the tBEnvironmentalParameterDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBEnvironmentalParameterDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-environmental-parameters/{id}")
    @Timed
    public ResponseEntity<TBEnvironmentalParameterDTO> getTBEnvironmentalParameter(@PathVariable Long id) {
        log.debug("REST request to get TBEnvironmentalParameter : {}", id);
        Optional<TBEnvironmentalParameterDTO> tBEnvironmentalParameterDTO = tBEnvironmentalParameterService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBEnvironmentalParameterDTO);
    }

    /**
     * DELETE  /tb-environmental-parameters/:id : delete the "id" tBEnvironmentalParameter.
     *
     * @param id the id of the tBEnvironmentalParameterDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-environmental-parameters/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBEnvironmentalParameter(@PathVariable Long id) {
        log.debug("REST request to delete TBEnvironmentalParameter : {}", id);
        tBEnvironmentalParameterService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/tb-environmental-parameters?query=:query : search for the tBEnvironmentalParameter corresponding
     * to the query.
     *
     * @param query the query of the tBEnvironmentalParameter search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/tb-environmental-parameters")
    @Timed
    public ResponseEntity<List<TBEnvironmentalParameterDTO>> searchTBEnvironmentalParameters(@RequestParam String query, Pageable pageable) {
        log.debug("REST request to search for a page of TBEnvironmentalParameters for query {}", query);
        Page<TBEnvironmentalParameterDTO> page = tBEnvironmentalParameterService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/tb-environmental-parameters");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }
    @GetMapping("/getListEnvironmentalParameter")
    @Timed
    public ResponseEntity<List<ActivityObjectUpdateDetailDTO>> getListEnvironmentalParameter(TBEnvironmentalParameterCriteria criteria, Pageable pageable) {
        Page<TBEnvironmentalParameterDTO> page = tBEnvironmentalParameterQueryService.findByCriteria(criteria, pageable);
        List<ActivityObjectUpdateDetailDTO> result = tBEnvironmentalParameterQueryService.getListEnvironmentalParameter(page);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/getListEnvironmentalParameter");
        return ResponseEntity.ok().headers(headers).body(result);
    }
}
