package com.bokecc.dwlivedemo.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Locale;

/**
 * Copyright (C), 20105-2021, 创盛视联数码科技（北京）有限公司
 * FileName:
 * Author: cc视频
 * Date: 2024/4/28
 * Description:
 */
public class NetUtils {
    public static String getLocalIpAddress(Context context) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // For Android M and above
                ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                Network network = connectivityManager.getActiveNetwork();
                if (network != null) {
                    NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
                    if (capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET))) {
                        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
                        while (networkInterfaces.hasMoreElements()) {
                            NetworkInterface networkInterface = networkInterfaces.nextElement();
                            if (!networkInterface.isUp() || networkInterface.isLoopback() || networkInterface.isVirtual())
                                continue;
                            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                            while (inetAddresses.hasMoreElements()) {
                                InetAddress inetAddress = inetAddresses.nextElement();
                                if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)) {
                                    return inetAddress.getHostAddress();
                                }
                            }
                        }
                    }
                }
            } else {
                WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                if (wifiMgr != null) {
                    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
                    if (wifiInfo != null && wifiInfo.getNetworkId() != -1) {
                        int ipAsInt = wifiInfo.getIpAddress();
                        return String.format(Locale.getDefault(), "%d.%d.%d.%d",
                                (ipAsInt & 0xff), (ipAsInt >> 8 & 0xff),
                                (ipAsInt >> 16 & 0xff), (ipAsInt >> 24 & 0xff));
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }
}
