package top.quantic.sentry.service.util;

import de.androidpit.colorthief.ColorThief;
import de.androidpit.colorthief.MMCQ;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.InetSocketAddress;
import java.net.URL;

import static top.quantic.sentry.service.util.Inflection.pluralize;
import static top.quantic.sentry.service.util.Inflection.singularize;

public class MiscUtil {

    public static String humanizeBytes(long bytes) {
        int unit = 1000; // 1024 for non-SI units
        if (bytes < unit)
            return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), "kMGTPE".charAt(exp - 1));
    }

    public static String inflect(long value, String label) {
        return value + " " + (value == 1 ? singularize(label) : pluralize(label));
    }

    public static Color getDominantColor(String urlStr, Color fallback) {
        try {
            URL url = new URL(urlStr);
            BufferedImage image = ImageIO.read(url);
            MMCQ.CMap result = ColorThief.getColorMap(image, 5);
            MMCQ.VBox vBox = result.vboxes.get(0);
            int[] rgb = vBox.avg(false);
            return new Color(rgb[0], rgb[1], rgb[2]);
        } catch (Exception ignore) {
        }
        return fallback;
    }

    public static InetSocketAddress getSourceServerAddress(String address) {
        int port = 0;
        if (address.indexOf(':') >= 0) {
            String[] tmpAddress = address.split(":", 2);
            port = Integer.parseInt(tmpAddress[1]);
            address = tmpAddress[0];
        }
        if (port == 0) {
            port = 27015;
        }
        return new InetSocketAddress(address, port);
    }

    public static String getIPAddress(String address) {
        if (address.indexOf(':') >= 0) {
            String[] tmpAddress = address.split(":", 2);
            address = tmpAddress[0];
        }
        return address;
    }

    private MiscUtil() {}
}

@@ -116,16 +116,73 @@
 ef.MMCQ;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -261,16 +261,44 @@
 dImage;%0A
+import java.io.InputStream;%0A
 import j
@@ -510,16 +510,96 @@
 Util %7B%0A%0A
+    private static final Logger log = LoggerFactory.getLogger(MiscUtil.class);%0A%0A
     publ
@@ -1171,92 +1171,614 @@
     
-URL url = new URL(urlStr);%0A            BufferedImage image = ImageIO.read(url);%0A    
+return getDominantColor(ImageIO.read(new URL(urlStr.replace(%22.webp%22, %22.jpg%22)))); // hack for now%0A        %7D catch (Exception e) %7B%0A            log.debug(%22Could not process %7B%7D: %7B%7D%22, urlStr, e.toString());%0A        %7D%0A        return fallback;%0A    %7D%0A%0A    public static Color getDominantColor(InputStream input, Color fallback) %7B%0A        try %7B%0A            return getDominantColor(ImageIO.read(input));%0A        %7D catch (Exception e) %7B%0A            log.debug(%22Could not process from stream: %7B%7D%22, e.toString());%0A        %7D%0A        return fallback;%0A    %7D%0A%0A    public static Color getDominantColor(BufferedImage image) %7B%0A
     
@@ -1834,20 +1834,16 @@
 ge, 5);%0A
-    
         
@@ -1889,20 +1889,16 @@
         
-    
 int%5B%5D rg
@@ -1918,20 +1918,16 @@
 false);%0A
-    
         
@@ -1972,80 +1972,8 @@
 %5D);%0A
-        %7D catch (Exception ignore) %7B%0A        %7D%0A        return fallback;%0A
     
@@ -2648,12 +2648,17 @@
 Util() %7B
+%0A    
 %7D%0A%7D%0A

Update dominant color methods