package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBAddressService;
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.TBAddressDTO;
import vn.azteam.tpf.service.dto.TBAddressCriteria;
import vn.azteam.tpf.service.TBAddressQueryService;
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 TBAddress.
 */
@RestController
@RequestMapping("/api")
public class TBAddressResource {

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

    private static final String ENTITY_NAME = "tBAddress";

    private final TBAddressService tBAddressService;

    private final TBAddressQueryService tBAddressQueryService;

    public TBAddressResource(TBAddressService tBAddressService, TBAddressQueryService tBAddressQueryService) {
        this.tBAddressService = tBAddressService;
        this.tBAddressQueryService = tBAddressQueryService;
    }

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

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

    /**
     * GET  /tb-addresses : get all the tBAddresses.
     *
     * @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 tBAddresses in body
     */
    @GetMapping("/tb-addresses")
    @Timed
    public ResponseEntity<List<TBAddressDTO>> getAllTBAddresses(TBAddressCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBAddresses by criteria: {}", criteria);
        Page<TBAddressDTO> page = tBAddressQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-addresses");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

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

    /**
     * GET  /tb-addresses/:id : get the "id" tBAddress.
     *
     * @param id the id of the tBAddressDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBAddressDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-addresses/{id}")
    @Timed
    public ResponseEntity<TBAddressDTO> getTBAddress(@PathVariable Long id) {
        log.debug("REST request to get TBAddress : {}", id);
        Optional<TBAddressDTO> tBAddressDTO = tBAddressService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBAddressDTO);
    }

    /**
     * DELETE  /tb-addresses/:id : delete the "id" tBAddress.
     *
     * @param id the id of the tBAddressDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-addresses/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBAddress(@PathVariable Long id) {
        log.debug("REST request to delete TBAddress : {}", id);
        tBAddressService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
