package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBGuidelineActivityService;
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.TBGuidelineActivityDTO;
import vn.azteam.tpf.service.dto.TBGuidelineActivityCriteria;
import vn.azteam.tpf.service.TBGuidelineActivityQueryService;
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 TBGuidelineActivity.
 */
@RestController
@RequestMapping("/api")
public class TBGuidelineActivityResource {

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

    private static final String ENTITY_NAME = "tBGuidelineActivity";

    private final TBGuidelineActivityService tBGuidelineActivityService;

    private final TBGuidelineActivityQueryService tBGuidelineActivityQueryService;

    public TBGuidelineActivityResource(TBGuidelineActivityService tBGuidelineActivityService, TBGuidelineActivityQueryService tBGuidelineActivityQueryService) {
        this.tBGuidelineActivityService = tBGuidelineActivityService;
        this.tBGuidelineActivityQueryService = tBGuidelineActivityQueryService;
    }

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

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

    /**
     * GET  /tb-guideline-activities : get all the tBGuidelineActivities.
     *
     * @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 tBGuidelineActivities in body
     */
    @GetMapping("/tb-guideline-activities")
    @Timed
    public ResponseEntity<List<TBGuidelineActivityDTO>> getAllTBGuidelineActivities(TBGuidelineActivityCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBGuidelineActivities by criteria: {}", criteria);
        Page<TBGuidelineActivityDTO> page = tBGuidelineActivityQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-guideline-activities");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-guideline-activities/count : count all the tBGuidelineActivities.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-guideline-activities/count")
    @Timed
    public ResponseEntity<Long> countTBGuidelineActivities(TBGuidelineActivityCriteria criteria) {
        log.debug("REST request to count TBGuidelineActivities by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBGuidelineActivityQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-guideline-activities/:id : get the "id" tBGuidelineActivity.
     *
     * @param id the id of the tBGuidelineActivityDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBGuidelineActivityDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-guideline-activities/{id}")
    @Timed
    public ResponseEntity<TBGuidelineActivityDTO> getTBGuidelineActivity(@PathVariable Long id) {
        log.debug("REST request to get TBGuidelineActivity : {}", id);
        Optional<TBGuidelineActivityDTO> tBGuidelineActivityDTO = tBGuidelineActivityService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBGuidelineActivityDTO);
    }

    /**
     * DELETE  /tb-guideline-activities/:id : delete the "id" tBGuidelineActivity.
     *
     * @param id the id of the tBGuidelineActivityDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-guideline-activities/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBGuidelineActivity(@PathVariable Long id) {
        log.debug("REST request to delete TBGuidelineActivity : {}", id);
        tBGuidelineActivityService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
