package ervu.controller;

import java.time.ZonedDateTime;
import java.util.TimeZone;

import ervu.service.fileupload.EmployeeInfoFileUploadService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Alexandr Shalaginov
 */
@RestController
public class EmployeeInfoFileUploadController {
  private final EmployeeInfoFileUploadService fileUploadService;

  public EmployeeInfoFileUploadController(EmployeeInfoFileUploadService fileUploadService) {
    this.fileUploadService = fileUploadService;
  }

  @RequestMapping(value = "/employee/document", method = RequestMethod.POST)
  public ResponseEntity<?> saveEmployeeInformationFile(
      @RequestParam("file") MultipartFile multipartFile,
      @RequestHeader("X-Employee-Info-File-Form-Type") String formType,
      @RequestHeader("Client-Time-Zone") String clientTimeZone) {

    String offset = ZonedDateTime.now(TimeZone.getTimeZone(clientTimeZone).toZoneId())
        .getOffset().getId();

    if (this.fileUploadService.saveEmployeeInformationFile(multipartFile, formType, offset)) {
      return ResponseEntity.ok("File successfully uploaded.");
    }

    return ResponseEntity.internalServerError().body("An error occurred while uploading file.");
  }
}
