package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBEquipmentTypeService;
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.TBEquipmentTypeDTO;
import vn.azteam.tpf.service.dto.TBEquipmentTypeCriteria;
import vn.azteam.tpf.service.TBEquipmentTypeQueryService;
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 TBEquipmentType.
 */
@RestController
@RequestMapping("/api")
public class TBEquipmentTypeResource {

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

    private static final String ENTITY_NAME = "tBEquipmentType";

    private final TBEquipmentTypeService tBEquipmentTypeService;

    private final TBEquipmentTypeQueryService tBEquipmentTypeQueryService;

    public TBEquipmentTypeResource(TBEquipmentTypeService tBEquipmentTypeService, TBEquipmentTypeQueryService tBEquipmentTypeQueryService) {
        this.tBEquipmentTypeService = tBEquipmentTypeService;
        this.tBEquipmentTypeQueryService = tBEquipmentTypeQueryService;
    }

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

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

    /**
     * GET  /tb-equipment-types : get all the tBEquipmentTypes.
     *
     * @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 tBEquipmentTypes in body
     */
    @GetMapping("/tb-equipment-types")
    @Timed
    public ResponseEntity<List<TBEquipmentTypeDTO>> getAllTBEquipmentTypes(TBEquipmentTypeCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBEquipmentTypes by criteria: {}", criteria);
        Page<TBEquipmentTypeDTO> page = tBEquipmentTypeQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-equipment-types");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

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

    /**
     * GET  /tb-equipment-types/:id : get the "id" tBEquipmentType.
     *
     * @param id the id of the tBEquipmentTypeDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBEquipmentTypeDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-equipment-types/{id}")
    @Timed
    public ResponseEntity<TBEquipmentTypeDTO> getTBEquipmentType(@PathVariable Long id) {
        log.debug("REST request to get TBEquipmentType : {}", id);
        Optional<TBEquipmentTypeDTO> tBEquipmentTypeDTO = tBEquipmentTypeService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBEquipmentTypeDTO);
    }

    /**
     * DELETE  /tb-equipment-types/:id : delete the "id" tBEquipmentType.
     *
     * @param id the id of the tBEquipmentTypeDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-equipment-types/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBEquipmentType(@PathVariable Long id) {
        log.debug("REST request to delete TBEquipmentType : {}", id);
        tBEquipmentTypeService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

    @GetMapping("/tb-equipment-types-to-customer")
    @Timed
    public ResponseEntity<List<TBEquipmentTypeDTO>> getBEquipmentTypesByCustomer(TBEquipmentTypeCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBEquipmentTypes by criteria: {}", criteria);
//        Page<TBEquipmentTypeDTO> page = tBEquipmentTypeQueryService.findByCriteria(criteria, pageable);
        Page<TBEquipmentTypeDTO> page = tBEquipmentTypeQueryService.getBEquipmentTypesByCustomer(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-equipment-types");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }
}
