package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBVendorService;
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.TBVendorDTO;
import vn.azteam.tpf.service.dto.TBVendorCriteria;
import vn.azteam.tpf.service.TBVendorQueryService;
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 TBVendor.
 */
@RestController
@RequestMapping("/api")
public class TBVendorResource {

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

    private static final String ENTITY_NAME = "tBVendor";

    private final TBVendorService tBVendorService;

    private final TBVendorQueryService tBVendorQueryService;

    public TBVendorResource(TBVendorService tBVendorService, TBVendorQueryService tBVendorQueryService) {
        this.tBVendorService = tBVendorService;
        this.tBVendorQueryService = tBVendorQueryService;
    }

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

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

    /**
     * GET  /tb-vendors : get all the tBVendors.
     *
     * @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 tBVendors in body
     */
    @GetMapping("/tb-vendors")
    @Timed
    public ResponseEntity<List<TBVendorDTO>> getAllTBVendors(TBVendorCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBVendors by criteria: {}", criteria);
        Page<TBVendorDTO> page = tBVendorQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-vendors");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

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

    /**
     * GET  /tb-vendors/:id : get the "id" tBVendor.
     *
     * @param id the id of the tBVendorDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBVendorDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-vendors/{id}")
    @Timed
    public ResponseEntity<TBVendorDTO> getTBVendor(@PathVariable Long id) {
        log.debug("REST request to get TBVendor : {}", id);
        Optional<TBVendorDTO> tBVendorDTO = tBVendorService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBVendorDTO);
    }

    /**
     * DELETE  /tb-vendors/:id : delete the "id" tBVendor.
     *
     * @param id the id of the tBVendorDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-vendors/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBVendor(@PathVariable Long id) {
        log.debug("REST request to delete TBVendor : {}", id);
        tBVendorService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
