package com.storlead.mail.util;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-07-24 14:55
 */
import com.storlead.mail.pojo.entity.MailAttachmentEntity;
import com.storlead.mail.properties.MailFileProperties;
import lombok.extern.log4j.Log4j2;

import javax.mail.Message;
import java.io.*;
import java.util.List;
import java.util.zip.*;

@Log4j2
public class ZipUtility {

    public static void zipFiles(List<MailAttachmentEntity> attachments, String outputZipFile, MailFileProperties mailFileProperties) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(outputZipFile);
             ZipOutputStream zos = new ZipOutputStream(fos)) {

            for (MailAttachmentEntity attachment : attachments) {
                String filePath = mailFileProperties.getPath().getPath() + File.separator + attachment.getFilePath();
                File file = new File(filePath);
                if (file.exists() && !file.isDirectory()) {
                    try (FileInputStream fis = new FileInputStream(file)) {
                        ZipEntry zipEntry = new ZipEntry(attachment.getFileName()+"."+attachment.getFileExt());
                        zos.putNextEntry(zipEntry);

                        byte[] buffer = new byte[1024];
                        int length;
                        while ((length = fis.read(buffer)) > 0) {
                            zos.write(buffer, 0, length);
                        }
                        zos.closeEntry();
                    }
                } else {
                    System.err.println("File not found: " + filePath);
                }
            }
        }
    }

    public static void zipFiles(File sourceDir, File zipFile) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(zipFile);
             ZipOutputStream zipOut = new ZipOutputStream(fos)) {
            File[] files = sourceDir.listFiles();
            for (File file : files) {
                try (FileInputStream fis = new FileInputStream(file)) {
                    ZipEntry zipEntry = new ZipEntry(file.getName());
                    zipOut.putNextEntry(zipEntry);
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = fis.read(buffer)) >= 0) {
                        zipOut.write(buffer, 0, length);
                    }
                    zipOut.closeEntry();
                }
            }
            System.out.println("ZIP file created: " + zipFile.getAbsolutePath());
        }
    }

    // 删除临时目录及其内容
    public static void deleteDirectory(File directory) {
        File[] files = directory.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    deleteDirectory(file);
                } else {
                    file.delete();
                }
            }
        }
        directory.delete();
    }

    public static void saveEmlFile(Message message, String filename) {
        try (FileOutputStream fos = new FileOutputStream(filename)) {
            message.writeTo(fos);  // 将邮件内容写入文件
            System.out.println("EML file saved as " + filename);
        } catch (Exception e) {
            log.error("saveEmlFile--------", e);
        }
    }
}
