package com.pusherman.networkinfo;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class RNNetworkInfo extends ReactContextBaseJavaModule {
    WifiManager wifi;

    public static final String TAG = "RNNetworkInfo";

    public RNNetworkInfo(ReactApplicationContext reactContext) {
        super(reactContext);

        wifi = (WifiManager) reactContext.getApplicationContext()
                .getSystemService(Context.WIFI_SERVICE);
    }

    @Override
    public String getName() {
        return TAG;
    }

    @ReactMethod
    public void getSSID(final Callback callback) {
        WifiInfo info = wifi.getConnectionInfo();

        // This value should be wrapped in double quotes, so we need to unwrap it.
        String ssid = info.getSSID();
        if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
            ssid = ssid.substring(1, ssid.length() - 1);
        }

        callback.invoke(ssid);
    }

    @ReactMethod
    public void getBSSID(final Callback callback) {
        callback.invoke(wifi.getConnectionInfo().getBSSID());
    }

    @ReactMethod
    public void getIPAddress(final Callback callback) {
        String ipAddress = null;

        for (InetAddress address : getInetAddresses()) {
            if (!address.isLoopbackAddress()) {
                ipAddress = address.getHostAddress().toString();
            }
        }

        callback.invoke(ipAddress);
    }

    @ReactMethod
    public void getIPV4Address(final Callback callback) {
        String ipAddress = null;

        for (InetAddress address : getInetAddresses()) {
            if (!address.isLoopbackAddress() && address instanceof Inet4Address) {
                ipAddress = address.getHostAddress().toString();
            }
        }

        callback.invoke(ipAddress);
    }


    private List<InetAddress> getInetAddresses() {
        List<InetAddress> addresses = new ArrayList<>();
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    addresses.add(inetAddress);
                }
            }
        } catch (Exception ex) {
            Log.e(TAG, ex.toString());
        }
        return addresses;
    }
}

@@ -132,16 +132,59 @@
 anager;%0A
+import android.support.annotation.NonNull;%0A
 import a
@@ -461,32 +461,66 @@
 et.InetAddress;%0A
+import java.net.InterfaceAddress;%0A
 import java.net.
@@ -1593,32 +1593,424 @@
 SSID());%0A    %7D%0A%0A
+    @ReactMethod%0A    public void getBroadcast(@NonNull String ip, final Callback callback) %7B%0A        String ipAddress = null;%0A%0A        for (InterfaceAddress address : getInetAddresses()) %7B%0A            if (address.getAddress().toString().equalsIgnoreCase(ip)) %7B%0A                ipAddress = address.getBroadcast().toString();%0A            %7D%0A        %7D%0A%0A        callback.invoke(ipAddress);%0A    %7D%0A%0A
     @ReactMethod
@@ -2107,34 +2107,39 @@
 %0A        for (In
-e
 t
+erface
 Address address 
@@ -2179,32 +2179,45 @@
    if (!address.
+getAddress().
 isLoopbackAddres
@@ -2251,32 +2251,45 @@
 dress = address.
+getAddress().
 getHostAddress()
@@ -2493,18 +2493,23 @@
  for (In
-e
 t
+erface
 Address 
@@ -2565,16 +2565,29 @@
 address.
+getAddress().
 isLoopba
@@ -2608,16 +2608,29 @@
  address
+.getAddress()
  instanc
@@ -2685,16 +2685,29 @@
 address.
+getAddress().
 getHostA
@@ -2807,34 +2807,39 @@
  private List%3CIn
-e
 t
+erface
 Address%3E getInet
@@ -2867,18 +2867,23 @@
  List%3CIn
-e
 t
+erface
 Address%3E
@@ -3097,32 +3097,33 @@
 .nextElement();%0A
+%0A
                 
@@ -3131,176 +3131,76 @@
 or (
-Enumeration%3CInetAddress%3E enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) %7B%0A                    InetAddress inetAddress = enumIpAddr.nextElement();
+InterfaceAddress interface_address : intf.getInterfaceAddresses()) %7B
 %0A   
@@ -3232,19 +3232,25 @@
 s.add(in
-etA
+terface_a
 ddress);

add broadcast