package net.educoder.ecsonar.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.ZipUtil;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * @Author: youys
 * @Date: 2023/5/19
 * @Description:
 */
public class RarUtil {

    public static void unrar(String sourceFilePath, String outputDirectory) {
        String s = RuntimeUtil.execForStr("unar", sourceFilePath,"-o", outputDirectory);
        System.out.println("result====" + s);
    }


    public static void un7Z(String sourceFilePath, String outputDirectory) {
        String s = RuntimeUtil.execForStr("unar", sourceFilePath,"-o", outputDirectory);
        System.out.println("result====" + s);
    }


    public static void main(String[] args) {
        //        unrar("/tmp/20230301/201905962222/20230519153409_wuiol3vn.rar", "/Users/youyongsheng/Desktop/aa");
//        un7Z("/tmp/20230301/201905962241/20230519153506_i39sv5p2.7z", "/Users/youyongsheng/Desktop/aa");

//        unzip();
            valid("/tmp/20230402");
//        removeFullCode("/tmp/20230302");
    }


    public static void unzip(){
        String sourceDir = "/tmp/20230401";
        String targetDir = "/tmp/20230402";

        File directory = new File(sourceDir);
        File[] studentDirs = directory.listFiles();
        if (studentDirs == null) {
            System.out.println("No student directories found.");
            return;
        }

        int i=0;
        for (File studentDir : studentDirs) {
            if (!studentDir.isDirectory()) {
                System.out.println("111--------------------------------");
                continue;
            }



            String studentId = studentDir.getName();
            String studentTargetDir = targetDir + "/" + studentId;

            File[] zipFiles = studentDir.listFiles();
//            File[] zipFiles = studentDir.listFiles((dir, name) -> name.endsWith(".zip"));
//            if (zipFiles == null) {
//                System.out.println("No zip files found for student: " + studentId);
//                continue;
//            }


            for (File zipFile : zipFiles) {
                try {
                    if(zipFile.getName().endsWith(".zip")){
                        unzip(zipFile.getAbsolutePath(), studentTargetDir);
                    }else if(zipFile.getName().endsWith(".rar")){
                        unRar(zipFile.getAbsolutePath(), studentTargetDir);
                    }else{
                        RarUtil.un7Z(zipFile.getAbsolutePath(), studentTargetDir);
                    }
                } catch (Exception e) {
                    System.out.println("Error occurred while unzipping file: " + zipFile.getName());
                }
            }

            System.out.println("--------------------------------");
        }
    }

    private static void unzip(String zipFilePath, String targetDir) throws IOException {
        System.out.println(zipFilePath + " to " + targetDir);
        ZipUtil.unzip(zipFilePath,targetDir, Charset.forName("GBK"));
    }

    private static void unRar(String zipFilePath, String targetDir) throws Exception {
        System.out.println(zipFilePath + " to " + targetDir);
        RarUtil.unrar(zipFilePath,targetDir);
    }


    public static void valid(String path){
        File directory = new File(path);
        File[] studentDirs = directory.listFiles();
        if (studentDirs == null) {
            System.out.println("No student directories found.");
            return;
        }

        for (File studentDir : studentDirs) {
            if (studentDir.isDirectory()) {

                File[] files = studentDir.listFiles();
                if(files == null || files.length == 0){
                    System.out.println(studentDir.getName()+" -> null");
                }
                for (File file : files) {
                    System.out.println(studentDir.getName()+" ->" + file.getName());
                }
                System.out.println("--------------------------------");
            }
        }

    }

    public static void removeFullCode(String path){
        // 删除完整代码
        File directory = new File(path);
        File[] studentDirs = directory.listFiles();
        if (studentDirs == null) {
            System.out.println("No student directories found.");
            return;
        }

        for (File studentDir : studentDirs) {
            if (studentDir.isDirectory()) {

                String stuNo = studentDir.getName();
                File[] files = studentDir.listFiles();
                if(files.length == 2){
                    for (File file : files) {
                        if (file.getName().contains("完整")) {
                            System.out.println(stuNo + "==11===" + file.getAbsolutePath());
                            cn.hutool.core.io.FileUtil.del(file.getAbsolutePath());
                        }
                    }

                }else if(files.length == 1){
                    File file = files[0];
                    File[] files1 = file.listFiles();
                    if(files1 != null && files1.length == 2){
                        for (File f : files1) {
                            if (f.getName().contains("完整")) {
                                System.out.println(stuNo + "==22===" + f.getAbsolutePath());
                                FileUtil.del(f.getAbsolutePath());
                            }
                        }

                    }
                }

            }
        }
    }
}
