package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBEntityTypeService;
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.TBEntityTypeDTO;
import vn.azteam.tpf.service.dto.TBEntityTypeCriteria;
import vn.azteam.tpf.service.TBEntityTypeQueryService;
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 TBEntityType.
 */
@RestController
@RequestMapping("/api")
public class TBEntityTypeResource {

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

    private static final String ENTITY_NAME = "tBEntityType";

    private final TBEntityTypeService tBEntityTypeService;

    private final TBEntityTypeQueryService tBEntityTypeQueryService;

    public TBEntityTypeResource(TBEntityTypeService tBEntityTypeService, TBEntityTypeQueryService tBEntityTypeQueryService) {
        this.tBEntityTypeService = tBEntityTypeService;
        this.tBEntityTypeQueryService = tBEntityTypeQueryService;
    }

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

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

    /**
     * GET  /tb-entity-types : get all the tBEntityTypes.
     *
     * @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 tBEntityTypes in body
     */
    @GetMapping("/tb-entity-types")
    @Timed
    public ResponseEntity<List<TBEntityTypeDTO>> getAllTBEntityTypes(TBEntityTypeCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBEntityTypes by criteria: {}", criteria);
        Page<TBEntityTypeDTO> page = tBEntityTypeQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-entity-types");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

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

    /**
     * GET  /tb-entity-types/:id : get the "id" tBEntityType.
     *
     * @param id the id of the tBEntityTypeDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBEntityTypeDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-entity-types/{id}")
    @Timed
    public ResponseEntity<TBEntityTypeDTO> getTBEntityType(@PathVariable Long id) {
        log.debug("REST request to get TBEntityType : {}", id);
        Optional<TBEntityTypeDTO> tBEntityTypeDTO = tBEntityTypeService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBEntityTypeDTO);
    }

    /**
     * DELETE  /tb-entity-types/:id : delete the "id" tBEntityType.
     *
     * @param id the id of the tBEntityTypeDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-entity-types/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBEntityType(@PathVariable Long id) {
        log.debug("REST request to delete TBEntityType : {}", id);
        tBEntityTypeService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
