package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBPermissionService;
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.TBPermissionDTO;
import vn.azteam.tpf.service.dto.TBPermissionCriteria;
import vn.azteam.tpf.service.TBPermissionQueryService;
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 TBPermission.
 */
@RestController
@RequestMapping("/api")
public class TBPermissionResource {

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

    private static final String ENTITY_NAME = "tBPermission";

    private final TBPermissionService tBPermissionService;

    private final TBPermissionQueryService tBPermissionQueryService;

    public TBPermissionResource(TBPermissionService tBPermissionService, TBPermissionQueryService tBPermissionQueryService) {
        this.tBPermissionService = tBPermissionService;
        this.tBPermissionQueryService = tBPermissionQueryService;
    }

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

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

    /**
     * GET  /tb-permissions : get all the tBPermissions.
     *
     * @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 tBPermissions in body
     */
    @GetMapping("/tb-permissions")
    @Timed
    public ResponseEntity<List<TBPermissionDTO>> getAllTBPermissions(TBPermissionCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBPermissions by criteria: {}", criteria);
        Page<TBPermissionDTO> page = tBPermissionQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-permissions");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

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

    /**
     * GET  /tb-permissions/:id : get the "id" tBPermission.
     *
     * @param id the id of the tBPermissionDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBPermissionDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-permissions/{id}")
    @Timed
    public ResponseEntity<TBPermissionDTO> getTBPermission(@PathVariable Long id) {
        log.debug("REST request to get TBPermission : {}", id);
        Optional<TBPermissionDTO> tBPermissionDTO = tBPermissionService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBPermissionDTO);
    }

    /**
     * DELETE  /tb-permissions/:id : delete the "id" tBPermission.
     *
     * @param id the id of the tBPermissionDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-permissions/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBPermission(@PathVariable Long id) {
        log.debug("REST request to delete TBPermission : {}", id);
        tBPermissionService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
