package com.oqpo.api.web.controller;

import com.oqpo.api.entity.oper.FileEntity;
import com.oqpo.api.service.FileService;
import com.oqpo.api.web.dto.request.FileRequest;
import com.oqpo.api.web.dto.response.FileResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/api/file")
public class FileController {
    @Autowired
    private FileService fileService;

    /**
     * 설명 : 파일업로드 - Swagger 에서는 파일번호 없을시, 공백으로 보냄
     *
     * @param userId
     * @param path
     * @param file
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "파일업로드 - Swagger 에서는 파일번호 없을시, 공백으로 보냄")
    @PostMapping("/upload")
    public ResponseEntity<FileResponse> fileUpload(@ApiIgnore String userId,
                                                   @RequestParam("fileNo") String fileNo,
                                                   @RequestParam("uploadFolder") String path,
                                                   @RequestParam("uploadFile") MultipartFile file) throws Exception {
        return ResponseEntity.ok(FileResponse.toDTO(fileService.fileUpload(userId, fileNo, path, file)));
    }

    /**
     * 설명 : 파일삭제
     *
     * @param fileRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "파일삭제")
    @DeleteMapping("/delete")
    public ResponseEntity<Void> fileDelete(@RequestBody @Valid FileRequest fileRequest) throws Exception {
        fileService.fileDelete(fileRequest.getFileNo(), fileRequest.getFileSeq());
        return ResponseEntity.ok().build();
    }

    /**
     * 설명 : 파일목록
     *
     * @param fileNo
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "파일목록")
    @GetMapping("/list")
    public ResponseEntity<List<FileResponse>> fileList(@RequestParam("fileNo") String fileNo) throws Exception {
        List<FileEntity> entities = fileService.fileList(fileNo);
        return ResponseEntity.ok(
                entities.stream()
                        .map(entity -> FileResponse.toDTO(entity))
                        .collect(Collectors.toList())
        );
    }

    /**
     * 설명 : 파일다운로드 - Swagger 에서는 한글이름으로 다운로드 않됨
     *
     * @param fileNo
     * @param fileSeq
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "파일다운로드 - Swagger 에서는 한글이름으로 다운로드 않됨")
    @GetMapping("/download/{fileNo}/{fileSeq}")
    public ResponseEntity<Void> download(@PathVariable("fileNo") String fileNo, @PathVariable("fileSeq") Integer fileSeq, HttpServletRequest request, HttpServletResponse response) throws Exception {
        fileService.fileDownload(fileNo, fileSeq, request, response);
        return ResponseEntity.ok().build();
    }
}
