package com.epson.mobilephone.common.wifidirect;

import com.google.common.primitives.UnsignedBytes;

import epson.print.CommonDefine;

import org.apache.commons.lang.ArrayUtils;
import org.opencv.imgproc.Imgproc;

public class NfcTagParser {
    public static final int FLAG_DISABLE_WIFI = 16777216;
    public static final int FLAG_INFRASTRUCTURE = 2;
    public static final int FLAG_LOCKED_BY_ADMIN = 65536;
    public static final int FLAG_MAX_CONNECTED = 256;
    public static final int FLAG_POWER_STATUS = 1;
    public static final int FLAG_SIMPLEAP_STATUS = 4;
    public static final int FLAG_SIMPLEAP_STATUS_5G = 16;
    private static final String INVALID_IPADDRESSV4 = "0.0.0.0";
    private static final int LENGTH_IPADDRESSV4 = 4;
    private static final int LENGTH_MACADDRESS = 6;
    private static final int LENGTH_TAGDATA = 290;
    private static final int OFFSET_DISABLE_WIFI = 133;
    private static final int OFFSET_INFRASTRUCTURE = 130;
    private static final int OFFSET_IPADDRESSV4 = 146;
    private static final int OFFSET_IPADDRESSV4_SIMPLEAP = 258;
    private static final int OFFSET_LOCKED_BY_ADMIN = 132;
    private static final int OFFSET_MACADDRESS = 115;
    private static final int OFFSET_MAX_CONNECTED = 68;
    private static final int OFFSET_MAX_CONNECT_SSID_LENGTH = 66;
    private static final int OFFSET_PASSWORD = 2;
    private static final int OFFSET_PASSWORD_LENGTH = 0;
    private static final int OFFSET_POWER_STATUS = 114;
    private static final int OFFSET_SIMPLEAP_STATUS = 131;
    private static final int OFFSET_SSID = 69;
    protected byte[] tagData = null;

    public boolean parseTag(byte[] bArr) {
        if (bArr.length != LENGTH_TAGDATA) {
            return false;
        }
        tagData = bArr;
        return true;
    }

    public String getMacAddress() {
        return NfcMacAddrUtils.getMacAdressStr(ArrayUtils.subarray(this.tagData, 115, Imgproc.COLOR_YUV2RGBA_YVYU));
    }

    public String getSSID() {
        byte[] bArr = tagData;
        return new String(ArrayUtils.subarray(bArr, 69, (bArr[66] - 1) + 69));
    }

    public String getPassWord() {
        byte[] bArr = tagData;
        byte[] subarray = ArrayUtils.subarray(bArr, 2, bArr[0] + 2);
        for (int i = 0; i < subarray.length; i++) {
            subarray[i] = (byte) ((~subarray[i]) & 255);
        }
        return new String(subarray);
    }

    public String getIPAddressV4() {
        return formatIPAddressV4(ArrayUtils.subarray(this.tagData, OFFSET_IPADDRESSV4, 150));
    }

    public String getIPAddressV4SimpleAP() {
        return formatIPAddressV4(ArrayUtils.subarray(this.tagData, 258, 262));
    }

    private String formatIPAddressV4(byte[] bArr) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < bArr.length; i++) {
            stringBuffer.append(String.valueOf(bArr[i] & UnsignedBytes.MAX_VALUE));
            if (i == bArr.length - 1) {
                break;
            }
            stringBuffer.append(CommonDefine.DOT);
        }
        if (INVALID_IPADDRESSV4.equals(stringBuffer.toString())) {
            return null;
        }
        return stringBuffer.toString();
    }

    private int getPrinterStatus() {
        int i = tagData[114] != 0 ? 1 : 0;
        if (this.tagData[130] != 0) {
            i |= 2;
        }
        if ((1 & tagData[131]) != 0) {
            i |= 4;
        }
        if ((this.tagData[131] & 2) != 0) {
            i |= 16;
        }
        if (this.tagData[68] != 0) {
            i |= 256;
        }
        if (this.tagData[132] != 0) {
            i |= 65536;
        }
        return tagData[133] != 0 ? i | 16777216 : i;
    }
}
