package com.swiftcleaner.chovey.model.tool;

import android.os.Environment;

import com.swiftcleaner.chovey.model.bean.CleanFileBean;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class EmptyFolderScanner {

    public static List<CleanFileBean> scanEmptyFolders() {
        List<CleanFileBean> emptyFolders = new ArrayList<>();
        File rootDir = Environment.getExternalStorageDirectory();
        scanFolder(rootDir, emptyFolders);
        return emptyFolders;
    }

    private static void scanFolder(File folder, List<CleanFileBean> emptyFolders) {
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                boolean isEmpty = true;
                for (File file : files) {
                    if (file.isDirectory()) {
                        // 递归扫描子文件夹
                        scanFolder(file, emptyFolders);
                    } else {
                        // 如果文件夹中有文件，标记为非空
                        isEmpty = false;
                    }
                }
                // 如果文件夹为空，添加到列表
                if (isEmpty) {
                    emptyFolders.add(new CleanFileBean(folder.getAbsolutePath()));
                }
            }
        }
    }
}
