package com.hh.hhomc.utils;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Description:
 * @author xwh
 * time 2024/7/11
 **/
public class ZipUtils {
    private static String TAG=ZipUtils.class.getName();

    public static String getTempZipDir() {
        //获取外存路径
        String externalStoragePath = PathUtils.getExternalAppFilesPath();
        //命名存储目录
        String logPath = externalStoragePath + File.separator + "TempZipFiles";
        //判断目录是否存在，不存在则判断是否建立成功
        FileUtils.createOrExistsDir(logPath);
        Log.i(TAG, "getLogcatDir = " + logPath);
        return logPath;
    }


    public static String createFileName() {
        String fileName = "";
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.CHINA);

        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);

        String curDate = dateFormat.format(calendar.getTime());
        //品牌+型号
        String model = Build.MODEL;

        fileName = model + "_" +DeviceUtils.getDeviceSN()+ "_"  + curDate  + ".zip";
        Log.i(TAG, "createFileName = " + fileName);
        return fileName;
    }
    /**
     * 
     * Description: 压缩ZIP
     * @author xwh
     * Time 2024/7/11
     *
    **/
    public static synchronized String createZipFolder(Context context,List<String> folderPath) throws IOException {
        synchronized (ZipUtils.class) {
            String logcatDir = getTempZipDir();
            String fileName = createFileName();
            String outputPath = logcatDir + File.separator + fileName;
            FileOutputStream fos = new FileOutputStream(outputPath);
            ZipOutputStream zipOut = new ZipOutputStream(fos);
            addFilesToZip(zipOut, folderPath, "");
            zipOut.close();
            fos.close();
            return outputPath;
        }
    }

    /**
     * 
     * Description: 多文件压缩，不支持文件夹
     * @author xwh
     * Time 2024/7/11
     *
    **/
    private static synchronized void addFilesToZip(ZipOutputStream zipOut, List<String> folderPaths, String parentFolderName) throws IOException {
       if(folderPaths!=null&&folderPaths.size()>0){
           for (int i=0;i<folderPaths.size();i++){
               File file=new File(folderPaths.get(i));
               if (!file.isDirectory()) {
                   byte[] buffer = new byte[1024];
                   FileInputStream fis = new FileInputStream(file);
                   zipOut.putNextEntry(new ZipEntry(parentFolderName + file.getName()));
                   int length;
                   while ((length = fis.read(buffer)) > 0) {
                       zipOut.write(buffer, 0, length);
                   }
                   fis.close();
               }
           }



       }

    }
}
