package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBDashboardTypeService;
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.TBDashboardTypeDTO;
import vn.azteam.tpf.service.dto.TBDashboardTypeCriteria;
import vn.azteam.tpf.service.TBDashboardTypeQueryService;
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 TBDashboardType.
 */
@RestController
@RequestMapping("/api")
public class TBDashboardTypeResource {

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

    private static final String ENTITY_NAME = "tBDashboardType";

    private final TBDashboardTypeService tBDashboardTypeService;

    private final TBDashboardTypeQueryService tBDashboardTypeQueryService;

    public TBDashboardTypeResource(TBDashboardTypeService tBDashboardTypeService, TBDashboardTypeQueryService tBDashboardTypeQueryService) {
        this.tBDashboardTypeService = tBDashboardTypeService;
        this.tBDashboardTypeQueryService = tBDashboardTypeQueryService;
    }

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

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

    /**
     * GET  /tb-dashboard-types : get all the tBDashboardTypes.
     *
     * @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 tBDashboardTypes in body
     */
    @GetMapping("/tb-dashboard-types")
    @Timed
    public ResponseEntity<List<TBDashboardTypeDTO>> getAllTBDashboardTypes(TBDashboardTypeCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBDashboardTypes by criteria: {}", criteria);
        Page<TBDashboardTypeDTO> page = tBDashboardTypeQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-dashboard-types");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-dashboard-types/count : count all the tBDashboardTypes.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-dashboard-types/count")
    @Timed
    public ResponseEntity<Long> countTBDashboardTypes(TBDashboardTypeCriteria criteria) {
        log.debug("REST request to count TBDashboardTypes by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBDashboardTypeQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-dashboard-types/:id : get the "id" tBDashboardType.
     *
     * @param id the id of the tBDashboardTypeDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBDashboardTypeDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-dashboard-types/{id}")
    @Timed
    public ResponseEntity<TBDashboardTypeDTO> getTBDashboardType(@PathVariable Long id) {
        log.debug("REST request to get TBDashboardType : {}", id);
        Optional<TBDashboardTypeDTO> tBDashboardTypeDTO = tBDashboardTypeService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBDashboardTypeDTO);
    }

    /**
     * DELETE  /tb-dashboard-types/:id : delete the "id" tBDashboardType.
     *
     * @param id the id of the tBDashboardTypeDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-dashboard-types/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBDashboardType(@PathVariable Long id) {
        log.debug("REST request to delete TBDashboardType : {}", id);
        tBDashboardTypeService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
