package eu.midnightdust.yaytris.util;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

public class FileUtil {
    public static BufferedImage loadImage(String location) {
        try (InputStream fileStream = FileUtil.class.getResourceAsStream(location)) {
            assert fileStream != null;
            return ImageIO.read(fileStream);
        } catch (IOException | NullPointerException ex) {
            throw new RuntimeException(ex);
        }
    }

    public static AudioInputStream loadAudio(String location) {
        try (InputStream fileStream = FileUtil.class.getResourceAsStream(location)) {
            assert fileStream != null;
            return AudioSystem.getAudioInputStream(fileStream);
        } catch (IOException | NullPointerException | UnsupportedAudioFileException ex) {
            throw new RuntimeException(ex);
        }
    }

    public static InputStream getFileStream(String location) {
        try (InputStream fileStream = FileUtil.class.getResourceAsStream(location)) {
            return fileStream;
        } catch (IOException | NullPointerException ex) {
            throw new RuntimeException(ex);
        }
    }
}
