package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.searchbox.strings.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import vn.azteam.tpf.domain.TBCodeStatusEnum;
import vn.azteam.tpf.service.TBCodeDetailsQueryService;
import vn.azteam.tpf.service.TBCodeDetailsService;
import vn.azteam.tpf.service.UserService;
import vn.azteam.tpf.service.dto.TBCodeDetailsDTO;
import vn.azteam.tpf.service.dto.UserDTO;
import vn.azteam.tpf.service.util.PageableUtil;
import vn.azteam.tpf.service.util.UserRoleUtil;
import vn.azteam.tpf.web.rest.errors.BadRequestAlertException;

import java.time.Instant;
import java.util.Optional;

@RestController
@RequestMapping("/api")
public class TBCodeDetailsResource {

    private static final String ENTITY_NAME = "tBCodeDetails";
    private final Logger log = LoggerFactory.getLogger(TBCodeDetailsResource.class);

    private final TBCodeDetailsService tBCodeDetailsService;

    private final TBCodeDetailsQueryService tBCodeDetailsQueryService;

    private final UserRoleUtil userRoleUtil;

    private final PageableUtil pageableUtil;

    private final UserService userService;


    public TBCodeDetailsResource(TBCodeDetailsService tBCodeDetailsService, TBCodeDetailsQueryService tBCodeDetailsQueryService,
                                 UserRoleUtil userRoleUtil, PageableUtil pageableUtil, UserService userService) {
        this.tBCodeDetailsService = tBCodeDetailsService;
        this.tBCodeDetailsQueryService = tBCodeDetailsQueryService;
        this.userRoleUtil = userRoleUtil;
        this.pageableUtil = pageableUtil;
        this.userService = userService;
    }

    @GetMapping("/tb-code-details/scan/{code}")
    @Timed
    public ResponseEntity<?> getTBCodeDetailsScan(@PathVariable String code) {
        if (StringUtils.isBlank(code)) {
            return (ResponseEntity<?>) ResponseEntity.badRequest();
        }
        tBCodeDetailsService.updateNumberScan(code);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/tb-code-details/update/{id}/status/{status}")
    @Timed
    public ResponseEntity<TBCodeDetailsDTO> updateTBCodeDetailsStatus(@PathVariable Long id, @PathVariable String status) {
        log.debug("REST request to update tb codes : {}, status {} ", id, status);
        if (id == null || org.apache.commons.lang3.StringUtils.isBlank(status)) {
            throw new BadRequestAlertException("1039", ENTITY_NAME, "1039");
        }
        try {
            TBCodeStatusEnum.valueOf(status);
        } catch (Exception exception) {
            throw new BadRequestAlertException("1039", ENTITY_NAME, "1039");
        }
        UserDTO currentUser = userService.getCurrentUserDTO().get();
        TBCodeDetailsDTO tbCodeDetailsDTO = new TBCodeDetailsDTO();
        tbCodeDetailsDTO.setId(id);
        tbCodeDetailsDTO.setModifiedDate(Instant.now());
        tbCodeDetailsDTO.setModifiedById(currentUser.getId());
        tbCodeDetailsDTO.setStatus(TBCodeStatusEnum.valueOf(status));

        Optional<TBCodeDetailsDTO> tBCodeUpdated =
            tBCodeDetailsService.updateStatusTBCodeDetails(tbCodeDetailsDTO);
//        if (null != tBCropDTO) {
//            sendNotificationPersonInCharge(cropPersonInChargeDTO, listAssignmentPrev);
//        }
        return ResponseUtil.wrapOrNotFound(tBCodeUpdated);
    }
}
