/*
 * Copyright (C) 2025 ctecinf.com.br
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.com.hobbyslotcar.racedirector;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 *
 * @author Cássio Conceição
 * @since 27/07/2023
 * @version 2503
 * @see http://hobbyslotcar.com.br
 */
public class Img {

    public static ImageIcon get(String name) {
        try {
            return new ImageIcon(ImageIO.read(Img.class.getResourceAsStream("img" + File.separator + name.replace(".png", "") + ".png")));
        } catch (IOException ex) {
            return null;
        }
    }

    public static ImageIcon parse(String path) throws IOException {
        return Img.parse(Img.create(path), 0);
    }

    public static ImageIcon parse(String path, int scale) throws IOException {
        return Img.parse(Img.create(path), scale);
    }

    public static ImageIcon parse(InputStream inputStream) throws IOException {
        return Img.parse(Img.create(inputStream), 0);
    }

    public static ImageIcon parse(InputStream inputStream, int scale) throws IOException {
        return Img.parse(Img.create(inputStream), scale);
    }

    public static ImageIcon parse(File file) throws IOException {
        return Img.parse(Img.create(file), 0);
    }

    public static ImageIcon parse(File file, int scale) throws IOException {
        return Img.parse(Img.create(file), scale);
    }

    public static ImageIcon parse(Image img, int scale) throws IOException {
        int scaledHeight = scale > 0 ? scale : img.getHeight(null);
        int scaledWidth = scale > 0 ? scale : img.getWidth(null);
        double imageHeight = img.getHeight(null);
        double imageWidth = img.getWidth(null);
        if (imageHeight / scaledHeight > imageWidth / scaledWidth) {
            scaledWidth = (int) (scaledHeight * imageWidth / imageHeight);
        } else {
            scaledHeight = (int) (scaledWidth * imageHeight / imageWidth);
        }
        return new ImageIcon(img.getScaledInstance(scaledWidth, scaledHeight, java.awt.Image.SCALE_DEFAULT));
    }

    private static BufferedImage create(Object obj) throws IOException {
        if (obj instanceof String) {
            return ImageIO.read(new File(obj.toString()));
        } else if (obj instanceof File) {
            return ImageIO.read((File) obj);
        } else if (obj instanceof InputStream) {
            return ImageIO.read((InputStream) obj);
        } else {
            return null;
        }
    }
}
