package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBMaintenanceScheduleService;
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.TBMaintenanceScheduleDTO;
import vn.azteam.tpf.service.dto.TBMaintenanceScheduleCriteria;
import vn.azteam.tpf.service.TBMaintenanceScheduleQueryService;
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 TBMaintenanceSchedule.
 */
@RestController
@RequestMapping("/api")
public class TBMaintenanceScheduleResource {

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

    private static final String ENTITY_NAME = "tBMaintenanceSchedule";

    private final TBMaintenanceScheduleService tBMaintenanceScheduleService;

    private final TBMaintenanceScheduleQueryService tBMaintenanceScheduleQueryService;

    public TBMaintenanceScheduleResource(TBMaintenanceScheduleService tBMaintenanceScheduleService, TBMaintenanceScheduleQueryService tBMaintenanceScheduleQueryService) {
        this.tBMaintenanceScheduleService = tBMaintenanceScheduleService;
        this.tBMaintenanceScheduleQueryService = tBMaintenanceScheduleQueryService;
    }

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

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

    /**
     * GET  /tb-maintenance-schedules : get all the tBMaintenanceSchedules.
     *
     * @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 tBMaintenanceSchedules in body
     */
    @GetMapping("/tb-maintenance-schedules")
    @Timed
    public ResponseEntity<List<TBMaintenanceScheduleDTO>> getAllTBMaintenanceSchedules(TBMaintenanceScheduleCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBMaintenanceSchedules by criteria: {}", criteria);
        Page<TBMaintenanceScheduleDTO> page = tBMaintenanceScheduleQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-maintenance-schedules");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-maintenance-schedules/count : count all the tBMaintenanceSchedules.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-maintenance-schedules/count")
    @Timed
    public ResponseEntity<Long> countTBMaintenanceSchedules(TBMaintenanceScheduleCriteria criteria) {
        log.debug("REST request to count TBMaintenanceSchedules by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBMaintenanceScheduleQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-maintenance-schedules/:id : get the "id" tBMaintenanceSchedule.
     *
     * @param id the id of the tBMaintenanceScheduleDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBMaintenanceScheduleDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-maintenance-schedules/{id}")
    @Timed
    public ResponseEntity<TBMaintenanceScheduleDTO> getTBMaintenanceSchedule(@PathVariable Long id) {
        log.debug("REST request to get TBMaintenanceSchedule : {}", id);
        Optional<TBMaintenanceScheduleDTO> tBMaintenanceScheduleDTO = tBMaintenanceScheduleService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBMaintenanceScheduleDTO);
    }

    /**
     * DELETE  /tb-maintenance-schedules/:id : delete the "id" tBMaintenanceSchedule.
     *
     * @param id the id of the tBMaintenanceScheduleDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-maintenance-schedules/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBMaintenanceSchedule(@PathVariable Long id) {
        log.debug("REST request to delete TBMaintenanceSchedule : {}", id);
        tBMaintenanceScheduleService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
