package guard.api.sensitive.ui.sapiguard.hookedapis;

import android.os.Build;

import java.util.ArrayList;
import java.util.List;

import guard.api.sensitive.ui.sapiguard.utils.PrivacyManager;
import guard.api.sensitive.ui.sapiguard.XHook;
import guard.api.sensitive.ui.sapiguard.utils.XParam;

public class XWifiManager extends XHook {
    private Methods mMethod;
    private String mClassName;
    private static final String cClassName = "android.net.wifi.WifiManager";

    private XWifiManager(Methods method, String restrictionName, String className) {
        super(restrictionName, method.name().replace("Srv_", ""), "WiFi." + method.name());
        mMethod = method;
        mClassName = className;
    }

    public String getClassName() {
        return mClassName;
    }

    // @formatter:off

    // public List<WifiConfiguration> getConfiguredNetworks()
    // public WifiInfo getConnectionInfo()
    // public DhcpInfo getDhcpInfo()
    // public List<ScanResult> getScanResults()
    // public WifiConfiguration getWifiApConfiguration()
    // frameworks/base/wifi/java/android/net/wifi/WifiManager.java
    // frameworks/base/wifi/java/android/net/wifi/WifiInfo.java
    // frameworks/base/core/java/android/net/DhcpInfo.java
    // http://developer.android.com/reference/android/net/wifi/WifiManager.html

    // public java.util.List<android.net.wifi.BatchedScanResult> getBatchedScanResults(java.lang.String callingPackage)
    // public java.util.List<android.net.wifi.WifiConfiguration> getConfiguredNetworks()
    // public android.net.wifi.WifiInfo getConnectionInfo()
    // public android.net.DhcpInfo getDhcpInfo()
    // public java.util.List<android.net.wifi.ScanResult> getScanResults(java.lang.String callingPackage)
    // public android.net.wifi.WifiConfiguration getWifiApConfiguration()
    // http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/com/android/server/wifi/WifiService.java
    // http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.0_r1/com/android/server/wifi/WifiServiceImpl.java

    // @formatter:on

    // @formatter:off
    private enum Methods {
        getConfiguredNetworks, getConnectionInfo, getDhcpInfo, getScanResults, getWifiApConfiguration,
        Srv_getBatchedScanResults, Srv_getConfiguredNetworks, Srv_getConnectionInfo, Srv_getDhcpInfo, Srv_getScanResults, Srv_getWifiApConfiguration
    }

    ;
    // @formatter:on

    public static List<XHook> getInstances(String className, boolean server) {
        List<XHook> listHook = new ArrayList<XHook>();
        if (!cClassName.equals(className)) {
            if (className == null)
                className = cClassName;

            String srvClassName;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                srvClassName = "com.android.server.wifi.WifiServiceImpl";
            else
                srvClassName = "com.android.server.wifi.WifiService";

            for (Methods wifi : Methods.values())
                if (wifi.name().startsWith("Srv_")) {
                    if (server)
                        listHook.add(new XWifiManager(wifi, PrivacyManager.cNetwork, srvClassName));
                } else
                    listHook.add(new XWifiManager(wifi, PrivacyManager.cNetwork, className));

            listHook.add(new XWifiManager(Methods.getScanResults, PrivacyManager.cLocation, className));
            if (server)
                listHook.add(new XWifiManager(Methods.Srv_getScanResults, PrivacyManager.cLocation, srvClassName));

            // This is to fake "offline", no permission required
            listHook.add(new XWifiManager(Methods.getConnectionInfo, PrivacyManager.cInternet, className));
            if (server)
                listHook.add(new XWifiManager(Methods.Srv_getConnectionInfo, PrivacyManager.cInternet, srvClassName));
        }

        return listHook;
    }

    @Override
    protected void before(XParam param) throws Throwable {
        // Do nothing
    }

    @Override
    @SuppressWarnings("rawtypes")
    protected void after(XParam param) throws Throwable {
        switch (mMethod) {
            case Srv_getBatchedScanResults:
            case getConfiguredNetworks:
            case Srv_getConfiguredNetworks:


            case getConnectionInfo:
            case Srv_getConnectionInfo:


            case getDhcpInfo:
            case Srv_getDhcpInfo:


            case getScanResults:
            case Srv_getScanResults:


            case getWifiApConfiguration:
            case Srv_getWifiApConfiguration:

                break;

        }
    }
}
