package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBFunctionGroupService;
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.TBFunctionGroupDTO;
import vn.azteam.tpf.service.dto.TBFunctionGroupCriteria;
import vn.azteam.tpf.service.TBFunctionGroupQueryService;
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 TBFunctionGroup.
 */
@RestController
@RequestMapping("/api")
public class TBFunctionGroupResource {

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

    private static final String ENTITY_NAME = "tBFunctionGroup";

    private final TBFunctionGroupService tBFunctionGroupService;

    private final TBFunctionGroupQueryService tBFunctionGroupQueryService;

    public TBFunctionGroupResource(TBFunctionGroupService tBFunctionGroupService, TBFunctionGroupQueryService tBFunctionGroupQueryService) {
        this.tBFunctionGroupService = tBFunctionGroupService;
        this.tBFunctionGroupQueryService = tBFunctionGroupQueryService;
    }

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

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

    /**
     * GET  /tb-function-groups : get all the tBFunctionGroups.
     *
     * @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 tBFunctionGroups in body
     */
    @GetMapping("/tb-function-groups")
    @Timed
    public ResponseEntity<List<TBFunctionGroupDTO>> getAllTBFunctionGroups(TBFunctionGroupCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBFunctionGroups by criteria: {}", criteria);
        Page<TBFunctionGroupDTO> page = tBFunctionGroupQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-function-groups");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-function-groups/count : count all the tBFunctionGroups.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-function-groups/count")
    @Timed
    public ResponseEntity<Long> countTBFunctionGroups(TBFunctionGroupCriteria criteria) {
        log.debug("REST request to count TBFunctionGroups by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBFunctionGroupQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-function-groups/:id : get the "id" tBFunctionGroup.
     *
     * @param id the id of the tBFunctionGroupDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBFunctionGroupDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-function-groups/{id}")
    @Timed
    public ResponseEntity<TBFunctionGroupDTO> getTBFunctionGroup(@PathVariable Long id) {
        log.debug("REST request to get TBFunctionGroup : {}", id);
        Optional<TBFunctionGroupDTO> tBFunctionGroupDTO = tBFunctionGroupService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBFunctionGroupDTO);
    }

    /**
     * DELETE  /tb-function-groups/:id : delete the "id" tBFunctionGroup.
     *
     * @param id the id of the tBFunctionGroupDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-function-groups/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBFunctionGroup(@PathVariable Long id) {
        log.debug("REST request to delete TBFunctionGroup : {}", id);
        tBFunctionGroupService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
