package com.epson.mobilephone.common.wifidirect;

import android.content.Context;

import com.google.common.primitives.UnsignedBytes;

import java.util.regex.Pattern;

public class WiFiControl {
    public static final String DEFAULT_MAC_ADDRESS = "02:00:00:00:00:00";
    private static final int MACADDR2SSID_OFFSET = 4;
    public static final String P2P_FY14_17_DEFAULT = "^EPSON...... ";
    private static final String PREFIX_SIMPLEAP = "#S ";
    private static final String PREFIX_WIFIP2P = "#P ";
    public static final String SSID_P2P_FY18 = "^DIRECT-..-";
    public static final String SSID_P2P_FY18_DEFAULT = "^DIRECT-..-EPSON-";
    public static final String SSID_P2P_FY18_LCD = "^DIRECT-..-EPSON-[0-9A-F]{6}$";
    public static final String SSID_SIMPLEAP_PREFIX = "DIRECT-";
    public static final String SSID_WIFIDIRECTMODE_FY13 = "^DIRECT-..EPSON..$";
    private static final String TAG = "WiFiControl";
    public static WiFiControl instance;
    private Context mAppContext = null;

    public enum ConnectType {
        SimpleAP,
        WiFiP2P,
        NONE
    }

    public static WiFiControl getInstance(Context context) {
        WiFiControl wiFiControl = instance;
        if (wiFiControl != null) {
            return wiFiControl;
        }
        instance = new WiFiControl(context);
        return instance;
    }

    private WiFiControl(Context context) {
        mAppContext = context.getApplicationContext();
    }

    public String getCurConnectInfo(String str) {
        WiFiUtils instance2 = WiFiUtils.getInstance(mAppContext);
        if (str == null || str.isEmpty()) {
            return null;
        }
        WifiP2pUtils.ConnectionInfo connectionInfo = WifiP2pUtils.getInstance(mAppContext).getConnectionInfo();
        if (connectionInfo != null) {
            String p2pAddr2PtrAddr = p2pAddr2PtrAddr(connectionInfo.p2PMacAdder, connectionInfo.printerName);
            if (MacAddrFilter.getInstance(mAppContext).isExcludedMacAddress(p2pAddr2PtrAddr) && p2pAddr2PtrAddr.equals(str)) {
                return null;
            }
            if (connectionInfo.isGroupOwnerThisDevice) {
                if (str.equals(p2pAddr2PtrAddr(connectionInfo.p2PMacAdder, connectionInfo.printerName))) {
                    return addSSIDPrefix(connectionInfo.printerName, ConnectType.WiFiP2P);
                }
            } else if (str.equals(p2pAddr2PtrAddrP2P(connectionInfo.p2PMacAdder, connectionInfo.printerName))) {
                return addSSIDPrefix(connectionInfo.printerName, ConnectType.WiFiP2P);
            }
        }
        String curSSID = instance2.getCurSSID();
        String curP2PMacAdder = instance2.getCurP2PMacAdder();
        if (curP2PMacAdder != null) {
            if (!DEFAULT_MAC_ADDRESS.equals(curP2PMacAdder)) {
                if (str.equals(MacAddrUtils.p2pAddr2MacAddrStr(curP2PMacAdder))) {
                    return addSSIDPrefix(curSSID, ConnectType.SimpleAP);
                }
            } else if (isSimpleAP(curSSID)) {
                return addSSIDPrefix(curSSID, ConnectType.SimpleAP);
            }
        }
        return null;
    }

    public boolean isNeedConnect(String str) {
        if (str == null || str.isEmpty()) {
            return false;
        }
        String removeSSIDPrefix = removeSSIDPrefix(str);
        switch (getSSIDType(str)) {
            case SimpleAP:
                if (!removeSSIDPrefix.equals(WiFiUtils.getInstance(mAppContext).getCurSSID())) {
                    return true;
                }
                EPLog.w(TAG, "Already connected : SimpleAP");
                ManageDefaultNetwork.getInstance(mAppContext).setDefaultNetworkSimpleAp();
                return false;
            case WiFiP2P:
                WifiP2pUtils.ConnectionInfo connectionInfo = WifiP2pUtils.getInstance(mAppContext).getConnectionInfo();
                if (connectionInfo == null || !removeSSIDPrefix.equals(connectionInfo.printerName)) {
                    return true;
                }
                EPLog.w(TAG, "Already connected : P2P");
                ManageDefaultNetwork.getInstance(mAppContext).resetDefaultNetwork();
                return false;
            default:
                EPLog.w(TAG, "Unknown Type");
                return true;
        }
    }

    public static String getPrinterNetworkName(String str) {
        String removeSSIDPrefix = removeSSIDPrefix(str);
        if (isSimpleAPFY13WiFiDirect(removeSSIDPrefix)) {
            return removeSSIDPrefix;
        }
        if (Pattern.matches("^EPSON...... .*", removeSSIDPrefix)) {
            return removeSSIDPrefix.replaceFirst(P2P_FY14_17_DEFAULT, "");
        }
        return (!Pattern.matches("^DIRECT-..-EPSON-.*", removeSSIDPrefix) || Pattern.matches(SSID_P2P_FY18_LCD, removeSSIDPrefix)) ? removeSSIDPrefix : removeSSIDPrefix.replaceFirst(SSID_P2P_FY18_DEFAULT, "");
    }

    public String getEstimateSSID(String str, String str2) {
        switch (getSSIDType(str)) {
            case SimpleAP:
                return removeSSIDPrefix(str);
            case WiFiP2P:
                String removeSSIDPrefix = removeSSIDPrefix(str);
                if (!Pattern.matches("^EPSON...... .*", removeSSIDPrefix)) {
                    return removeSSIDPrefix;
                }
                return SSID_SIMPLEAP_PREFIX + str2.substring(4);
            default:
                return str;
        }
    }

    public static boolean isSimpleAP(String str) {
        return str != null && removeSSIDPrefix(str).startsWith(SSID_SIMPLEAP_PREFIX);
    }

    public static boolean isSimpleAPFY13WiFiDirect(String str) {
        return str != null && removeSSIDPrefix(str).matches(SSID_WIFIDIRECTMODE_FY13);
    }

    public static ConnectType getSSIDType(String str) {
        if (str != null && !str.isEmpty()) {
            if (str.startsWith(PREFIX_SIMPLEAP)) {
                return ConnectType.SimpleAP;
            }
            if (str.startsWith(PREFIX_WIFIP2P)) {
                return ConnectType.WiFiP2P;
            }
        }
        return ConnectType.NONE;
    }

    public static String rebuildSSID(String str) {
        if (hasSSIDPrefix(str)) {
            return str;
        }
        return addSSIDPrefix(str, ConnectType.SimpleAP);
    }

    public static String addSSIDPrefix(String str, ConnectType connectType) {
        if (hasSSIDPrefix(str)) {
            return str;
        }
        switch (connectType) {
            case SimpleAP:
                return PREFIX_SIMPLEAP + str;
            case WiFiP2P:
                return PREFIX_WIFIP2P + str;
            default:
                EPLog.w(TAG, "Unknown Type");
                return str;
        }
    }

    public static String removeSSIDPrefix(String str) {
        if (str == null) {
            return null;
        }
        if (str.startsWith(PREFIX_SIMPLEAP)) {
            return str.replaceFirst(PREFIX_SIMPLEAP, "");
        }
        return str.startsWith(PREFIX_WIFIP2P) ? str.replaceFirst(PREFIX_WIFIP2P, "") : str;
    }

    public static boolean hasSSIDPrefix(String str) {
        if (str == null) {
            return false;
        }
        return !str.equals(removeSSIDPrefix(str));
    }

    public static String p2pAddr2PtrAddrP2P(String str, String str2) {
        byte[] macAdressBytes = MacAddrUtils.getMacAdressBytes(str);
        if (str2 == null) {
            return MacAddrUtils.getMacAdressStr(macAdressBytes);
        }
        if (hasSSIDPrefix(str2)) {
            str2 = removeSSIDPrefix(str2);
        }
        if (str2.matches("^DIRECT-..-.*")) {
            return MacAddrUtils.getMacAdressStr(macAdressBytes);
        }
        macAdressBytes[4] = (byte) (macAdressBytes[4] ^ UnsignedBytes.MAX_POWER_OF_TWO);
        return MacAddrUtils.getMacAdressStr(macAdressBytes);
    }

    public static String p2pAddr2PtrAddr(String str, String str2) {
        byte[] macAdressBytes = MacAddrUtils.getMacAdressBytes(str);
        macAdressBytes[0] = (byte) (macAdressBytes[0] & ((byte) (macAdressBytes[0] & -3)));
        if (str2 != null) {
            if (hasSSIDPrefix(str2)) {
                str2 = removeSSIDPrefix(str2);
            }
            if (!str2.matches("^DIRECT-..-.*")) {
                return MacAddrUtils.getMacAdressStr(macAdressBytes);
            }
        }
        macAdressBytes[4] = (byte) (macAdressBytes[4] ^ UnsignedBytes.MAX_POWER_OF_TWO);
        return MacAddrUtils.getMacAdressStr(macAdressBytes);
    }

    public static String p2pPtrAddr2PtrAddr(String str) {
        byte[] macAdressBytes = MacAddrUtils.getMacAdressBytes(str);
        if ((macAdressBytes[0] & 2) == 0) {
            return MacAddrUtils.getMacAdressStr(macAdressBytes);
        }
        macAdressBytes[0] = (byte) (macAdressBytes[0] & ((byte) (macAdressBytes[0] & -3)));
        macAdressBytes[4] = (byte) (macAdressBytes[4] ^ UnsignedBytes.MAX_POWER_OF_TWO);
        return MacAddrUtils.getMacAdressStr(macAdressBytes);
    }
}
