package vip.xiaonuo.disk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.common.util.MimeUtils;
import com.qiwenshare.ufop.constant.UploadFileStatusEnum;
import com.qiwenshare.ufop.exception.operation.DownloadException;
import com.qiwenshare.ufop.exception.operation.UploadException;
import com.qiwenshare.ufop.factory.UFOPFactory;
import com.qiwenshare.ufop.operation.download.Downloader;
import com.qiwenshare.ufop.operation.download.domain.DownloadFile;
import com.qiwenshare.ufop.operation.preview.Previewer;
import com.qiwenshare.ufop.operation.preview.domain.PreviewFile;
import com.qiwenshare.ufop.operation.upload.Uploader;
import com.qiwenshare.ufop.operation.upload.domain.UploadFile;
import com.qiwenshare.ufop.operation.upload.domain.UploadFileResult;
import com.qiwenshare.ufop.util.UFOPUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.disk.component.ResourceFileDealComp;
import vip.xiaonuo.disk.domain.*;
import vip.xiaonuo.disk.dto.file.DownloadFileDTO;
import vip.xiaonuo.disk.dto.file.EachOtherExchangeDTO;
import vip.xiaonuo.disk.dto.file.PreviewDTO;
import vip.xiaonuo.disk.dto.file.UploadFileDTO;
import vip.xiaonuo.disk.io.QiwenFile;
import vip.xiaonuo.disk.mapper.*;
import vip.xiaonuo.disk.service.ResourceFileService;
import vip.xiaonuo.disk.util.OperationLogUtil;
import vip.xiaonuo.disk.vo.file.UploadFileVo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Slf4j
@Service
@Transactional(rollbackFor=Exception.class)
public class ResourceFileServiceImpl extends ServiceImpl<ResourceFileMapper, ResourceFile> implements ResourceFileService {

    @Resource
    ResourceFileMapper resourceFileMapper;

    @Resource
    ResourceUserFileMapper resourceUserFileMapper;
    @Resource
    UFOPFactory ufopFactory;
    @Resource
    ResourceFileDealComp resourceFileDealComp;
    @Resource
    UploadTaskDetailMapper uploadTaskDetailMapper;
    @Resource
    UploadTaskMapper uploadTaskMapper;
    @Resource
    ImageMapper imageMapper;
    @Resource
    PictureFileMapper pictureFileMapper;



    @Resource
    OperationLogUtil operationLogUtil;

    public static final String SUCCESS = "操作成功";

    public static final String CURRENT_MODULE = "文件传输接口";

    public static final String FAIL = "操作失败";
    public static Executor exec = Executors.newFixedThreadPool(300);

    @Override
    public UploadFileVo uploadFileSpeed(HttpServletRequest request,String userId,UploadFileDTO uploadFileDTO) {
        UploadFileVo uploadFileVo = new UploadFileVo();
        String funcType="0";
        if(StringUtils.isNotEmpty(uploadFileDTO.getFuncType()))funcType=uploadFileDTO.getFuncType();

        Map<String, Object> param = new HashMap<>();
        //看看该md5批次号有没有对应存在的文件
        param.put("identifier", uploadFileDTO.getIdentifier());
        List<ResourceFile> list = resourceFileMapper.selectByMap(param);

        String filePath = uploadFileDTO.getFilePath();
        String relativePath = uploadFileDTO.getRelativePath();
        QiwenFile qiwenFile = null;
        if (relativePath.contains("/")) {
            qiwenFile = new QiwenFile(filePath, relativePath, false);
        } else {
            qiwenFile = new QiwenFile(filePath, uploadFileDTO.getFilename(), false);
        }

        //如果有这个文件，取第一个重新绑定一下该用户即可
        if (list != null && !list.isEmpty()) {
            ResourceFile file = list.get(0);
            ResourceUserFile resourceUserFile = new ResourceUserFile(qiwenFile, userId, file.getFileId());

            try {
                resourceUserFile.setIsCollet("0");
                resourceUserFile.setFuncType(funcType);
                resourceUserFileMapper.insert(resourceUserFile);
                uploadFileVo.setUploadStatus("1");
            } catch (Exception e) {
                log.warn("极速上传文件冲突重命名处理: {}", JSON.toJSONString(resourceUserFile));

            }

            //此处
            if (relativePath.contains("/")) {
                QiwenFile finalQiwenFile = qiwenFile;
                exec.execute(()->{
                    resourceFileDealComp.restoreParentFilePath(finalQiwenFile, userId);
                });

            }
            uploadFileVo.setSkipUpload(true);
            uploadFileVo.setUserFileId(resourceUserFile.getUserFileId());

        } else {
            uploadFileVo.setSkipUpload(false);
            uploadFileVo.setUploadStatus("0");
            List<Integer> uploaded = uploadTaskDetailMapper.selectUploadedChunkNumList(uploadFileDTO.getIdentifier());
            if (uploaded != null && !uploaded.isEmpty()) {
                uploadFileVo.setUploaded(uploaded);
            } else {

                LambdaQueryWrapper<UploadTask> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(UploadTask::getIdentifier, uploadFileDTO.getIdentifier());
                List<UploadTask> rslist = uploadTaskMapper.selectList(lambdaQueryWrapper);
                if (rslist == null || rslist.isEmpty()) {
                    UploadTask uploadTask = new UploadTask();
                    uploadTask.setIdentifier(uploadFileDTO.getIdentifier());
                    uploadTask.setUploadTime(DateUtil.getCurrentTime());
                    uploadTask.setUploadStatus(UploadFileStatusEnum.UNCOMPLATE.getCode());
                    uploadTask.setFileName(qiwenFile.getNameNotExtend());
                    uploadTask.setFilePath(qiwenFile.getParent());
                    uploadTask.setExtendName(qiwenFile.getExtendName());
                    uploadTask.setUserId(userId);
                    uploadTaskMapper.insert(uploadTask);
                }
            }

        }


        return uploadFileVo;
    }

    @Override
    public UploadFileVo uploadFile(HttpServletRequest request, UploadFileDTO uploadFileDto, String userId) {
        UploadFileVo uploadFileVo = new UploadFileVo();
        String funcType="0";
        if(StringUtils.isNotEmpty(uploadFileDto.getFuncType()))funcType=uploadFileDto.getFuncType();
        UploadFile uploadFile = new UploadFile();
        uploadFile.setChunkNumber(uploadFileDto.getChunkNumber());
        uploadFile.setChunkSize(uploadFileDto.getChunkSize());
        uploadFile.setTotalChunks(uploadFileDto.getTotalChunks());
        uploadFile.setIdentifier(uploadFileDto.getIdentifier());
        uploadFile.setTotalSize(uploadFileDto.getTotalSize());
        uploadFile.setCurrentChunkSize(uploadFileDto.getCurrentChunkSize());

        Uploader uploader = ufopFactory.getUploader();
        if (uploader == null) {
            log.error("上传失败，请检查storageType是否配置正确");
            throw new UploadException("上传失败");
        }
        List<UploadFileResult> uploadFileResultList;
        try {
            uploadFileResultList = uploader.upload(request, uploadFile);
        } catch (Exception e) {
            log.error("上传失败，请检查UFOP连接配置是否正确");
            throw new UploadException("上传失败", e);
        }
        for (int i = 0; i < uploadFileResultList.size(); i++){
            UploadFileResult uploadFileResult = uploadFileResultList.get(i);
            String relativePath = uploadFileDto.getRelativePath();
            QiwenFile qiwenFile = null;
            if (relativePath.contains("/")) {
                qiwenFile = new QiwenFile(uploadFileDto.getFilePath(), relativePath, false);
            } else {
                qiwenFile = new QiwenFile(uploadFileDto.getFilePath(), uploadFileDto.getFilename(), false);
            }

            if (UploadFileStatusEnum.SUCCESS.equals(uploadFileResult.getStatus())){
                ResourceFile resourceFile = new ResourceFile(uploadFileResult);
                resourceFile.setCreateUserId(userId);
                try {
                    resourceFileMapper.insert(resourceFile);
                } catch (Exception e) {
                    log.warn("identifier Duplicate: {}", resourceFile.getIdentifier());
                    resourceFile = resourceFileMapper.selectOne(new QueryWrapper<ResourceFile>().lambda().eq(ResourceFile::getIdentifier, resourceFile.getIdentifier()));
                }

                ResourceUserFile resourceUserFile = new ResourceUserFile(qiwenFile, userId, resourceFile.getFileId());


                try {
                    resourceUserFile.setIsCollet("0");
                    resourceUserFile.setFuncType(funcType);
                    resourceUserFileMapper.insert(resourceUserFile);

                } catch (Exception e) {
                    ResourceUserFile userFile1 = resourceUserFileMapper.selectOne(new QueryWrapper<ResourceUserFile>().lambda()
                            .eq(ResourceUserFile::getUserId, resourceUserFile.getUserId())
                            .eq(ResourceUserFile::getFilePath, resourceUserFile.getFilePath())
                            .eq(ResourceUserFile::getFileName, resourceUserFile.getFileName())
                            .eq(ResourceUserFile::getExtendName, resourceUserFile.getExtendName())
                            .eq(ResourceUserFile::getDeleteFlag, resourceUserFile.getDeleteFlag())
                            .eq(ResourceUserFile::getIsDir, resourceUserFile.getIsDir()));
                    ResourceFile file1 = resourceFileMapper.selectById(userFile1.getFileId());
                    if (!StringUtils.equals(resourceFile.getIdentifier(), file1.getIdentifier())) {
                        log.warn("文件冲突重命名处理: {}", JSON.toJSONString(userFile1));
                        String fileName = resourceFileDealComp.getRepeatFileName(resourceUserFile, resourceUserFile.getFilePath());
                        resourceUserFile.setFileName(fileName);
                        resourceUserFileMapper.insert(resourceUserFile);
                    }
                }
                uploadFileVo.setUserFileId(resourceUserFile.getUserFileId());
                uploadFileVo.setUploadStatus("1");

                if (relativePath.contains("/")) {
                    QiwenFile finalQiwenFile = qiwenFile;
                    exec.execute(()->{
                        resourceFileDealComp.restoreParentFilePath(finalQiwenFile, userId);
                    });
                }

                LambdaQueryWrapper<UploadTaskDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(UploadTaskDetail::getIdentifier, uploadFileDto.getIdentifier());
                uploadTaskDetailMapper.delete(lambdaQueryWrapper);

                LambdaUpdateWrapper<UploadTask> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lambdaUpdateWrapper.set(UploadTask::getUploadStatus, UploadFileStatusEnum.SUCCESS.getCode())
                        .eq(UploadTask::getIdentifier, uploadFileDto.getIdentifier());
                uploadTaskMapper.update(null, lambdaUpdateWrapper);


                try {
                    if (UFOPUtils.isImageFile(uploadFileResult.getExtendName())) {
                        BufferedImage src = uploadFileResult.getBufferedImage();
                        Image image = new Image();
                        image.setImageWidth(src.getWidth());
                        image.setImageHeight(src.getHeight());
                        image.setFileId(resourceFile.getFileId());
                        imageMapper.insert(image);
                    }
                } catch (Exception e) {
                    log.error("生成图片缩略图失败！", e);
                }

                resourceFileDealComp.parseMusicFile(uploadFileResult.getExtendName(), uploadFileResult.getStorageType().getCode(), uploadFileResult.getFileUrl(), resourceFile.getFileId());

            } else if (UploadFileStatusEnum.UNCOMPLATE.equals(uploadFileResult.getStatus())) {
                UploadTaskDetail uploadTaskDetail = new UploadTaskDetail();
                uploadTaskDetail.setFilePath(qiwenFile.getParent());
                uploadTaskDetail.setFilename(qiwenFile.getNameNotExtend());
                uploadTaskDetail.setChunkNumber(uploadFileDto.getChunkNumber());
                uploadTaskDetail.setChunkSize((int)uploadFileDto.getChunkSize());
                uploadTaskDetail.setRelativePath(uploadFileDto.getRelativePath());
                uploadTaskDetail.setTotalChunks(uploadFileDto.getTotalChunks());
                uploadTaskDetail.setTotalSize((int)uploadFileDto.getTotalSize());
                uploadTaskDetail.setIdentifier(uploadFileDto.getIdentifier());
                uploadTaskDetailMapper.insert(uploadTaskDetail);
                uploadFileVo.setUploadStatus("0");

            } else if (UploadFileStatusEnum.FAIL.equals(uploadFileResult.getStatus())) {
                LambdaQueryWrapper<UploadTaskDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(UploadTaskDetail::getIdentifier, uploadFileDto.getIdentifier());
                uploadTaskDetailMapper.delete(lambdaQueryWrapper);

                LambdaUpdateWrapper<UploadTask> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lambdaUpdateWrapper.set(UploadTask::getUploadStatus, UploadFileStatusEnum.FAIL.getCode())
                        .eq(UploadTask::getIdentifier, uploadFileDto.getIdentifier());
                uploadTaskMapper.update(null, lambdaUpdateWrapper);
                uploadFileVo.setUploadStatus("2");
            }
        }
        uploadFileVo.setSkipUpload(false);
        return uploadFileVo;
    }


    @Override
    public void downloadFile(HttpServletResponse httpServletResponse, DownloadFileDTO downloadFileDTO) {
        ResourceUserFile resourceUserFile = resourceUserFileMapper.selectById(downloadFileDTO.getUserFileId());

        if (resourceUserFile.isFile()) {

            ResourceFile fileBean = resourceFileMapper.selectById(resourceUserFile.getFileId());
            Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType());
            if (downloader == null) {
                log.error("下载失败，文件存储类型不支持下载，storageType:{}", fileBean.getStorageType());
                throw new DownloadException("下载失败");
            }
            DownloadFile downloadFile = new DownloadFile();

            downloadFile.setFileUrl(fileBean.getFileUrl());
            httpServletResponse.setContentLengthLong(fileBean.getFileSize());
            downloader.download(httpServletResponse, downloadFile);
        } else {

            QiwenFile qiwenFile = new QiwenFile(resourceUserFile.getFilePath(), resourceUserFile.getFileName(), true);
            List<ResourceUserFile> userFileList = resourceUserFileMapper.selectUserFileByLikeRightFilePath(qiwenFile.getPath() , resourceUserFile.getUserId());
            List<String> userFileIds = userFileList.stream().map(ResourceUserFile::getUserFileId).collect(Collectors.toList());

            downloadUserFileList(httpServletResponse, resourceUserFile.getFilePath(), resourceUserFile.getFileName(), userFileIds);
        }
    }

    @Override
    public void downloadUserFileList(HttpServletResponse httpServletResponse, String filePath, String fileName, List<String> userFileIds) {
        String staticPath = UFOPUtils.getStaticPath();
        String tempPath = staticPath + "temp" + File.separator;
        File tempDirFile = new File(tempPath);
        if (!tempDirFile.exists()) {
            tempDirFile.mkdirs();
        }

        FileOutputStream f = null;
        try {
            f = new FileOutputStream(tempPath + fileName + ".zip");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
        ZipOutputStream zos = new ZipOutputStream(csum);
        BufferedOutputStream out = new BufferedOutputStream(zos);

        try {
            for (String userFileId : userFileIds) {
                ResourceUserFile resourceUserFile1 = resourceUserFileMapper.selectById(userFileId);
                if (resourceUserFile1.isFile()) {
                    ResourceFile fileBean = resourceFileMapper.selectById(resourceUserFile1.getFileId());
                    Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType());
                    if (downloader == null) {
                        log.error("下载失败，文件存储类型不支持下载，storageType:{}", fileBean.getStorageType());
                        throw new UploadException("下载失败");
                    }
                    DownloadFile downloadFile = new DownloadFile();
                    downloadFile.setFileUrl(fileBean.getFileUrl());
                    InputStream inputStream = downloader.getInputStream(downloadFile);
                    BufferedInputStream bis = new BufferedInputStream(inputStream);
                    try {
                        QiwenFile qiwenFile = new QiwenFile(StrUtil.removePrefix(resourceUserFile1.getFilePath(), filePath), resourceUserFile1.getFileName() + "." + resourceUserFile1.getExtendName(), false);
                        zos.putNextEntry(new ZipEntry(qiwenFile.getPath()));

                        byte[] buffer = new byte[1024];
                        int i = bis.read(buffer);
                        while (i != -1) {
                            out.write(buffer, 0, i);
                            i = bis.read(buffer);
                        }
                    } catch (IOException e) {
                        log.error("" + e);
                        e.printStackTrace();
                    } finally {
                        IOUtils.closeQuietly(bis);
                        try {
                            out.flush();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    QiwenFile qiwenFile = new QiwenFile(StrUtil.removePrefix(resourceUserFile1.getFilePath(), filePath), resourceUserFile1.getFileName(), true);
                    // 空文件夹的处理
                    zos.putNextEntry(new ZipEntry(qiwenFile.getPath() + QiwenFile.separator));
                    // 没有文件，不需要文件的copy
                    zos.closeEntry();
                }
            }

        } catch (Exception e) {
            log.error("压缩过程中出现异常:" + e);
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void previewPictureFile(HttpServletResponse httpServletResponse, PreviewDTO previewDTO) {
        byte[] bytesUrl = Base64.getDecoder().decode(previewDTO.getUrl());
        PictureFile pictureFile = new PictureFile();
        pictureFile.setFileUrl(new String(bytesUrl));
        pictureFile = pictureFileMapper.selectOne(new QueryWrapper<>(pictureFile));
        Previewer previewer = ufopFactory.getPreviewer(pictureFile.getStorageType());
        if (previewer == null) {
            log.error("预览失败，文件存储类型不支持预览，storageType:{}", pictureFile.getStorageType());
            throw new UploadException("预览失败");
        }
        PreviewFile previewFile = new PreviewFile();
        previewFile.setFileUrl(pictureFile.getFileUrl());
//        previewFile.setFileSize(pictureFile.getFileSize());
        try {

            String mime= MimeUtils.getMime(pictureFile.getExtendName());
            httpServletResponse.setHeader("Content-Type", mime);

            String fileName = pictureFile.getFileName() + "." + pictureFile.getExtendName();
            try {
                fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            httpServletResponse.addHeader("Content-Disposition", "fileName=" + fileName);// 设置文件名

            previewer.imageOriginalPreview(httpServletResponse, previewFile);
        } catch (Exception e){
            //org.apache.catalina.connector.ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。
            if (e.getMessage().contains("ClientAbortException")) {
                //该异常忽略不做处理
            } else {
                log.error("预览文件出现异常：{}", e.getMessage());
            }

        }
    }

    @Override
    public void previewFile(HttpServletResponse httpServletResponse, PreviewDTO previewDTO) {
        ResourceUserFile resourceUserFile = resourceUserFileMapper.selectById(previewDTO.getUserFileId());
        ResourceFile resourceFile = resourceFileMapper.selectById(resourceUserFile.getFileId());
        Previewer previewer = ufopFactory.getPreviewer(resourceFile.getStorageType());
        if (previewer == null) {
            log.error("预览失败，文件存储类型不支持预览，storageType:{}", resourceFile.getStorageType());
            throw new UploadException("预览失败");
        }
        PreviewFile previewFile = new PreviewFile();
        previewFile.setFileUrl(resourceFile.getPreviewFileUrl());
        try {
            if ("true".equals(previewDTO.getIsMin())) {
                previewer.imageThumbnailPreview(httpServletResponse, previewFile);
            } else {
                previewer.imageOriginalPreview(httpServletResponse, previewFile);
            }
        } catch (Exception e){
            //org.apache.catalina.connector.ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。
            if (e.getMessage().contains("ClientAbortException")) {
                //该异常忽略不做处理
            } else {
                log.error("预览文件出现异常：{}", e.getMessage());
            }

        }

    }

    @Override
    public UploadFileVo eachOtherExchange(HttpServletRequest request, EachOtherExchangeDTO UploadFileDto, String userId) {
        UploadFileVo uploadFileVo = new UploadFileVo();
        ResourceUserFile oldResourceUserFile = resourceUserFileMapper.selectById(UploadFileDto.getUserfileId());
        QiwenFile qiwenFile = null;
        qiwenFile = new QiwenFile(oldResourceUserFile.getFilePath(), oldResourceUserFile.getFileName(), false);
        ResourceUserFile resourceUserFile = new ResourceUserFile(qiwenFile, userId, oldResourceUserFile.getFileId());

        try {
            resourceUserFile.setIsCollet("0");
            resourceUserFile.setFuncType(UploadFileDto.getFuncType());
            resourceUserFileMapper.insert(resourceUserFile);
            uploadFileVo.setUploadStatus("1");
        } catch (Exception e) {
            log.warn("极速上传文件冲突重命名处理: {}", JSON.toJSONString(resourceUserFile));

        }

        //此处
        if (oldResourceUserFile.getFilePath().contains("/")) {
            QiwenFile finalQiwenFile = qiwenFile;
            exec.execute(() -> {
                resourceFileDealComp.restoreParentFilePath(finalQiwenFile, userId);
            });

        }
        uploadFileVo.setSkipUpload(true);
        uploadFileVo.setUserFileId(resourceUserFile.getUserFileId());

        return uploadFileVo;
    }



    @Override
    public Long selectStorageSizeByUserId(String userId) {
        return resourceUserFileMapper.selectStorageSizeByUserId(userId);
    }


}
