package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBCropTypeService;
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.TBCropTypeDTO;
import vn.azteam.tpf.service.dto.TBCropTypeCriteria;
import vn.azteam.tpf.service.TBCropTypeQueryService;
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 TBCropType.
 */
@RestController
@RequestMapping("/api")
public class TBCropTypeResource {

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

    private static final String ENTITY_NAME = "tBCropType";

    private final TBCropTypeService tBCropTypeService;

    private final TBCropTypeQueryService tBCropTypeQueryService;

    public TBCropTypeResource(TBCropTypeService tBCropTypeService, TBCropTypeQueryService tBCropTypeQueryService) {
        this.tBCropTypeService = tBCropTypeService;
        this.tBCropTypeQueryService = tBCropTypeQueryService;
    }

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

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

    /**
     * GET  /tb-crop-types : get all the tBCropTypes.
     *
     * @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 tBCropTypes in body
     */
    @GetMapping("/tb-crop-types")
    @Timed
    public ResponseEntity<List<TBCropTypeDTO>> getAllTBCropTypes(TBCropTypeCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBCropTypes by criteria: {}", criteria);
        Page<TBCropTypeDTO> page = tBCropTypeQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-crop-types");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

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

    /**
     * GET  /tb-crop-types/:id : get the "id" tBCropType.
     *
     * @param id the id of the tBCropTypeDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBCropTypeDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-crop-types/{id}")
    @Timed
    public ResponseEntity<TBCropTypeDTO> getTBCropType(@PathVariable Long id) {
        log.debug("REST request to get TBCropType : {}", id);
        Optional<TBCropTypeDTO> tBCropTypeDTO = tBCropTypeService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBCropTypeDTO);
    }

    /**
     * DELETE  /tb-crop-types/:id : delete the "id" tBCropType.
     *
     * @param id the id of the tBCropTypeDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-crop-types/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBCropType(@PathVariable Long id) {
        log.debug("REST request to delete TBCropType : {}", id);
        tBCropTypeService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
