// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.builtin.buildertools.objimport;

import java.util.HashMap;
import javax.annotation.Nullable;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import javax.annotation.Nonnull;
import java.awt.image.BufferedImage;
import java.nio.file.Path;
import java.util.Map;

public final class TextureSampler
{
    private static final Map<Path, BufferedImage> textureCache;
    
    private TextureSampler() {
    }
    
    @Nullable
    public static BufferedImage loadTexture(@Nonnull final Path path) {
        if (!Files.exists(path, new LinkOption[0])) {
            return null;
        }
        final BufferedImage cached = TextureSampler.textureCache.get(path);
        if (cached != null) {
            return cached;
        }
        try {
            final BufferedImage image = ImageIO.read(path.toFile());
            if (image != null) {
                TextureSampler.textureCache.put(path, image);
            }
            return image;
        }
        catch (final IOException e) {
            return null;
        }
    }
    
    @Nonnull
    public static int[] sampleAt(@Nonnull final BufferedImage texture, float u, float v) {
        u -= (float)Math.floor(u);
        v -= (float)Math.floor(v);
        v = 1.0f - v;
        final int width = texture.getWidth();
        final int height = texture.getHeight();
        final int x = Math.min((int)(u * width), width - 1);
        final int y = Math.min((int)(v * height), height - 1);
        final int rgb = texture.getRGB(x, y);
        return new int[] { rgb >> 16 & 0xFF, rgb >> 8 & 0xFF, rgb & 0xFF };
    }
    
    public static int sampleAlphaAt(@Nonnull final BufferedImage texture, float u, float v) {
        if (!texture.getColorModel().hasAlpha()) {
            return 255;
        }
        u -= (float)Math.floor(u);
        v -= (float)Math.floor(v);
        v = 1.0f - v;
        final int width = texture.getWidth();
        final int height = texture.getHeight();
        final int x = Math.min((int)(u * width), width - 1);
        final int y = Math.min((int)(v * height), height - 1);
        final int rgba = texture.getRGB(x, y);
        return rgba >> 24 & 0xFF;
    }
    
    public static void clearCache() {
        TextureSampler.textureCache.clear();
    }
    
    @Nullable
    public static int[] getAverageColor(@Nonnull final Path path) {
        if (!Files.exists(path, new LinkOption[0])) {
            return null;
        }
        try {
            final BufferedImage image = ImageIO.read(path.toFile());
            if (image == null) {
                return null;
            }
            long totalR = 0L;
            long totalG = 0L;
            long totalB = 0L;
            int count = 0;
            final int width = image.getWidth();
            final int height = image.getHeight();
            final boolean hasAlpha = image.getColorModel().hasAlpha();
            for (int y = 0; y < height; ++y) {
                for (int x = 0; x < width; ++x) {
                    final int rgba = image.getRGB(x, y);
                    if (hasAlpha) {
                        final int alpha = rgba >> 24 & 0xFF;
                        if (alpha == 0) {
                            continue;
                        }
                    }
                    final int r = rgba >> 16 & 0xFF;
                    final int g = rgba >> 8 & 0xFF;
                    final int b = rgba & 0xFF;
                    totalR += r;
                    totalG += g;
                    totalB += b;
                    ++count;
                }
            }
            if (count == 0) {
                return null;
            }
            return new int[] { (int)(totalR / count), (int)(totalG / count), (int)(totalB / count) };
        }
        catch (final IOException e) {
            return null;
        }
    }
    
    static {
        textureCache = new HashMap<Path, BufferedImage>();
    }
}
