package com.jwipc.sys.client.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.jwipc.api.dto.ESignFileResposeDTO;
import com.jwipc.sys.client.FileClient;
import com.jwipc.sys.client.convert.FileClientConvert;
import com.jwipc.sys.client.dto.FileResposeCDTO;
import com.jwipc.sys.client.dto.FileResposePreviewCDTO;
import com.jwipc.sys.dto.FileResposeDTO;
import com.jwipc.sys.entity.Attachment;
import com.jwipc.sys.logic.FileLogic;
import com.jwipc.sys.service.IAttachmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
public class FileClientImpl implements FileClient {
    private final IAttachmentService attachmentService;
    private final FileClientConvert fileClientConvert;
    private final FileLogic fileLogic;

    /**
     * 创建一个空文件
     *
     * @param fileName 文件名称
     * @return 文件
     */
    @Override
    public File createFile(String fileName) {
        return fileLogic.createFile(fileName);
    }

    @Override
    public File createBarFile(String fileName) {
        return fileLogic.createBarFile(fileName);
    }

    @Override
    public FileResposeCDTO uploadFile(File file) {
        MultipartFile cMultiFile = null;
        try {
            cMultiFile = new MockMultipartFile("file", file.getName(), null, new FileInputStream(file));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        FileResposeDTO fileResposeDTO = fileLogic.uploadFile(cMultiFile);
        return fileClientConvert.fileResposeDTO2CDTO(fileResposeDTO);
    }

    @Override
    public ESignFileResposeDTO uploadESignFile(File file) {
        MultipartFile cMultiFile = null;
        try {
            cMultiFile = new MockMultipartFile("file", file.getName(), null, new FileInputStream(file));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        FileResposeDTO fileResposeDTO = fileLogic.uploadFile(cMultiFile);
        return fileClientConvert.fileResposeDTO2ESignDTO(fileResposeDTO);
    }

    @Override
    public MultipartFile file2MultipartFile(File file) {
        MultipartFile cMultiFile = null;
        try {
            cMultiFile = new MockMultipartFile("file", file.getName(), null, new FileInputStream(file));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return cMultiFile;
    }

    public FileResposeCDTO getFileInfo(Long fileId) {
        final Attachment byId = attachmentService.getById(fileId);
        return fileClientConvert.entity2DTO(byId);
    }

    public List<FileResposeCDTO> getFileInfos(Collection<? extends Serializable> idList) {
        if (CollUtil.isEmpty(idList)) {
            return Collections.emptyList();
        }
        final List<Attachment> byId = attachmentService.listByIds(idList);
        return fileClientConvert.entities2DTOs(byId);
    }

    public FileResposeCDTO getFileForDownload(Long fileId) {
        return fileClientConvert.covert(fileLogic.getForDownload(fileId));
    }

    /**
     * 压缩文件zip
     *
     * @param zipFileName 文件名
     * @param idList      附件id
     * @return 压缩后的文件路径
     */
    public String zipFiles(String zipFileName, Collection<? extends Serializable> idList) {
        if (CollUtil.isEmpty(idList)) {
            return StrUtil.EMPTY;
        }
        if (StrUtil.isEmpty(zipFileName)) {
            zipFileName = IdWorker.get32UUID();
        }
        // TODO 压缩文件
        return fileLogic.zipFiles(zipFileName, idList);
    }

    /**
     * 获取文件，带文件预览绝对地址
     *
     * @param idList 文件id
     * @return
     * @throws UnsupportedEncodingException
     */
    public List<FileResposePreviewCDTO> getPreviewUrl(Collection<? extends Serializable> idList)
    {
        return fileClientConvert.convert2(fileLogic.getPreviewUrl(idList));
    }


    public FileResposeCDTO saveSignFile(File file) {
        return fileClientConvert.covert(fileLogic.saveSignFile(file));
    }

    @Override
    public Pair<String, String> getDefaultFileAbsolutePath() {
        final Pair<String, String> stringStringPair = fileLogic.genDatePath();
        return stringStringPair;
    }

    @Override
    public FileResposeCDTO createMultipartFile(MultipartFile file) {
        final FileResposeDTO fileResposeDTO = fileLogic.uploadFile(file);
        return fileClientConvert.fileResposeDTO2CDTO(fileResposeDTO);
    }

    @Override
    public String getRootPath() {
        return fileLogic.getFilePath();
    }
}
