package com.epson.mobilephone.common.escpr;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import com.box.androidsdk.content.models.BoxUser;
import com.epson.mobilephone.common.EpLog;
import com.epson.mobilephone.common.maintain.EPS_SUPPLY_INFO;
import com.epson.mobilephone.common.maintain2.BatteryInfo;
import com.epson.mobilephone.common.maintain2.BatteryInfoEx;
import com.epson.mobilephone.common.maintain2.MaintainPrinterInfo2;
import com.epson.mobilephone.common.wifidirect.escprLib;
import epson.print.BuildConfig;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.Random;

public class EscprLib {
    public static final int EPS_ERR_NONE = 0;
    public static final int EPS_ERR_PRINTER_NOT_FOUND = -1300;
    private static final int MACADRESS_SIZE = 6;
    public static final int MSG_FIND_PRINTER_CALLBACK = 0;
    public static final int MSG_NOTIFY_CONTINUEABLE = 1;
    public static final int MSG_NOTIFY_ERROR = 2;
    public static final int MSG_NOTIFY_PAGE = 30;
    private static final int RUNDOM_SIZE = 4;
    public static final int SECURITY_TYPE_AUTO = 255;
    public static final int SECURITY_TYPE_NONE = 0;
    private static final EscprLib instance = new EscprLib();
    final int EPS_CANCEL = 1;
    final int EPS_CONTINUE = 4;
    volatile boolean bInvalidId = false;
    private String cardprintPackageName = "com.epson.cardprint";
    private String debugString;
    private String iPrintPackageName = BuildConfig.APPLICATION_ID;
    private Handler mHandler;
    private boolean mIsSearch;
    volatile String printerId;
    volatile String printerIp;
    private String printerList;

    public enum PrinterFirmInfo {
        NicFlg,
        NetVer,
        MainVer,
        FY,
        MarketID,
        UrlVer
    }

    public enum snmpParseType {
        STRING,
        MAC_ADDRESS,
        INTEGER,
        SECURITY
    }

    private native int epsWrapperFindPrinter(int i, int i2);

    private native int epsWrapperGetFirmwareInfo(byte[] bArr, byte[] bArr2, byte[] bArr3, byte[] bArr4, byte[] bArr5, byte[] bArr6);

    private native int epsWrapperGetReadyPrintStatus(int[] iArr);

    private native int epsWrapperProbePrinter2(int i, int i2, char[] cArr);

    private native int start_job(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10, int i11, int i12, int i13, int i14, int i15, int i16, int i17, int i18, int i19);

    private native int start_job2(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10, int i11, int i12);

    public native int cancel_print();

    public native int cancel_search_printer();

    public native int confirm_cancel(boolean z);

    public native int confirm_continue(boolean z);

    public native int end_job();

    public native int end_job(int i);

    public native int end_page(boolean z);

    public native int epsWrapperCancelFindPrinter();

    public native int epsWrapperDoMainteCmd(int i);

    public native int epsWrapperGetAutoPowerOnEnable(String str, int[] iArr);

    public native int[] epsWrapperGetBatteryInfo();

    public native int epsWrapperGetEmaStatus(int[] iArr);

    public native String epsWrapperGetId();

    public native int epsWrapperGetInkInfo(int[] iArr);

    public native int epsWrapperGetInkInfo2(MaintainPrinterInfo2 maintainPrinterInfo2);

    public native String epsWrapperGetIp();

    public native int[] epsWrapperGetMaintenanceBoxInformation();

    public native int[] epsWrapperGetMediaInfo(int i, int i2);

    public native int[] epsWrapperGetPaperInfo();

    public native int epsWrapperGetStatus(int[] iArr);

    public native int epsWrapperGetSupplyInfo(EPS_SUPPLY_INFO eps_supply_info);

    public native int epsWrapperGetSupportedMedia(String str);

    public native int epsWrapperInitDriver(Context context, int i, byte b);

    public native int epsWrapperSetAutoPowerOnEnable(String str, boolean z);

    public native int epsWrapperSetEmaStatus(int i);

    public native int[] get_color(int i, int i2);

    public native int get_duplex(int i, int i2);

    public native int get_lang();

    public native int[] get_layout(int i, int i2);

    public native int[] get_paper_size();

    public native int[] get_paper_source(int i, int i2);

    public native int[] get_paper_type(int i);

    public native int[] get_printable_area();

    public native int get_printable_area_info_all(EPS_PRINT_AREA_INFO eps_print_area_info);

    public native int[] get_printer_status();

    public native int[] get_quality(int i, int i2);

    public native int get_start_job_resolution2();

    public native int get_supported_media(String str);

    public native int get_supported_media2();

    public native int init_driver(Context context, String str, byte b);

    public native int init_image(String str);

    public native boolean is64bit();

    public native boolean is_printer_set();

    public native int mibCheckMaxofSimpleAP(MIBCommand mIBCommand);

    public native int mibEnableSimpleAP(MIBCommand mIBCommand);

    public native int mibEnableWiFi(MIBCommand mIBCommand);

    public native int mibGetESSIDList(MIBCommand mIBCommand, byte b);

    public native int mibGetMacadress(MIBCommand mIBCommand);

    public native int mibGetSerial(MIBCommand mIBCommand);

    public native int mibGetSimpleAPSSID(MIBCommand mIBCommand);

    public native int mibGetStatusInfra(MIBCommand mIBCommand);

    public native int mibGetStatusSimpleAP(MIBCommand mIBCommand);

    public native int mibIsAdminLocked(MIBCommand mIBCommand);

    public native int mibParseResponseAsInt(MIBCommand mIBCommand, int[] iArr);

    public native String mibParseResponseAsMacaddress(MIBCommand mIBCommand);

    public native String mibParseResponseAsStr(MIBCommand mIBCommand);

    public native int mibParseSecResponse(MIBCommand mIBCommand, byte[] bArr);

    public native int mibRebootNWwithBLE(MIBCommand mIBCommand);

    public native int mibRebootNWwithSNMP(MIBCommand mIBCommand);

    public native int mibSecLogin(MIBCommand mIBCommand, byte[] bArr);

    public native int mibSecLogout(MIBCommand mIBCommand, byte[] bArr);

    public native int mibSecSetPassword(MIBCommand mIBCommand, byte[] bArr, String str);

    public native int mibSendRecieveCommand(MIBCommand mIBCommand, MIBCommand mIBCommand2, byte[] bArr);

    public native int mibSetSSID(MIBCommand mIBCommand, String str);

    public native int mibSetSecurityType(MIBCommand mIBCommand, int i);

    public native boolean pageS_needRotate2(int i);

    public native int print_jpeg(String str);

    public native int print_page();

    public native int release_driver();

    public native int release_image();

    public native int search_device(int i);

    public native int search_printer(String str);

    public native int search_printer2(String str, String str2, int i, int i2);

    public native int set_printer(int i);

    public native int start_job_roll(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10, int i11, int i12, int i13, int i14, int i15, int i16);

    public native int start_page();

    static {
        try {
            System.loadLibrary("escprlib");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private EscprLib() {
    }

    public static EscprLib getInstance() {
        return instance;
    }

    public int init_driver(Context context, String str) {
        EpLog.m77i("init_driver :" + str);
        EpLog.m77i(is64bit() ? "Native is operating at 64 bit !!" : "Native is operating at 32 bit");
        return init_driver(context, str, iPrintPackageName.equals(context.getPackageName()) ? (byte) 8 : 10);
    }

    public int start_job(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10, int i11, int i12, int i13, int i14, int i15, int i16, int i17, int i18, boolean z) {
        return start_job(i, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15, i16, i17, i18, z ? 2 : 0);
    }

    public int start_job2(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10, int i11, boolean z) {
        return start_job2(i, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, z ? 2 : 0);
    }

    public void setHanlder(Handler handler) {
        mHandler = handler;
    }

    public void findPrinterCallback(String str) {
        EpLog.m78i("called findPrinterCallback : %s", str);
        if (mHandler != null) {
            Message obtain = Message.obtain();
            obtain.what = 0;
            obtain.obj = str.toString();
            mHandler.sendMessage(obtain);
        }
    }

    public void epsNotifyDataChange(String str) {
        EpLog.m78i("  @@@@@@@@@@   called epsNotifyDataChange : %s", str);
        if (mHandler != null) {
            Message obtain = Message.obtain();
            obtain.what = 0;
            obtain.obj = str.toString();
            mHandler.sendMessage(obtain);
        }
    }

    public void epsNotifyContinueable(int i) {
        EpLog.m77i("called epsNotifyContinueable :" + i);
        if (mHandler != null) {
            Message obtain = Message.obtain();
            obtain.what = 1;
            obtain.arg1 = i;
            mHandler.sendMessage(obtain);
        }
    }

    public void epsNotifyError(int i, int i2, boolean z) {
        EpLog.m77i("called epsNotifyError " + i + ":" + i2 + ":" + z);
        if (mHandler != null) {
            Message obtain = Message.obtain();
            obtain.what = 2;
            obtain.arg1 = i;
            obtain.arg2 = i2;
            obtain.obj = Boolean.valueOf(z);
            mHandler.sendMessage(obtain);
        }
    }

    public void epsNotifyPage(int i) {
        EpLog.m77i("called epsNotifyPage :" + i);
        if (mHandler != null) {
            Message obtain = Message.obtain();
            obtain.what = 30;
            obtain.arg1 = i;
            mHandler.sendMessage(obtain);
        }
    }

    public int epsGetReadyPrintStatus(EPS_REDYINKINFO eps_redyinkinfo) {
        int[] iArr = new int[5];
        Arrays.fill(iArr, 0);
        int epsWrapperGetReadyPrintStatus = epsWrapperGetReadyPrintStatus(iArr);
        eps_redyinkinfo.emaStatus = iArr[0];
        eps_redyinkinfo.subscriptionStatus = iArr[1];
        eps_redyinkinfo.subscriptionErrorCode = iArr[2];
        eps_redyinkinfo.subscriptionMode = iArr[3];
        eps_redyinkinfo.subscriptionError = iArr[4];
        return epsWrapperGetReadyPrintStatus;
    }

    public int doFindPrinter(int i, int i2) {
        EpLog.m78i("escprLib2", "**********Call do find printer************");
        return epsWrapperFindPrinter(i, i2);
    }

    public int epsWrapperInitDriver(Context context, int i) {
        return epsWrapperInitDriver(context, i, iPrintPackageName.equals(context.getPackageName()) ? (byte) 8 : 10);
    }

    public int epsWrapperProbePrinter2(int i, String str, String str2, int i2) {
        EpLog.m76i();
        if (i2 != 1) {
            if (i2 == 3 && str2 != null && str2.length() > 0) {
                printerIp = str2;
                printerId = str;
                int epsWrapperProbePrinter2 = epsWrapperProbePrinter2(i, 2, str2.toCharArray());
                if (epsWrapperProbePrinter2 != 0 || !this.bInvalidId) {
                    return epsWrapperProbePrinter2;
                }
                return -500001;
            }
        } else if (str != null && str.length() > 0) {
            return epsWrapperProbePrinter2(i, 1, str.toCharArray());
        }
        return -1300;
    }

    public void resetIPAdressCheck() {
        printerId = null;
        printerIp = null;
        bInvalidId = false;
    }

    public void setSearchStt(boolean z) {
        mIsSearch = z;
    }

    private void findPrinterCB() {
        EpLog.m77i("JAVA findPrinterCB");
        String str = printerList;
        if (str != null && str.length() > 0) {
            String name = getClass().getName();
            EpLog.m78i(name, "EXTRACT Printer information: " + printerList);
            String[] split = printerList.split("\\|\\|");
            if (this.printerIp == null || printerId == null || !this.printerIp.equals(split[2]) || printerId.equals(split[4])) {
                EpLog.m77i("JAVA mIsSearch == " + mIsSearch);
                if (mIsSearch) {
                    EpLog.m77i("JAVA mHandler == " + mHandler);
                    if (mHandler != null) {
                        Message obtain = Message.obtain();
                        obtain.what = 0;
                        Bundle bundle = new Bundle();
                        bundle.putString("index", String.valueOf(Integer.parseInt(split[0]) - 1));
                        bundle.putString("name", split[1]);
                        bundle.putString(escprLib.PRINTER_IP, split[2]);
                        bundle.putString("id", split[4]);
                        if (split.length >= 6) {
                            bundle.putString(escprLib.PRINTER_SERIAL_NO, split[5]);
                        }
                        if (split.length >= 7) {
                            bundle.putString("common_devicename", split[6]);
                        }
                        if (split.length >= 8) {
                            bundle.putString(BoxUser.FIELD_LANGUAGE, split[7]);
                        }
                        obtain.setData(bundle);
                        EpLog.m77i("JAVA send message");
                        mHandler.sendMessage(obtain);
                    }
                }
            } else {
                bInvalidId = true;
                String name2 = getClass().getName();
                EpLog.m72e(name2, "Invalid IP Printer: Expected = " + printerId);
            }
        }
        printerList = "";
    }

    public int epsWrapperPrintErrorNotifyCB(int[] iArr) {
        System.out.println("epsWrapperPrintErrorNotifyCB() call");
        int i = iArr[0];
        int i2 = iArr[1];
        PrintStream printStream = System.out;
        printStream.println("printer error" + i + ";;;choice type: " + i2);
        return 1;
    }

    public int getFirmwareInfo(EnumMap<PrinterFirmInfo, String> enumMap) {
        byte[] bArr = new byte[1];
        byte[] bArr2 = new byte[6];
        byte[] bArr3 = new byte[7];
        byte[] bArr4 = new byte[5];
        byte[] bArr5 = new byte[3];
        byte[] bArr6 = new byte[3];
        int epsWrapperGetFirmwareInfo = epsWrapperGetFirmwareInfo(bArr, bArr2, bArr3, bArr4, bArr5, bArr6);
        enumMap.clear();
        if (epsWrapperGetFirmwareInfo == 0) {
            try {
                PrinterFirmInfo printerFirmInfo = PrinterFirmInfo.NicFlg;
                enumMap.put(printerFirmInfo, "" + ((char) (bArr[0] + 48)));
                enumMap.put(PrinterFirmInfo.NetVer, cStrToString(bArr2));
                enumMap.put(PrinterFirmInfo.MainVer, cStrToString(bArr3));
                enumMap.put(PrinterFirmInfo.FY, cStrToString(bArr4));
                enumMap.put(PrinterFirmInfo.MarketID, cStrToString(bArr5));
                enumMap.put(PrinterFirmInfo.UrlVer, cStrToString(bArr6));
            } catch (RuntimeException unused) {
                enumMap.clear();
            }
        }
        return epsWrapperGetFirmwareInfo;
    }

    public int getBatteryInfo(BatteryInfo batteryInfo) {
        int[] epsWrapperGetBatteryInfo = epsWrapperGetBatteryInfo();
        if (epsWrapperGetBatteryInfo[0] == 0) {
            batteryInfo.powerSourceType = epsWrapperGetBatteryInfo[1];
            batteryInfo.batteryChargeState = epsWrapperGetBatteryInfo[2];
            batteryInfo.batteryRemain = epsWrapperGetBatteryInfo[3];
        }
        return epsWrapperGetBatteryInfo[0];
    }

    public int getBatteryInfoEx(BatteryInfoEx batteryInfoEx) {
        EPS_SUPPLY_INFO eps_supply_info = new EPS_SUPPLY_INFO();
        int epsWrapperGetSupplyInfo = epsWrapperGetSupplyInfo(eps_supply_info);
        batteryInfoEx.powerSourceType = eps_supply_info.powerSourceEx.powerSource;
        batteryInfoEx.number = eps_supply_info.powerSourceEx.number;
        batteryInfoEx.batteryType = (int[]) eps_supply_info.powerSourceEx.batteryType.clone();
        batteryInfoEx.batteryState = (int[]) eps_supply_info.powerSourceEx.batteryState.clone();
        batteryInfoEx.batteryRemain = (int[]) eps_supply_info.powerSourceEx.remaining.clone();
        return epsWrapperGetSupplyInfo;
    }

    private String cStrToString(byte[] bArr) {
        String str = new String(bArr);
        return str.substring(0, str.indexOf(0));
    }

    public static byte[] getMd5(byte[] bArr) {
        byte[] bArr2 = {0};
        try {
            MessageDigest instance2 = MessageDigest.getInstance("MD5");
            instance2.update(bArr);
            return instance2.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return bArr2;
        }
    }

    public byte[] getEngineId(Context context) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(10);
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            byteArrayOutputStream.write(random.nextInt());
        }
        String macAddress = ((WifiManager) context.getApplicationContext().getSystemService(Service.WIFI_SERVICE)).getConnectionInfo().getMacAddress();
        if (macAddress != null) {
            EpLog.m69d("getEngineId macAdress = " + macAddress);
            String[] split = macAddress.split(":", 6);
            if (split.length == 6) {
                for (int i2 = 0; i2 < 6; i2++) {
                    byteArrayOutputStream.write((byte) Integer.parseInt(split[i2], 16));
                }
            }
        } else {
            byteArrayOutputStream.write(new byte[]{0, 0, 0, 0, 0, 0}, 0, 6);
        }
        return byteArrayOutputStream.toByteArray();
    }
}
