package me.teridax.jcash.gui;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Objects;

import static me.teridax.jcash.Logging.LOGGER;

/**
 * Static class for providing the capabilities to load images from file.
 */
public class IconProvider {

    private static final Image DEFAULT_IMAGE = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);

    /**
     * Fetches the windows icon.
     * @return the windows icon
     */
    public static Image getWindowIcon() {
        return loadIcon("res/register.png");
    }

    /**
     * Loads the specified image from disk.
     * If the file cannot be made into an image because its corrupted or the file cannot be read,
     * the default image is returned {@link #DEFAULT_IMAGE}
     * @param path the path to the image
     * @return the specified image or {@link #DEFAULT_IMAGE}
     */
    private static Image loadIcon(String path) {
        try {
            var is = Objects.requireNonNull(IconProvider.class.getResourceAsStream(path));
            return ImageIO.read(is);
        } catch (Exception e) {
            LOGGER.severe("Unable to load icon " + path + " because: " + e.getMessage());
        }

        return DEFAULT_IMAGE;
    }

    /**
     * Fetches the background image used for the login screen
     * @return login screen background image
     */
    public static Image getBackground() {
        return loadIcon("res/background.png");
    }
}
