package com.vpn.fastestvpnservice.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;

public class CheckInternetConnection {

    Context a;
    boolean statusToReturn;

    public static CheckInternetConnection checkInternetConnection = null;

    public static CheckInternetConnection getInternetConnection(Context a) {
        if (checkInternetConnection == null) {
            checkInternetConnection = new CheckInternetConnection(a);
        }

        return checkInternetConnection;
    }

    public CheckInternetConnection(Context a) {
        this.a = a;
    }

    public boolean isConnectedToInternet() {
        ConnectivityManager connectivity = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
        }
        return false;
    }

    public boolean isOpenWifiConnected() {

        ConnectivityManager connectivityManager = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null)
        {
            Network[] allNetworks = connectivityManager.getAllNetworks();
            for (Network network : allNetworks){
                NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network);
                if (networkCapabilities != null && networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)){
                    if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL)){
                        if (!networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL)){ // checking Authentication
                            return true;    // Open Wifi Connected
                        }
                    }
                }
            }
        }
        return false;
    }

//    public boolean isConnectedOpenWifi1() {
//        WifiManager wifiManager1 = (WifiManager) a.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//
//        WifiManager wifiManager2 = (WifiManager) App.getApplication().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//        Log.d("OpenWiFi test t...", "Testing...");
//
//        if (wifiManager1 == null) {
//            Log.e("OpenWiFi", "Wi-Fi is not supported on this device.");
//            return false;
//        }
//
////        if (!wifiManager.isWifiEnabled()) {
////            Log.d("OpenWiFi test wifi en", "Not Enable");
////            wifiManager.setWifiEnabled(true);
////        }
//
//        if (wifiManager1.isWifiEnabled()) {
//            boolean status = wifiManager1.startScan();
//
//            Log.d("OpenWiFi test scan stat", "Status = " + status);
//
//            List<ScanResult> scanResults = wifiManager1.getScanResults();
//
//            Log.d("OpenWiFi test scan", String.valueOf(scanResults.size()) + status);
//
//            for (ScanResult scanResult : scanResults) {
//                String ssid = scanResult.SSID;
//                String securityType = getSecurityType(scanResult);
//
//                Log.d("OpenWiFi test type", securityType);
//
//                if (securityType.equals("Open")) {
//                    Log.d("OpenWiFi test", "Open Wi-Fi network: " + ssid);
//
//                    ConnectivityManager connectivityManager = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
//                    NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
//
//                    if (activeNetwork != null && activeNetwork.isConnected() && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
//                        WifiManager wifiManager3 = (WifiManager) a.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//                        WifiInfo wifiInfo = wifiManager3.getConnectionInfo();
//
//                        if (wifiInfo != null && wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
//                            String connectedSSID = wifiInfo.getSSID().replace("\"", "");
//                            String targetSSID = ssid;
//
//                            Log.d("open wifi rec WiFi Conn", "Normal wifi connected " + connectedSSID);
//
//                            if (connectedSSID.equals(targetSSID)) {
//                                Log.d("open wifi rec Open con", "Device is connected to the open Wi-Fi network: " + connectedSSID);
//                                return true;
//                            } else {
//                                Log.d("open wifi rec nrml conn", "Device is connected to the Normal Wi-Fi network: " + connectedSSID);
//                            }
//                        }
//
//                        return true;
//                    }
//                }
//            }
//        }
//        return false;
//    }

//    public boolean isConnectedOpenWifi3() {
//
//        WifiManager wifiManager = (WifiManager) a.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//
//        if (wifiManager == null) {
//            Log.e("Open wifi test ns", "Wi-Fi is not supported on this device.");
//            return false;
//        }
//
//        if (wifiManager.isWifiEnabled())
//        {
//            wifiManager.startScan();
//
//            List<ScanResult> results = wifiManager.getScanResults();
//
//            Log.d("open wifi rec results", results.size() + "");
//
//
//            for (ScanResult scanResult : results) {
//
//                Log.d("open wifi rec list", scanResult.SSID);
//
//                String ssid = scanResult.SSID;
//
//                if (scanResult.capabilities != null) {
//
//                    if (scanResult.capabilities.equals("[ESS]")) {
//                        Log.d("open wifi rec ESS", "Open wifi detected " + ssid);
//
//                        ConnectivityManager connectivityManager = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
//                        NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
//
//                        if (activeNetwork != null && activeNetwork.isConnected() && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI){
//                            WifiManager wifiManager1 = (WifiManager) a.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//                            WifiInfo wifiInfo = wifiManager1.getConnectionInfo();
//
//                            if (wifiInfo != null && wifiInfo.getSupplicantState() == SupplicantState.COMPLETED){
//                                String connectedSSID = wifiInfo.getSSID().replace("\"", "");
//                                String targetSSID = ssid;
//
//                                Log.d("open wifi rec WiFi Conn", "Normal wifi connected " + connectedSSID);
//
//                                if (connectedSSID.equals(targetSSID)){
//                                    Log.d("open wifi rec Open con", "Device is connected to the open Wi-Fi network: " + connectedSSID);
//                                    return true;
//                                }
//                                else{
//                                    Log.d("open wifi rec nrml conn", "Device is connected to the Normal Wi-Fi network: " + connectedSSID);
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        }
//
//
//        return false;
//    }
    private String getSecurityType(ScanResult result) {
        String securityType = "Unknown";

        if (result.capabilities.contains("WPA")) {
            securityType = "WPA";
        } else if (result.capabilities.contains("WEP")) {
            securityType = "WEP";
        } else if (result.capabilities.contains("EAP")) {
            securityType = "EAP";
        } else if (result.capabilities.contains("PSK")) {
            securityType = "WPA-PSK";
        } else if (result.capabilities.contains("SAE")) {
            securityType = "WPA3-SAE";
        } else if (result.capabilities.contains("OWE")) {
            securityType = "WPA3-OWE";
        } else if (result.capabilities.contains("MFP")) {
            securityType = "WPA2-MFP";
        } else if (result.capabilities.contains("WAPI-KEY")) {
            securityType = "WAPI";
        } else if (result.capabilities.contains("ESS")){
            securityType = "Open";
        }
        else {
            securityType = "Open";
        }

        return securityType;
    }

//    public boolean isConnectedOpenWifi2() {
//
//        WifiManager wifiManager = (WifiManager) a.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//
//        BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
//            @Override
//            public void onReceive(Context context, Intent intent) {
//
////                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
////                {
////                    boolean success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false);
////
////                    if (success){
////                        statusToReturn = scanSuccess(wifiManager);
////                    }
////                    else{
////                        statusToReturn = scanFailure(wifiManager);
////                    }
////                }
//
//
//            }
//        };
//
//
//        IntentFilter intentFilter = new IntentFilter();
//        intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
//        a.registerReceiver(broadcastReceiver, intentFilter);
//
//        boolean success = wifiManager.startScan();
//        if (!success)
//        {
//            statusToReturn = scanFailure(wifiManager);
//        }
//
//        Log.d("OpenWiFi test status", String.valueOf(statusToReturn));
//        return statusToReturn;
//    }


//    private boolean scanSuccess(WifiManager wifiManager) {
//        List<ScanResult> scanResults = wifiManager.getScanResults();
//
//        Log.d("OpenWiFi test scan", String.valueOf(scanResults.size()));
//
//        for (ScanResult scanResult : scanResults){
//            String ssid = scanResult.SSID;
//            String securityType = getSecurityType(scanResult);
//
//            Log.d("OpenWiFi test type", securityType);
//
//            if (securityType.equals("Open")){
//                Log.d("OpenWiFi test", "Open Wi-Fi network: " + ssid);
//                return true;
//            }
//        }
//        return false;
//    }
//    private boolean scanFailure(WifiManager wifiManager) {
//        List<ScanResult> scanResults = wifiManager.getScanResults();
//
//        return false;
//    }

}
