package com.axis.innovators.box.gui;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;

/**
 * 负责加载图片
 * @author tzdwindows 7
 */
public class LoadIcon {
    private static final Logger logger = LogManager.getLogger(LoadIcon.class);
    private static final String ICON_PATH = "/icons/";
    public static ImageIcon loadIcon(String filename, int size) {
        try {
            String fullPath = ICON_PATH + filename;
            URL imgUrl = LoadIcon.class.getResource(fullPath);
            if (imgUrl == null) {
                return createPlaceholderIcon(size);
            }
            Image image = new ImageIcon(imgUrl).getImage();
            return new ImageIcon(image.getScaledInstance(size, size, Image.SCALE_SMOOTH));
        } catch (Exception e) {
            logger.error("Failed to load icon image path '{}'", filename, e);
            return createPlaceholderIcon(size);
        }
    }

    private static ImageIcon createPlaceholderIcon(int size) {
        BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setColor(Color.LIGHT_GRAY);
        g2d.fillRect(0, 0, size, size);
        g2d.dispose();
        return new ImageIcon(img);
    }
}
