package com.axis.innovators.box.gui;

import com.axis.innovators.box.plugins.PluginDescriptor;
import com.axis.innovators.box.plugins.PluginLoader;
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.io.File;
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/";

    /**
     * 加载图片
     * @param filename 图片名
     * @param size 图片大小
     * @return ImageIcon对象
     */
    public static ImageIcon loadIcon(String filename, int size) {
        return loadIcon(LoadIcon.class, filename, size);
    }

    /**
     * 加载图片
     * @param clazz resources包所在的jar
     * @param filename 图片名
     * @param size 图片大小
     * @return ImageIcon对象
     */
    public static ImageIcon loadIcon(Class<?> clazz, String filename, int size) {
        try {
            if (filename.isEmpty()) {
                return createPlaceholderIcon(size);
            }

            if (new File(filename).isAbsolute()) {
                return loadAbsolutePathIcon(filename, size);
            }

            String fullPath = ICON_PATH + filename;
            URL imgUrl = clazz.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 loadAbsolutePathIcon(String absolutePath, int size) {
        try {
            Image image = new ImageIcon(absolutePath).getImage();
            return new ImageIcon(image.getScaledInstance(size, size, Image.SCALE_SMOOTH));
        } catch (Exception e) {
            logger.error("Failed to load absolute path icon '{}'", absolutePath, e);
            return createPlaceholderIcon(size);
        }
    }

    /**
     * 加载图片,另一个版本
     * @param clazz resources包所在的jar
     * @param filename 图片名
     * @param size 图片大小
     * @return ImageIcon对象
     */
    public static ImageIcon loadIcon0(Class<?> clazz ,String filename, int size) {
        try {
            if (filename.isEmpty()){
                return createPlaceholderIcon(size);
            }
            URL imgUrl = clazz.getResource(filename);
            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);
    }
}
