package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBCommandService;
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.TBCommandDTO;
import vn.azteam.tpf.service.dto.TBCommandCriteria;
import vn.azteam.tpf.service.TBCommandQueryService;
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 TBCommand.
 */
@RestController
@RequestMapping("/api")
public class TBCommandResource {

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

    private static final String ENTITY_NAME = "tBCommand";

    private final TBCommandService tBCommandService;

    private final TBCommandQueryService tBCommandQueryService;

    public TBCommandResource(TBCommandService tBCommandService, TBCommandQueryService tBCommandQueryService) {
        this.tBCommandService = tBCommandService;
        this.tBCommandQueryService = tBCommandQueryService;
    }

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

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

    /**
     * GET  /tb-commands : get all the tBCommands.
     *
     * @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 tBCommands in body
     */
    @GetMapping("/tb-commands")
    @Timed
    public ResponseEntity<List<TBCommandDTO>> getAllTBCommands(TBCommandCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBCommands by criteria: {}", criteria);
        Page<TBCommandDTO> page = tBCommandQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-commands");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

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

    /**
     * GET  /tb-commands/:id : get the "id" tBCommand.
     *
     * @param id the id of the tBCommandDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBCommandDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-commands/{id}")
    @Timed
    public ResponseEntity<TBCommandDTO> getTBCommand(@PathVariable Long id) {
        log.debug("REST request to get TBCommand : {}", id);
        Optional<TBCommandDTO> tBCommandDTO = tBCommandService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBCommandDTO);
    }

    /**
     * DELETE  /tb-commands/:id : delete the "id" tBCommand.
     *
     * @param id the id of the tBCommandDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-commands/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBCommand(@PathVariable Long id) {
        log.debug("REST request to delete TBCommand : {}", id);
        tBCommandService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
