package com.thebrokenrail.gestus.skin;

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

public final class SkinColor {
    public final int chest;
    public final int legs;
    public final int boots;

    private SkinColor(int chest, int legs, int boots) {
        this.chest = chest;
        this.legs = legs;
        this.boots = boots;
    }

    private static class Color {
        private final int data;

        private Color(int data) {
            this.data = data;
        }

        private Color(int red, int green, int blue) {
            data = 255 << 24 | (red & 255) << 16 | (green & 255) << 8 | (blue & 255);
        }

        private Color(long red, long green, long blue) {
            this((int) red, (int) green, (int) blue);
        }

        public int getRed() {
            return data >> 16 & 255;
        }

        public int getGreen() {
            return data >> 8 & 255;
        }

        public int getBlue() {
            return data & 255;
        }

        public int getData() {
            return (getRed() << 16) | (getGreen() << 8) | getBlue();
        }
    }

    private static Color averageColor(BufferedImage image, int x0, int y0, int w, int h) {
        int x1 = x0 + w;
        int y1 = y0 + h;

        long sumRed = 0;
        long sumGreen = 0;
        long sumBlue = 0;

        int num = 0;

        for (int x = x0; x < x1; x++) {
            for (int y = y0; y < y1; y++) {
                int data = image.getRGB(x, y);
                if ((data >> 24 & 255) == 255) {
                    Color pixel = new Color(data);
                    sumRed += pixel.getRed();
                    sumGreen += pixel.getGreen();
                    sumBlue += pixel.getBlue();
                    num++;
                }
            }
        }

        return new Color(sumRed / num, sumGreen / num, sumBlue / num);
    }

    private static Color averageColor(Color color1, Color color2) {
        long sumRed = color1.getRed() + color2.getRed();
        long sumGreen = color1.getGreen() + color2.getGreen();
        long sumBlue = color1.getBlue() + color2.getBlue();
        return new Color(sumRed / 2, sumGreen / 2, sumBlue / 2);
    }

    public static SkinColor get(UUID uuid) {
        String skin = SkinJSON.get(uuid);
        if (skin != null) {
            URL url;
            try {
                url = new URL(skin);
            } catch (MalformedURLException e) {
                return null;
            }

            BufferedImage image;
            try {
                image = ImageIO.read(url);
            } catch (IOException e) {
                return null;
            }

            int chest = averageColor(image, 16, 20, 23, 11).getData();
            int legs = averageColor(averageColor(image, 16, 52, 11, 11), averageColor(image, 0, 20, 11, 11)).getData();
            int boots = averageColor(averageColor(image, 20, 48, 7, 3), averageColor(image, 44, 16, 7, 3)).getData();

            return new SkinColor(chest, legs, boots);
        } else {
            return null;
        }
    }

    public static SkinColor blank() {
        return new SkinColor(-1, -1, -1);
    }
}
