package com.lemenzo.gallery.util;

import android.os.Process;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/** Background executors for MediaStore/index/search work so scrolling and rendering keep priority. */
public final class BackgroundExecutors {
    private static final AtomicInteger IDS = new AtomicInteger();

    private BackgroundExecutors() {}

    public static ExecutorService single(String name) {
        final String prefix = name == null || name.trim().isEmpty() ? "LemenzoGalleryBg" : name;
        ThreadFactory factory = runnable -> {
            Thread thread = new Thread(() -> {
                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                runnable.run();
            }, prefix + "-" + IDS.incrementAndGet());
            thread.setDaemon(true);
            return thread;
        };
        return Executors.newSingleThreadExecutor(factory);
    }
}
