package com.docus.server.controller;

import com.docus.server.common.event.FlowEvent;
import com.docus.server.common.service.IFileUploadService;
import com.docus.server.dto.segmentation.UploadBatchFileRequest;
import com.docus.server.entity.segmentation.OcrFileInfoTest;
import com.docus.server.service.IOcrCutConfigService;
import com.docus.server.service.IOcrCutConfigTestService;
import com.docus.server.service.IOcrFileInfoTestService;
import com.docus.server.service.IPublishEventService;
import com.docus.server.service.impl.CommonService;
import com.docus.server.service.impl.PlatformServiceImpl;
import com.docus.server.vo.scheduling.management.schcollectorversionfile.UploadFileVO;
import com.docus.server.vo.segmentation.ocrcutconfig.OcrCutConfigVO;
import com.docus.server.vo.segmentation.ocrcutconfigtest.OcrCutConfigTestVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件上传下载 API
 *
 * @author AutoGenerator
 * @since 2023-07-15
 */
@Api(value = "通用文件上传下载接口", tags = "通用文件上传下载接口")
@RestController
@RequestMapping("/ocr/file")
public class FileController {
    @Resource
    private IOcrCutConfigService iOcrCutConfigService;
    @Resource
    private IOcrCutConfigTestService iOcrCutConfigTestService;
    @Resource
    private IFileUploadService iFileUploadService;
    @Resource
    private PlatformServiceImpl downloadService;
    @Resource
    private CommonService commonService;
    @Resource
    private IPublishEventService iPublishEventService;
    @Resource
    private IOcrFileInfoTestService iOcrFileInfoTestService;

    @ApiOperation("文件下载")
    @GetMapping("/download")
    public void downloadFile(String filePath, HttpServletResponse response) throws Exception {
        iFileUploadService.downloadFile(filePath, response);
    }

    @ApiOperation("获取图片")
    @GetMapping("/getImage")
    public void getImage(String filePath, HttpServletResponse response, HttpServletRequest request) throws Exception {
        iFileUploadService.getImage(filePath, response, request);
    }

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "files", value = "文件", required = true, dataTypeClass = MultipartFile.class)
    })
    public Object uploadFile(@RequestPart("files") MultipartFile[] files, @Validated UploadBatchFileRequest request) throws Exception {

        int testData = request.getTestData();
        Double height;
        Double widthStart;
        Double widthEnd;

        if (testData == 1) {
            OcrCutConfigTestVO ocrCutConfigTestVO = iOcrCutConfigTestService.findAll().get(0);
            height = ocrCutConfigTestVO.getHeight();
            widthStart = ocrCutConfigTestVO.getWidthStart();
            widthEnd = ocrCutConfigTestVO.getWidthEnd();
        } else {
            OcrCutConfigVO ocrCutConfigVO = iOcrCutConfigService.findAll().get(0);
            height = ocrCutConfigVO.getHeight();
            widthStart = ocrCutConfigVO.getWidthStart();
            widthEnd = ocrCutConfigVO.getWidthEnd();
        }

        List<UploadFileVO> segmentation = iFileUploadService.uploadFile(files, "segmentation", height, widthStart, widthEnd);

        //将基础信息存到库表里面，后面自动分段后，需要上传到归档系统
        Object result = commonService.add(segmentation, request);

        iPublishEventService.publishEvent(request.getPatientId(), FlowEvent.FlowTypeEnum.START_SEGMENT_TEST);

        return result;
    }

    @ApiOperation("单张文件上传")
    @PostMapping("/singleUpload")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "files", value = "文件", required = true, dataTypeClass = MultipartFile.class)
    })
    public OcrFileInfoTest singleUpload(@RequestPart("files") MultipartFile[] files, @Validated UploadBatchFileRequest request) throws Exception {
        int testData = request.getTestData();
        Double height;
        Double widthStart;
        Double widthEnd;

        if (testData == 1) {
            OcrCutConfigTestVO ocrCutConfigTestVO = iOcrCutConfigTestService.findAll().get(0);
            height = ocrCutConfigTestVO.getHeight();
            widthStart = ocrCutConfigTestVO.getWidthStart();
            widthEnd = ocrCutConfigTestVO.getWidthEnd();
        } else {
            OcrCutConfigVO ocrCutConfigVO = iOcrCutConfigService.findAll().get(0);
            height = ocrCutConfigVO.getHeight();
            widthStart = ocrCutConfigVO.getWidthStart();
            widthEnd = ocrCutConfigVO.getWidthEnd();
        }

        List<UploadFileVO> segmentation = iFileUploadService.uploadFile(files, "singleSegmentation", height, widthStart, widthEnd);

        return commonService.getOcr(segmentation.get(0), request);
    }

//
//    /**
//     * 下载平台
//     *
//     * @throws Exception
//     */
//    @ApiOperation("批量上传文件到归档系统")
//    @GetMapping("/uploadPlatform")
//    public void uploadPlatform() throws Exception {
//        downloadService.uploadPlatform(null, null);
//    }
}
