package com.epson.memcardacc;

import android.content.Context;

import com.epson.mobilephone.common.maintain2.MaintainPrinter2;

import epson.print.MyPrinter;

public class EscprLibPrinter {
    public static final int ERROR_PRINTER_CONNECTION = 2;
    public static final int ERROR_PRINTER_LOCATION = 1;
    public static final int NO_ERROR = 0;
    public static final int STATUS_BUSY = 2;
    public static final int STATUS_CANCELLING = 3;
    public static final int STATUS_ERROR = 4;
    public static final int STATUS_IDEL = 0;
    public static final int STATUS_PRINTING = 1;
    MaintainPrinter2 mMaintainPrinter;

    public int init(Context context, int i, boolean z) {
        EpLog.i();
        MyPrinter currentPrinter = getCurrentPrinter(context);
        String printerId = currentPrinter.getPrinterId();
        String ip = currentPrinter.getIp();
        int location = currentPrinter.getLocation();
        if (location != 1 && location != 3) {
            return 1;
        }
        mMaintainPrinter = getMaintainPrinter();
        mMaintainPrinter.doInitDriver(context, 2);
        if (mMaintainPrinter.doProbePrinter(i, printerId, ip, location) != 0) {
            mMaintainPrinter = null;
            return 2;
        } else if (mMaintainPrinter.doSetPrinter() != 0) {
            mMaintainPrinter = null;
            return 2;
        } else if (location == 1) {
            String doGetIp = mMaintainPrinter.doGetIp();
            if (ip.equals(doGetIp) || !z) {
                return 0;
            }
            currentPrinter.setIp(doGetIp);
            currentPrinter.setCurPrinter(context);
            return 0;
        } else if (location != 3 || printerId.equals(mMaintainPrinter.doGetId())) {
            return 0;
        } else {
            mMaintainPrinter = null;
            return 2;
        }
    }

    public int getStatus() {
        if (mMaintainPrinter.doGetStatus() != 0) {
            return 4;
        }
        switch (mMaintainPrinter.getMPrinterInfor().getMStatus()[0]) {
            case 0:
                return 0;
            case 1:
                return 1;
            case 2:
                return 2;
            case 3:
                return 3;
            default:
                return 4;
        }
    }

    public void release() {
        if (mMaintainPrinter != null) {
            mMaintainPrinter = null;
        }
    }

    public static int checkIdleOrError(Context context, int i) {
        EscprLibPrinter escprLibPrinter = new EscprLibPrinter();
        if (escprLibPrinter.init(context, i, false) != 0) {
            return 1;
        }
        int status = escprLibPrinter.getStatus();
        escprLibPrinter.release();
        if (status == 0 || status == 4) {
            return 0;
        }
        return 2;
    }

    private MyPrinter getCurrentPrinter(Context context) {
        return MyPrinter.getCurPrinter(context);
    }

    private MaintainPrinter2 getMaintainPrinter() {
        return MaintainPrinter2.getInstance();
    }
}
