package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.multipart.MultipartFile;
import vn.azteam.tpf.Constants.Constants;
import vn.azteam.tpf.service.TBActivityQueryService;
import vn.azteam.tpf.service.TBActivityService;
import vn.azteam.tpf.service.TBObjectUpdateService;
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.TBObjectUpdateQueryService;
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 vn.azteam.tpf.service.util.UserRoleUtil;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

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

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

    private static final String ENTITY_NAME = "tBObjectUpdate";

    private final TBObjectUpdateService tBObjectUpdateService;

    private final TBObjectUpdateQueryService tBObjectUpdateQueryService;

    private final TBActivityService tBActivityService;

    private final UserRoleUtil userRoleUtil;

    private final ObjectMapper objectMapper;

    private final TBActivityQueryService tBActivityQueryService;

    public TBObjectUpdateResource(TBObjectUpdateService tBObjectUpdateService,
                                  TBObjectUpdateQueryService tBObjectUpdateQueryService,
                                  TBActivityService tBActivityService,
                                  UserRoleUtil userRoleUtil,
                                  ObjectMapper objectMapper,
                                  TBActivityQueryService tBActivityQueryService) {
        this.tBObjectUpdateService = tBObjectUpdateService;
        this.tBObjectUpdateQueryService = tBObjectUpdateQueryService;
        this.tBActivityService = tBActivityService;
        this.userRoleUtil = userRoleUtil;
        this.objectMapper = objectMapper;
        this.tBActivityQueryService = tBActivityQueryService;
    }

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

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

    /**
     * GET  /tb-object-updates : get all the tBObjectUpdates.
     *
     * @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 tBObjectUpdates in body
     */
    @GetMapping("/tb-object-updates")
    @Timed
    public ResponseEntity<List<TBObjectUpdateDTO>> getAllTBObjectUpdates(TBObjectUpdateCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBObjectUpdates by criteria: {}", criteria);
        Page<TBObjectUpdateDTO> page = tBObjectUpdateQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-object-updates");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * GET  /tb-object-updates/count : count all the tBObjectUpdates.
     *
     * @param criteria the criterias which the requested entities should match
     * @return the ResponseEntity with status 200 (OK) and the count in body
     */
    @GetMapping("/tb-object-updates/count")
    @Timed
    public ResponseEntity<Long> countTBObjectUpdates(TBObjectUpdateCriteria criteria) {
        log.debug("REST request to count TBObjectUpdates by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBObjectUpdateQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-object-updates/:id : get the "id" tBObjectUpdate.
     *
     * @param id the id of the tBObjectUpdateDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBObjectUpdateDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-object-updates/{id}")
    @Timed
    public ResponseEntity<TBObjectUpdateDTO> getTBObjectUpdate(@PathVariable Long id) {
        log.debug("REST request to get TBObjectUpdate : {}", id);
        Optional<TBObjectUpdateDTO> tBObjectUpdateDTO = tBObjectUpdateService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBObjectUpdateDTO);
    }

    /**
     * DELETE  /tb-object-updates/:id : delete the "id" tBObjectUpdate.
     *
     * @param id the id of the tBObjectUpdateDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-object-updates/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBObjectUpdate(@PathVariable Long id) {
        log.debug("REST request to delete TBObjectUpdate : {}", id);
        tBObjectUpdateService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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