package com.swiftcleaner.chovey.model.tool;

/**
 * 作者：马海钊
 * 时间：2024/12/11 21:45
 * 功能：
 */
import android.os.Handler;
import android.os.Looper;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FileDeleter {

    private ExecutorService executorService;

    public FileDeleter() {
        this.executorService = Executors.newCachedThreadPool();
    }

    public FileDeleter deleteFiles(List<String> filePaths, OnFilesDeletedListener listener) {
        executorService.execute(() -> {
            for (String filePath : filePaths) {
                File file = new File(filePath);
                if (file.exists() && file.delete()) {
                    // 文件删除成功，立即回调
                    new Handler(Looper.getMainLooper()).post(() -> {
                        if (listener != null) {
                            listener.onFileDeleted(true, filePath);
                        }
                    });
                } else {
                    // 文件删除失败，立即回调
                    new Handler(Looper.getMainLooper()).post(() -> {
                        if (listener != null) {
                            listener.onFileDeleted(false, filePath);
                        }
                    });
                }
            }
        });
        return this; // 返回当前实例
    }

    public void shutdown() {
        executorService.shutdown(); // 关闭线程池
    }

    public interface OnFilesDeletedListener {
        void onFileDeleted(boolean success, String filePath);
    }
}