package io.xpipe.app.util;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.ServerSocket;

public class HostHelper {

    private static int portCounter = 0;

    public static int randomPort() {
        var p = 40000 + portCounter;
        portCounter = portCounter + 1 % 1000;
        return p;
    }

    public static boolean isPortAvailable(int port) {
        try (ServerSocket ignored = new ServerSocket(port)) {
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    public static int findRandomOpenPortOnAllLocalInterfaces() {
        try (ServerSocket socket = new ServerSocket(0)) {
            return socket.getLocalPort();
        } catch (IOException e) {
            return randomPort();
        }
    }

    public static boolean isLocalhost(String host) {
        host = host.strip();
        if (host.equalsIgnoreCase("localhost")) {
            return true;
        }

        if (host.equals("127.0.0.1")) {
            return true;
        }

        if (host.equals("::1") || host.equals("[::1]")) {
            return true;
        }

        return false;
    }

    public static boolean isLocalNetworkAddress(String host) {
        Inet4Address inet4Address;
        try {
            inet4Address = Inet4Address.ofLiteral(host);
        } catch (IllegalArgumentException ignored) {
            return false;
        }

        return inet4Address.isSiteLocalAddress();
    }
}
