package epson.print.ecclient;

import android.content.Context;

import com.epson.mobilephone.common.EPLog;

import epson.common.Constants;
import epson.provider.SharedPreferencesProvider;

public class EpsonConnectAccess {
    private static Factory sFactory;
    EcClientLib mEcClientLib = new EcClientLib();

    public static class RemotePrinterInfo {
        public String mDeviceId;
        public String mPrinterName;
        public String mSerialNumber;
    }

    EpsonConnectAccess() {
    }

    public boolean Initialize() {
        return mEcClientLib.Initialize();
    }

    public void Terminate() {
        mEcClientLib.Terminate();
    }

    private int internalLogin(String str, String str2, String str3) {
        return mEcClientLib.Login(str, str2, str3);
    }

    public int Login(Context context, String str, String str2) {
        String quoteForJsonString = EcClientLibUtil.quoteForJsonString(str);
        String quoteForJsonString2 = EcClientLibUtil.quoteForJsonString(str2);
        String loadClientId = loadClientId(context);
        int Login = mEcClientLib.Login(quoteForJsonString, quoteForJsonString2, loadClientId);
        if (Login == 0 && loadClientId == null) {
            saveClientId(context, getClientId());
        }
        return Login;
    }

    public int logout() {
        return mEcClientLib.Logout();
    }

    private int RegPrinter(String str, String str2, String str3) {
        return mEcClientLib.RegPrinter(str, str2, str3);
    }

    public int GetCapability() {
        return mEcClientLib.GetCapability();
    }

    public int GetDefaultSetting() {
        return mEcClientLib.GetDefaultSetting();
    }

    public int GetPrintLogUri(String str, String str2, String str3, String str4) {
        return mEcClientLib.GetPrintLogUri(str, str2, str3, str4);
    }

    public int CreateJob(int i, String str, int i2, EpsJobAttrib epsJobAttrib, int i3, int i4) {
        return mEcClientLib.CreateJob(i, str, i2, epsJobAttrib, i3, i4);
    }

    public int EndJob() {
        return mEcClientLib.EndJob();
    }

    public int UploadFile(String str, int i, int i2) {
        return mEcClientLib.UploadFile(str, i, i2);
    }

    public int GetRenderingStatus() {
        return mEcClientLib.GetRenderingStatus();
    }

    public int DownloadPreview(int i, String str) {
        return mEcClientLib.DownloadPreview(i, str);
    }

    public int ChangePrintSetting(EpsJobAttrib epsJobAttrib, String str, String str2, boolean z) {
        return mEcClientLib.ChangePrintSetting(epsJobAttrib, str, str2, z);
    }

    public int StartPrint(int i, int i2) {
        return mEcClientLib.StartPrint(i, i2);
    }

    public void cancel() {
        mEcClientLib.cancel();
    }

    public void resetCancel() {
        mEcClientLib.resetCancel();
    }

    public RemotePrinterInfo oldLibGetPrinterInfo() {
        if (GetCapability() != 0) {
            return null;
        }
        RemotePrinterInfo remotePrinterInfo = new RemotePrinterInfo();
        remotePrinterInfo.mSerialNumber = "";
        remotePrinterInfo.mPrinterName = mEcClientLib.printerName();
        remotePrinterInfo.mDeviceId = remotePrinterInfo.mPrinterName;
        return remotePrinterInfo;
    }

    public RemotePrinterInfo newLibGetPrinterInfo() {
        if (this.mEcClientLib.GetPrinterInfo() != 0) {
            return null;
        }
        RemotePrinterInfo remotePrinterInfo = new RemotePrinterInfo();
        remotePrinterInfo.mDeviceId = mEcClientLib.deviceId();
        remotePrinterInfo.mSerialNumber = mEcClientLib.serialNumber();
        remotePrinterInfo.mPrinterName = mEcClientLib.printerName2();
        return remotePrinterInfo;
    }

    public RemotePrinterInfo libGetPrinterInfo() {
        return newLibGetPrinterInfo();
    }

    public String getClientId() {
        return mEcClientLib.clientId();
    }

    public static EpsonConnectAccess getInstance() {
        setFactory();
        return sFactory.getEpsonConnectAccessClass();
    }

    public static synchronized int registRemotePrinter(Context context, String str, String str2) {
        synchronized (EpsonConnectAccess.class) {
            EpsonConnectAccess instance = getInstance();
            if (!instance.Initialize()) {
                return -1100;
            }
            String quoteForJsonString = EcClientLibUtil.quoteForJsonString(str);
            String quoteForJsonString2 = EcClientLibUtil.quoteForJsonString(str2);
            String loadClientId = loadClientId(context);
            int RegPrinter = instance.RegPrinter(quoteForJsonString, quoteForJsonString2, loadClientId);
            if (RegPrinter == 0) {
                if (loadClientId == null) {
                    saveClientId(context, instance.getClientId());
                }
                instance.Terminate();
                return RegPrinter;
            }
            instance.Terminate();
            return RegPrinter;
        }
    }

    public static RemotePrinterInfo getPrinterInfo(Context context, String str, String str2) {
        EpsonConnectAccess instance = getInstance();
        if (!instance.Initialize() || instance.Login(context, str, str2) != 0) {
            return null;
        }
        RemotePrinterInfo libGetPrinterInfo = instance.libGetPrinterInfo();
        instance.logout();
        instance.Terminate();
        return libGetPrinterInfo;
    }

    static String loadClientId(Context context) {
        String string = SharedPreferencesProvider.getInstace(context).getString(Constants.PRINTER_CLIENT_ID, (String) null);
        EPLog.d("▲clientId = " + string);
        if (string == null || string.length() <= 0) {
            return null;
        }
        return string;
    }

    static void saveClientId(Context context, String str) {
        EPLog.d("▲ save  clientId = " + str);
        SharedPreferencesProvider.getInstace(context).edit().putString(Constants.PRINTER_CLIENT_ID, str).apply();
    }

    static Factory setFactory() {
        if (sFactory == null) {
            sFactory = new Factory();
        }
        return sFactory;
    }

    public static class Factory {
        public EpsonConnectAccess getEpsonConnectAccessClass() {
            return new EpsonConnectAccess();
        }
    }

    public static class AltEpsonConnectAccess extends EpsonConnectAccess {
        public RemotePrinterInfo libGetPrinterInfo() {
            return oldLibGetPrinterInfo();
        }
    }

    public static class AltFactory extends Factory {
        public EpsonConnectAccess getEpsonConnectAccessClass() {
            return new AltEpsonConnectAccess();
        }
    }
}
