package de.siphalor.was.util;

import java.awt.image.ImageObserver;
import java.io.IOException;
import java.util.function.IntPredicate;
import java.util.function.Predicate;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class Util {
	public static final Logger LOGGER;

	private static final Predicate<?> DUMMY_PREDICATE = o -> true;
	private static final IntPredicate DUMMY_INT_PREDICATE = value -> true;

	private static final Object[] EMPTY_ARRAY = new Object[0];

	@SuppressWarnings("unchecked")
	public static <T> Predicate<T> dummyPredicate() {
		return (Predicate<T>) DUMMY_PREDICATE;
	}

	public static IntPredicate dummyIntPredicate() {
		return DUMMY_INT_PREDICATE;
	}

	@SuppressWarnings("unchecked")
	public static <T> T[] emptyArray() {
		return (T[]) EMPTY_ARRAY;
	}

	public static String pathToId(String packId, String path) {
		int dot = path.lastIndexOf('.');
		if (dot >= 0) {
			path = path.substring(0, dot);
		}
		path = path.replace('/', '.').replace('\\', '.');
		if (packId.isEmpty()) {
			return path;
		}
		return packId + "." + path;
	}

	public static ImageObserver dummyImageObserver() {
		return (img, infoflags, x, y, width, height) -> false;
	}

	static {
		LOGGER = Logger.getLogger("");
		try {
			FileHandler logFile = new FileHandler("was.log");
			logFile.setLevel(Level.FINEST);
			logFile.setFormatter(new SimpleFormatter());
			LOGGER.addHandler(logFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
