package cz.jzitnik.tui;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import lombok.extern.slf4j.Slf4j;

/**
 * Utility class responsible for loading text-based resources (e.g., ANSI art) from the classpath.
 */
@Slf4j
public class ResourceLoader {
    /**
     * Loads a resource file as a UTF-8 encoded string from the <code>textures/</code> directory on the classpath.
     *
     * @param fileName Name of the file to load (relative to <code>textures/</code>).
     * @return Contents of the file as a string, or {@code null} if the file is not found or an I/O error occurs.
     */
    public static String loadResource(String fileName) {
        log.info("Loading resource: {}", "textures/" + fileName);
        try (InputStream inputStream = ResourceLoader.class.getClassLoader()
                .getResourceAsStream("textures/" + fileName)) {
            if (inputStream == null) {
                // If the file is not found, return null
                return null;
            }

            byte[] bytes = inputStream.readAllBytes();
            return new String(bytes, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
