package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.service.filter.IntegerFilter;
import io.github.jhipster.service.filter.LongFilter;
import vn.azteam.tpf.domain.TBEntityLocationOfCustomer;
import vn.azteam.tpf.service.TBEntityLocationOfCustomerService;
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.TBEntityLocationOfCustomerDTO;
import vn.azteam.tpf.service.dto.TBEntityLocationOfCustomerCriteria;
import vn.azteam.tpf.service.TBEntityLocationOfCustomerQueryService;
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 TBEntityLocationOfCustomer.
 */
@RestController
@RequestMapping("/api")
public class TBEntityLocationOfCustomerResource {

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

    private static final String ENTITY_NAME = "tBEntityLocationOfCustomer";

    private final TBEntityLocationOfCustomerService tBEntityLocationOfCustomerService;

    private final TBEntityLocationOfCustomerQueryService tBEntityLocationOfCustomerQueryService;

    public TBEntityLocationOfCustomerResource(TBEntityLocationOfCustomerService tBEntityLocationOfCustomerService, TBEntityLocationOfCustomerQueryService tBEntityLocationOfCustomerQueryService) {
        this.tBEntityLocationOfCustomerService = tBEntityLocationOfCustomerService;
        this.tBEntityLocationOfCustomerQueryService = tBEntityLocationOfCustomerQueryService;
    }

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

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

    /**
     * GET  /tb-entity-location-of-customers : get all the tBEntityLocationOfCustomers.
     *
     * @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 tBEntityLocationOfCustomers in body
     */
    @GetMapping("/tb-entity-location-of-customers")
    @Timed
    public ResponseEntity<List<TBEntityLocationOfCustomerDTO>> getAllTBEntityLocationOfCustomers(TBEntityLocationOfCustomerCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBEntityLocationOfCustomers by criteria: {}", criteria);
        Page<TBEntityLocationOfCustomerDTO> page = tBEntityLocationOfCustomerQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-entity-location-of-customers");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-entity-location-of-customers/count : count all the tBEntityLocationOfCustomers.
    *
    * @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-location-of-customers/count")
    @Timed
    public ResponseEntity<Long> countTBEntityLocationOfCustomers(TBEntityLocationOfCustomerCriteria criteria) {
        log.debug("REST request to count TBEntityLocationOfCustomers by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBEntityLocationOfCustomerQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-entity-location-of-customers/:id : get the "id" tBEntityLocationOfCustomer.
     *
     * @param id the id of the tBEntityLocationOfCustomerDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBEntityLocationOfCustomerDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-entity-location-of-customers/{id}")
    @Timed
    public ResponseEntity<TBEntityLocationOfCustomerDTO> getTBEntityLocationOfCustomer(@PathVariable Long id) {
        log.debug("REST request to get TBEntityLocationOfCustomer : {}", id);
        Optional<TBEntityLocationOfCustomerDTO> tBEntityLocationOfCustomerDTO = tBEntityLocationOfCustomerService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBEntityLocationOfCustomerDTO);
    }

    /**
     * DELETE  /tb-entity-location-of-customers/:id : delete the "id" tBEntityLocationOfCustomer.
     *
     * @param id the id of the tBEntityLocationOfCustomerDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-entity-location-of-customers/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBEntityLocationOfCustomer(@PathVariable Long id) {
        log.debug("REST request to delete TBEntityLocationOfCustomer : {}", id);
        tBEntityLocationOfCustomerService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

    @GetMapping("/tb-entity-location-of-customers-by-customer/{tbCustomerId}")
    @Timed
    public ResponseEntity<List<TBEntityLocationOfCustomerDTO>> getAllTBEntityLocationOfCustomersByCustomer(@PathVariable Long tbCustomerId, Pageable pageable) {
        List<TBEntityLocationOfCustomerDTO> tbEntityLocationOfCustomerDTOs = tBEntityLocationOfCustomerQueryService.getListTbEntityOfCustomerByCustomer(tbCustomerId);
        return ResponseEntity.ok().headers(null).body(tbEntityLocationOfCustomerDTOs);
    }
}
