package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBObjectParameterQueryService;
import vn.azteam.tpf.service.TBSuppliesTypeService;
import vn.azteam.tpf.service.dto.*;
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.TBSuppliesTypeQueryService;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing TBSuppliesType.
 */
@RestController
@RequestMapping("/api")
public class TBSuppliesTypeResource {

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

    private static final String ENTITY_NAME = "tBSuppliesType";

    private final TBSuppliesTypeService tBSuppliesTypeService;

    private final TBSuppliesTypeQueryService tBSuppliesTypeQueryService;

    public TBSuppliesTypeResource(TBSuppliesTypeService tBSuppliesTypeService,
                                  TBSuppliesTypeQueryService tBSuppliesTypeQueryService) {
        this.tBSuppliesTypeService = tBSuppliesTypeService;
        this.tBSuppliesTypeQueryService = tBSuppliesTypeQueryService;
    }

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

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

    /**
     * GET  /tb-supplies-types : get all the tBSuppliesTypes.
     *
     * @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 tBSuppliesTypes in body
     */
    @GetMapping("/tb-supplies-types")
    @Timed
    public ResponseEntity<List<TBSuppliesTypeDTO>> getAllTBSuppliesTypes(TBSuppliesTypeCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBSuppliesTypes by criteria: {}", criteria);
        Page<TBSuppliesTypeDTO> page = tBSuppliesTypeQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-supplies-types");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

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

    /**
     * GET  /tb-supplies-types/:id : get the "id" tBSuppliesType.
     *
     * @param id the id of the tBSuppliesTypeDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBSuppliesTypeDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-supplies-types/{id}")
    @Timed
    public ResponseEntity<TBSuppliesTypeDTO> getTBSuppliesType(@PathVariable Long id) {
        log.debug("REST request to get TBSuppliesType : {}", id);
        Optional<TBSuppliesTypeDTO> tBSuppliesTypeDTO = tBSuppliesTypeService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBSuppliesTypeDTO);
    }

    /**
     * DELETE  /tb-supplies-types/:id : delete the "id" tBSuppliesType.
     *
     * @param id the id of the tBSuppliesTypeDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-supplies-types/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBSuppliesType(@PathVariable Long id) {
        log.debug("REST request to delete TBSuppliesType : {}", id);
        tBSuppliesTypeService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

    @GetMapping("/list-supplies-types-create")
    @Timed
    public ResponseEntity<List<TBSuppliesTypeDTO>> getListTBSuppliesTypesCreate(@RequestParam(value = "q", required=false) String q,
                                                                                TBSuppliesTypeCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBSuppliesTypes by criteria: {}", criteria);
        Page<TBSuppliesTypeDTO> page = tBSuppliesTypeQueryService.findByCriteria(criteria, pageable);
        List<TBSuppliesTypeDTO> result = tBSuppliesTypeQueryService.getListTBSuppliesTypesCreate(q);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/list-supplies-types-create");
        return ResponseEntity.ok().headers(headers).body(result);
    }
}
