package com.docus.server.service.impl;

import cn.hutool.core.io.FileUtil;
import com.docus.core.util.Func;
import com.docus.core.util.json.JSON;
import com.docus.server.api.ocr.OcrApi;
import com.docus.server.dto.segmentation.UploadBatchFileRequest;
import com.docus.server.dto.segmentation.ocrbasic.AddOcrBasicDTO;
import com.docus.server.dto.segmentation.ocrbasictest.AddOcrBasicTestDTO;
import com.docus.server.dto.segmentation.ocrfileinfo.AddOcrFileInfoDTO;
import com.docus.server.dto.segmentation.ocrfileinfotest.AddOcrFileInfoTestDTO;
import com.docus.server.dto.segmentation.ocrfileinfotest.DeleteOcrFileInfoTestDTO;
import com.docus.server.entity.segmentation.OcrFileInfoTest;
import com.docus.server.enums.OcrStatusEnum;
import com.docus.server.service.IOcrBasicService;
import com.docus.server.service.IOcrBasicTestService;
import com.docus.server.service.IOcrFileInfoService;
import com.docus.server.service.IOcrFileInfoTestService;
import com.docus.server.service.handler.StartOcrTestHandler;
import com.docus.server.service.handler.StartSegmentTestHandler;
import com.docus.server.vo.scheduling.management.schcollectorversionfile.UploadFileVO;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class CommonService {
    @Resource
    private IOcrBasicService iOcrBasicService;
    @Resource
    private IOcrFileInfoService iOcrFileInfoService;

    @Resource
    private IOcrBasicTestService iOcrBasicTestService;
    @Resource
    private IOcrFileInfoTestService iOcrFileInfoTestService;
    @Resource
    private OcrApi ocrApi;
    @Resource
    private StartSegmentTestHandler startSegmentTestHandler;

    @Transactional(rollbackFor = Exception.class)
    public Object add(List<UploadFileVO> segmentation, UploadBatchFileRequest request) {
        //test
        if (request.getTestData() == 1) {
            AddOcrBasicTestDTO addOcrBasicDTO = new AddOcrBasicTestDTO();

            addOcrBasicDTO.setPatientId(request.getPatientId());
            addOcrBasicDTO.setInpatientNo(request.getInpatientNo());
            addOcrBasicDTO.setName(request.getName());

            iOcrBasicTestService.saveOrUpdate(addOcrBasicDTO);


            List<AddOcrFileInfoTestDTO> files = new ArrayList<>();

            for (UploadFileVO uploadFileVO : segmentation) {
                AddOcrFileInfoTestDTO addOcrFileInfoDTO = new AddOcrFileInfoTestDTO();

                addOcrFileInfoDTO.setPatientId(request.getPatientId());
                addOcrFileInfoDTO.setFileTitle(uploadFileVO.getFileTitle());
                addOcrFileInfoDTO.setPicName(uploadFileVO.getFileName());
                addOcrFileInfoDTO.setFileType(2);
                addOcrFileInfoDTO.setSerialNumber(UUID.randomUUID().toString().replace("-", ""));
                addOcrFileInfoDTO.setPicUrl(uploadFileVO.getSaveFilePath() + uploadFileVO.getFilePath());
                addOcrFileInfoDTO.setPicCompressUrl(uploadFileVO.getSaveCompressFilePath() + uploadFileVO.getFilePath());
                addOcrFileInfoDTO.setPicCutUrl(uploadFileVO.getSaveCutFilePath() + uploadFileVO.getFilePath());
                addOcrFileInfoDTO.setFileSize(uploadFileVO.getFileSize());
                addOcrFileInfoDTO.setHeight(uploadFileVO.getHeight());
                addOcrFileInfoDTO.setWidth(uploadFileVO.getWidth());
                addOcrFileInfoDTO.setX(uploadFileVO.getX());
                addOcrFileInfoDTO.setY(uploadFileVO.getY());
                addOcrFileInfoDTO.setJsonStr(JSON.toJSON(uploadFileVO.getParams()));
                addOcrFileInfoDTO.setOcrStatus(OcrStatusEnum.NO_START);
                files.add(addOcrFileInfoDTO);
            }


            iOcrFileInfoTestService.addBatch(files);

            return files;
        } else {
            AddOcrBasicDTO addOcrBasicDTO = new AddOcrBasicDTO();

            addOcrBasicDTO.setPatientId(request.getPatientId());
            addOcrBasicDTO.setInpatientNo(request.getInpatientNo());
            addOcrBasicDTO.setName(request.getName());

            iOcrBasicService.saveOrUpdate(addOcrBasicDTO);

            List<AddOcrFileInfoDTO> files = new ArrayList<>();

            for (UploadFileVO uploadFileVO : segmentation) {
                AddOcrFileInfoDTO addOcrFileInfoDTO = new AddOcrFileInfoDTO();
                addOcrFileInfoDTO.setPatientId(request.getPatientId());
                addOcrFileInfoDTO.setFileTitle(uploadFileVO.getFileTitle());
                addOcrFileInfoDTO.setPicName(uploadFileVO.getFileName());
                addOcrFileInfoDTO.setFileType(2);
                addOcrFileInfoDTO.setSerialNumber(UUID.randomUUID().toString().replace("-", ""));
                addOcrFileInfoDTO.setPicUrl(uploadFileVO.getSaveFilePath() + uploadFileVO.getFilePath());
                addOcrFileInfoDTO.setPicCompressUrl(uploadFileVO.getSaveCompressFilePath() + uploadFileVO.getFilePath());
                addOcrFileInfoDTO.setPicCutUrl(uploadFileVO.getSaveCutFilePath() + uploadFileVO.getFilePath());
                addOcrFileInfoDTO.setFileSize(uploadFileVO.getFileSize());
                addOcrFileInfoDTO.setHeight(uploadFileVO.getHeight());
                addOcrFileInfoDTO.setWidth(uploadFileVO.getWidth());
                addOcrFileInfoDTO.setX(uploadFileVO.getX());
                addOcrFileInfoDTO.setY(uploadFileVO.getY());
                addOcrFileInfoDTO.setJsonStr(JSON.toJSON(uploadFileVO.getParams()));
                addOcrFileInfoDTO.setOcrStatus(OcrStatusEnum.NO_START);
                files.add(addOcrFileInfoDTO);
            }

            iOcrFileInfoService.addBatch(files);

            return files;
        }

    }

    @Transactional(rollbackFor = Exception.class)
    public AddOcrFileInfoTestDTO add(UploadFileVO uploadFileVO, UploadBatchFileRequest request) {

        AddOcrFileInfoTestDTO addOcrFileInfoDTO = new AddOcrFileInfoTestDTO();

//        addOcrFileInfoDTO.setPatientId(request.getPatientId());
        addOcrFileInfoDTO.setFileTitle(uploadFileVO.getFileTitle());
        addOcrFileInfoDTO.setPicName(uploadFileVO.getFileName());
        addOcrFileInfoDTO.setFileType(2);
        addOcrFileInfoDTO.setSerialNumber(UUID.randomUUID().toString().replace("-", ""));
        addOcrFileInfoDTO.setPicUrl(uploadFileVO.getSaveFilePath() + uploadFileVO.getFilePath());
        addOcrFileInfoDTO.setPicCompressUrl(uploadFileVO.getSaveCompressFilePath() + uploadFileVO.getFilePath());
        addOcrFileInfoDTO.setPicCutUrl(uploadFileVO.getSaveCutFilePath() + uploadFileVO.getFilePath());
        addOcrFileInfoDTO.setFileSize(uploadFileVO.getFileSize());
        addOcrFileInfoDTO.setHeight(uploadFileVO.getHeight());
        addOcrFileInfoDTO.setWidth(uploadFileVO.getWidth());
        addOcrFileInfoDTO.setX(uploadFileVO.getX());
        addOcrFileInfoDTO.setY(uploadFileVO.getY());
        addOcrFileInfoDTO.setJsonStr(JSON.toJSON(uploadFileVO.getParams()));
        addOcrFileInfoDTO.setOcrStatus(OcrStatusEnum.NO_START);

        Long id = iOcrFileInfoTestService.saveOrUpdate(addOcrFileInfoDTO);
        addOcrFileInfoDTO.setPatientId(String.valueOf(id));
        addOcrFileInfoDTO.setId(id);

        return addOcrFileInfoDTO;
    }

    public OcrFileInfoTest getOcr(UploadFileVO uploadFileVO, UploadBatchFileRequest request) {

        //上一份
        String patientId = request.getPatientId();
        if (Func.isNotBlank(patientId)) {
            OcrFileInfoTest ocrFileInfoTest = iOcrFileInfoTestService.findOneBy(patientId);

            if (null != ocrFileInfoTest) {
                iOcrFileInfoTestService.delete(DeleteOcrFileInfoTestDTO.builder().ids(Lists.newArrayList(ocrFileInfoTest.getId())).build());

                FileUtil.del(ocrFileInfoTest.getPicUrl());
                FileUtil.del(ocrFileInfoTest.getPicCompressUrl());
                FileUtil.del(ocrFileInfoTest.getPicCutUrl());
            }
        }

        //当前
        AddOcrFileInfoTestDTO addOcrFileInfoTestDTO = this.add(uploadFileVO, request);

        List<OcrFileInfoTest> ocrFileInfoTests = startSegmentTestHandler.startSegment(addOcrFileInfoTestDTO.getPatientId());

        if (Func.isNotEmpty(ocrFileInfoTests)) {
            return ocrFileInfoTests.get(0);
        } else {
            return null;
        }
    }
}
