package cc.smtweb.system.bpm.spring.file.attach;

import cc.smtweb.framework.core.util.StringUtil;
import cc.smtweb.system.bpm.spring.config.FileConfigProperties;
import cc.smtweb.system.bpm.util.UtilDownloadFile;
import cc.smtweb.system.bpm.util.UtilFile;
import cc.smtweb.system.bpm.web.sys.base.attach.AttachInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Component
public final class AttachFileWork implements IAttachWorkIntf {
    @Autowired
    private FileConfigProperties properties;

    private String getPath(String remoteFolderPath) {
        String path = properties.getAttachPath();
        if (StringUtil.isNotEmpty(remoteFolderPath) && !path.endsWith("/") && !remoteFolderPath.startsWith("/"))
            path = path + "/" + remoteFolderPath;
        else path = path + remoteFolderPath;
        path = path.replaceAll("-", "");
        UtilFile.makeDir(path);
        if (!path.endsWith("/")) path = path + "/";
        return path;
    }

    private String getFileName(String remoteFolderPath, String fileName) {
        return getPath(remoteFolderPath) + fileName;
    }

    public void saveAttach(String remoteAttachName, String remoteFolderPath, String localAttach, boolean isDelLocal) throws Exception {
        String dstFile = getFileName(remoteFolderPath, remoteAttachName);
        if (isDelLocal) UtilFile.renameTo(localAttach, dstFile);
        else UtilFile.copy(localAttach, dstFile);
    }

    @Override
    public void copyAttach(String srcFileName, String desFileName, String remoteFolderPath, boolean isDelSrc) throws Exception {
        String srcFile = getFileName(remoteFolderPath, srcFileName);
        String dstFile = getFileName(remoteFolderPath, desFileName);
        if (isDelSrc) UtilFile.renameTo(srcFile, dstFile);
        else UtilFile.copy(srcFile, dstFile);
    }


    public void deletePath(final String remotePath) throws Exception {
        UtilFile.delFile(getPath(remotePath));
    }

    public void deleteAttach(final String remotePath, final String remoteName) throws Exception {
        UtilFile.delFile(getFileName(remotePath, remoteName));
    }

    public void downloadAttach(final String remotePath, final String remoteName, final String fileName, final HttpServletResponse response) throws Exception {
        UtilDownloadFile.downfileEx(response, getFileName(remotePath, remoteName), fileName);
    }

    public void downloadAttach(final String remotePath, final String remoteName, final String localPath) throws Exception {
        UtilFile.copy(getFileName(remotePath, remoteName), localPath + "/" + remoteName);
    }

    @Override
    public void downloadAttach(String remotePath, String remoteName, String localPath, String localName) throws Exception {
        UtilFile.copy(getFileName(remotePath, remoteName), localPath + "/" + localName);
    }

    public void showAttachImg(final String remotePath, final String remoteName, final HttpServletResponse response) throws Exception {
        UtilDownloadFile.sendImgFile(response, remoteName);
    }

    public List<String> getAttachListFromFtp(final List<AttachInfo> attachlist, final String ownerType, final String localRoot) throws Exception {
        final List<String> files = new ArrayList<>();
        String localPath = localRoot + "/" + ownerType + "/";
        UtilFile.makeDir(localPath);
        String fileName;
        File file;
        for (AttachInfo attach : attachlist) {
            fileName = localPath + attach.getName();
            file = new File(fileName);
            if (!file.exists())
                UtilFile.copy(getFileName(attach.getPath(), attach.getName()), fileName);
            files.add(fileName);
        }
        return files;
    }

    public void makeRemoteDir(String path) throws Exception {
        UtilFile.makeDir(getPath(path));
    }

    @Override
    public Path getAttachPath(String remotePath, String remoteName) {
        String path = getFileName(remotePath, remoteName);
        return Paths.get(path);
    }
}
