package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBFunctionService;
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.TBFunctionDTO;
import vn.azteam.tpf.service.dto.TBFunctionCriteria;
import vn.azteam.tpf.service.TBFunctionQueryService;
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 TBFunction.
 */
@RestController
@RequestMapping("/api")
public class TBFunctionResource {

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

    private static final String ENTITY_NAME = "tBFunction";

    private final TBFunctionService tBFunctionService;

    private final TBFunctionQueryService tBFunctionQueryService;

    public TBFunctionResource(TBFunctionService tBFunctionService, TBFunctionQueryService tBFunctionQueryService) {
        this.tBFunctionService = tBFunctionService;
        this.tBFunctionQueryService = tBFunctionQueryService;
    }

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

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

    /**
     * GET  /tb-functions : get all the tBFunctions.
     *
     * @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 tBFunctions in body
     */
    @GetMapping("/tb-functions")
    @Timed
    public ResponseEntity<List<TBFunctionDTO>> getAllTBFunctions(TBFunctionCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBFunctions by criteria: {}", criteria);
        Page<TBFunctionDTO> page = tBFunctionQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-functions");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-functions/count : count all the tBFunctions.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-functions/count")
    @Timed
    public ResponseEntity<Long> countTBFunctions(TBFunctionCriteria criteria) {
        log.debug("REST request to count TBFunctions by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBFunctionQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-functions/:id : get the "id" tBFunction.
     *
     * @param id the id of the tBFunctionDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBFunctionDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-functions/{id}")
    @Timed
    public ResponseEntity<TBFunctionDTO> getTBFunction(@PathVariable Long id) {
        log.debug("REST request to get TBFunction : {}", id);
        Optional<TBFunctionDTO> tBFunctionDTO = tBFunctionService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBFunctionDTO);
    }

    /**
     * DELETE  /tb-functions/:id : delete the "id" tBFunction.
     *
     * @param id the id of the tBFunctionDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-functions/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBFunction(@PathVariable Long id) {
        log.debug("REST request to delete TBFunction : {}", id);
        tBFunctionService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
