/*
 * Copyright (C) 2025  Tim Zimmermann <tim@aglais.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.aglais.zms.util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

/**
 * Utility class for handling resources, particularly image resources.
 * Provides methods for retrieving image resources from the classpath.
 * This class is designed to be used as a static utility and should not be instantiated.
 */
public class ResourceUtil {

    private static final String LOG_TAG = ResourceUtil.class.getSimpleName();

    /**
     * The name of the application icon.
     */
    public static final String APP_ICON_NAME = "aglais_io";

    /**
     * The extension of the application icon.
     */
    public static final String APP_ICON_EXTENSION = "png";

    /**
     * Retrieves an image resource by its name.
     *
     * @param name The name of the image resource to retrieve.
     * @return The {@link BufferedImage} object representing the image, or null if the image could not be read.
     */
    public static BufferedImage getImage(String name) {
        URL iconURL = ResourceUtil.class.getResource("/images/" + name);

        if (iconURL != null) {
            try {
                return ImageIO.read(iconURL);
            } catch (IOException e) {
                Log.e(LOG_TAG, "Failed to read icon", e);
            }
        }

        return null;
    }

}