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 this.mEcClientLib.Initialize();
    }

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

    private int internalLogin(String str, String str2, String str3) {
        return this.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 = this.mEcClientLib.Login(quoteForJsonString, quoteForJsonString2, loadClientId);
        if (Login == 0 && loadClientId == null) {
            saveClientId(context, getClientId());
        }
        return Login;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    public String getClientId() {
        return this.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();
        }
    }
}
