package com.vinfast.companion.core.managers;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes15.dex */
@kotlin.Metadata(d1 = {"\u00004\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\b\n\u0002\u0010\u0003\n\u0002\b\u0005\u0018\u0000 \u00112\u00020\u0001:\u0001\u001aB\u0013\b\u0007\u0012\b\b\u0001\u0010\u0003\u001a\u00020\u0002¢\u0006\u0004\b\u0004\u0010\u0005J\u001f\u0010\u000b\u001a\u00020\n2\u0006\u0010\u0007\u001a\u00020\u00062\u0006\u0010\t\u001a\u00020\bH\u0002¢\u0006\u0004\b\u000b\u0010\fJ\u0019\u0010\u000f\u001a\u0004\u0018\u00010\r2\u0006\u0010\u000e\u001a\u00020\rH\u0002¢\u0006\u0004\b\u000f\u0010\u0010J\u0019\u0010\u0011\u001a\u0004\u0018\u00010\r2\u0006\u0010\u000e\u001a\u00020\rH\u0002¢\u0006\u0004\b\u0011\u0010\u0010J\u001b\u0010\u0012\u001a\u0004\u0018\u00010\r2\b\u0010\u000e\u001a\u0004\u0018\u00010\rH\u0002¢\u0006\u0004\b\u0012\u0010\u0010J\u001b\u0010\u0013\u001a\u0004\u0018\u00010\r2\b\u0010\u000e\u001a\u0004\u0018\u00010\rH\u0002¢\u0006\u0004\b\u0013\u0010\u0010J\u0017\u0010\u0014\u001a\u00020\b2\u0006\u0010\u0007\u001a\u00020\u0006H\u0002¢\u0006\u0004\b\u0014\u0010\u0015J\u0017\u0010\u0017\u001a\u00020\n2\u0006\u0010\u0007\u001a\u00020\u0016H\u0016¢\u0006\u0004\b\u0017\u0010\u0018R\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0083\u0004¢\u0006\u0006\n\u0004\b\u0017\u0010\u0019¨\u0006\u001b"}, d2 = {"Lcom/vinfast/companion/core/managers/AppErrorManager;", "Lcom/vinfast/companion/core/managers/IErrorManager;", "Landroid/content/Context;", "context", "<init>", "(Landroid/content/Context;)V", "Lretrofit2/HttpException;", "throwable", "Lcom/vinfast/companion/core/managers/ServiceType;", "serviceType", "Lcom/vinfast/companion/core/base/ViewModelState$Error;", "g", "(Lretrofit2/HttpException;Lcom/vinfast/companion/core/managers/ServiceType;)Lcom/vinfast/companion/core/base/ViewModelState$Error;", "", "errJson", "c", "(Ljava/lang/String;)Ljava/lang/String;", "b", "e", "f", "d", "(Lretrofit2/HttpException;)Lcom/vinfast/companion/core/managers/ServiceType;", "", "a", "(Ljava/lang/Throwable;)Lcom/vinfast/companion/core/base/ViewModelState$Error;", "Landroid/content/Context;", "Companion", "core_release"}, k = 1, mv = {2, 2, 0}, xi = 48)
@kotlin.jvm.internal.SourceDebugExtension
public final class AppErrorManager implements com.vinfast.companion.core.managers.IErrorManager {

    /* JADX INFO: renamed from: a, reason: from kotlin metadata */
    private final android.content.Context context;

    /* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
    @kotlin.Metadata(k = 3, mv = {2, 2, 0}, xi = 48)
    public static final /* synthetic */ class WhenMappings {
        public static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[com.vinfast.companion.core.managers.ServiceType.values().length];
            try {
                iArr[com.vinfast.companion.core.managers.ServiceType.Charging.ordinal()] = 1;
            } catch (java.lang.NoSuchFieldError unused) {
            }
            try {
                iArr[com.vinfast.companion.core.managers.ServiceType.Payment.ordinal()] = 2;
            } catch (java.lang.NoSuchFieldError unused2) {
            }
            try {
                iArr[com.vinfast.companion.core.managers.ServiceType.HomeCharger.ordinal()] = 3;
            } catch (java.lang.NoSuchFieldError unused3) {
            }
            try {
                iArr[com.vinfast.companion.core.managers.ServiceType.Sota.ordinal()] = 4;
            } catch (java.lang.NoSuchFieldError unused4) {
            }
            a = iArr;
        }
    }

    public AppErrorManager(android.content.Context context) {
        kotlin.jvm.internal.Intrinsics.l(context, "context");
        this.context = context;
    }

    private final java.lang.String b(java.lang.String errJson) {
        try {
            return new org.json.JSONObject(errJson).getString("defaultMessage");
        } catch (java.lang.Exception unused) {
            return null;
        }
    }

    private final java.lang.String c(java.lang.String errJson) {
        try {
            return new org.json.JSONObject(errJson).getString("localized");
        } catch (java.lang.Exception unused) {
            return null;
        }
    }

    private final com.vinfast.companion.core.managers.ServiceType d(retrofit2.HttpException throwable) {
        okhttp3.Response responseG;
        okhttp3.Request request;
        okhttp3.HttpUrl httpUrlUrl;
        retrofit2.Response responseB = throwable.b();
        java.lang.String strEncodedPath = (responseB == null || (responseG = responseB.g()) == null || (request = responseG.request()) == null || (httpUrlUrl = request.url()) == null) ? null : httpUrlUrl.encodedPath();
        if (strEncodedPath != null && true == kotlin.text.StringsKt.f0(strEncodedPath, "/hccharging/", false, 2, (java.lang.Object) null)) {
            return com.vinfast.companion.core.managers.ServiceType.HomeCharger;
        }
        if (strEncodedPath != null && true == kotlin.text.StringsKt.f0(strEncodedPath, "/ccarcharging/", false, 2, (java.lang.Object) null)) {
            return com.vinfast.companion.core.managers.ServiceType.Charging;
        }
        if (strEncodedPath == null || true != kotlin.text.StringsKt.f0(strEncodedPath, "/ccarpayment/", false, 2, (java.lang.Object) null)) {
            return (strEncodedPath == null || true != kotlin.text.StringsKt.f0(strEncodedPath, "/ccar-sota/", false, 2, (java.lang.Object) null)) ? com.vinfast.companion.core.managers.ServiceType.Other : com.vinfast.companion.core.managers.ServiceType.Sota;
        }
        return com.vinfast.companion.core.managers.ServiceType.Payment;
    }

    private final java.lang.String e(java.lang.String errJson) {
        try {
            java.lang.String errorCode = ((com.vinfast.companion.core.base.errors.ApiError) new com.google.gson.GsonBuilder().create().fromJson(errJson, com.vinfast.companion.core.base.errors.ApiError.class)).getErrorCode();
            return errorCode == null ? ((com.vinfast.companion.core.base.errors.Auth0Error) new com.google.gson.GsonBuilder().create().fromJson(errJson, com.vinfast.companion.core.base.errors.Auth0Error.class)).a() : errorCode;
        } catch (java.lang.Exception e) {
            timber.log.Timber.a.r(e);
            return null;
        }
    }

    private final java.lang.String f(java.lang.String errJson) {
        try {
            java.lang.String message = ((com.vinfast.companion.core.base.errors.ApiError) new com.google.gson.GsonBuilder().create().fromJson(errJson, com.vinfast.companion.core.base.errors.ApiError.class)).getMessage();
            if (message == null) {
                message = ((com.vinfast.companion.core.base.errors.Auth0Error) new com.google.gson.GsonBuilder().create().fromJson(errJson, com.vinfast.companion.core.base.errors.Auth0Error.class)).getMessage();
            }
            if (message == null || message.length() == 0) {
                throw new java.lang.Exception();
            }
            return message;
        } catch (java.lang.Exception e) {
            timber.log.Timber.a.r(e);
            return null;
        }
    }

    /* JADX WARN: Code duplicated, block: B:24:0x0066  */
    /* JADX WARN: Code duplicated, block: B:42:0x00d2  */
    private final com.vinfast.companion.core.base.ViewModelState.Error g(retrofit2.HttpException throwable, com.vinfast.companion.core.managers.ServiceType serviceType) {
        java.lang.String strF;
        java.lang.String str;
        okhttp3.ResponseBody responseBodyD;
        retrofit2.Response responseB = throwable.b();
        java.lang.String strString = (responseB == null || (responseBodyD = responseB.d()) == null) ? null : responseBodyD.string();
        timber.log.Timber.a.a("Error---> %s", new java.lang.Object[]{strString});
        if (strString == null || strString.length() == 0) {
            java.lang.String string = com.vinfast.companion.core.managers.VFLocaleKt.a(this.context).getString(com.vinfast.companion.core.R.string.msg_service_unavailable);
            kotlin.jvm.internal.Intrinsics.k(string, "getString(...)");
            return new com.vinfast.companion.core.base.ViewModelState.Error(null, string, false, 5, null);
        }
        java.lang.String strE = e(strString);
        java.lang.String strC = c(strString);
        if (strC != null) {
            return new com.vinfast.companion.core.base.ViewModelState.Error(java.lang.String.valueOf(strE), strC, false, 4, null);
        }
        java.lang.String strB = b(strString);
        if (strB != null) {
            if (strE != null) {
                java.lang.String str2 = "(" + strE + ") " + strB;
                if (str2 == null) {
                    str = strB;
                } else {
                    str = str2;
                }
            } else {
                str = strB;
            }
            return new com.vinfast.companion.core.base.ViewModelState.Error(java.lang.String.valueOf(strE), str, false, 4, null);
        }
        int i = com.vinfast.companion.core.managers.AppErrorManager.WhenMappings.a[serviceType.ordinal()];
        if (i == 1 || i == 2 || i == 3 || i == 4) {
            strF = f(strString);
        } else {
            strF = com.vinfast.companion.core.extensions.ContextExtensionKt.t(com.vinfast.companion.core.managers.VFLocaleKt.a(this.context), "msg_error_" + strE);
        }
        if (strF == null) {
            java.lang.String string2 = com.vinfast.companion.core.managers.VFLocaleKt.a(this.context).getString(com.vinfast.companion.core.R.string.msg_service_unavailable);
            kotlin.jvm.internal.Intrinsics.k(string2, "getString(...)");
            if (strE != null) {
                strF = "(" + strE + ") " + string2;
                if (strF == null) {
                    strF = string2;
                }
            } else {
                strF = string2;
            }
        }
        return new com.vinfast.companion.core.base.ViewModelState.Error(java.lang.String.valueOf(strE), strF, false, 4, null);
    }

    @Override // com.vinfast.companion.core.managers.IErrorManager
    public com.vinfast.companion.core.base.ViewModelState.Error a(java.lang.Throwable throwable) {
        kotlin.jvm.internal.Intrinsics.l(throwable, "throwable");
        if (!(throwable instanceof java.net.UnknownHostException) && !(throwable instanceof java.net.ConnectException) && !(throwable instanceof java.net.SocketTimeoutException) && !(throwable instanceof javax.net.ssl.SSLHandshakeException) && !(throwable instanceof java.security.cert.CertificateException)) {
            if (throwable instanceof retrofit2.HttpException) {
                retrofit2.HttpException httpException = (retrofit2.HttpException) throwable;
                return g(httpException, d(httpException));
            }
            java.lang.String string = com.vinfast.companion.core.managers.VFLocaleKt.a(this.context).getString(com.vinfast.companion.core.R.string.msg_service_unavailable);
            kotlin.jvm.internal.Intrinsics.k(string, "getString(...)");
            return new com.vinfast.companion.core.base.ViewModelState.Error(null, string, false, 5, null);
        }
        if (com.vinfast.companion.core.extensions.ContextExtensionKt.w(this.context)) {
            java.lang.String string2 = com.vinfast.companion.core.managers.VFLocaleKt.a(this.context).getString(com.vinfast.companion.core.R.string.common_connection_error_unstable_message);
            kotlin.jvm.internal.Intrinsics.k(string2, "getString(...)");
            return new com.vinfast.companion.core.base.ViewModelState.Error("timeout", string2, false, 4, null);
        }
        java.lang.String string3 = com.vinfast.companion.core.managers.VFLocaleKt.a(this.context).getString(com.vinfast.companion.core.R.string.common_connection_error_offline_message);
        kotlin.jvm.internal.Intrinsics.k(string3, "getString(...)");
        return new com.vinfast.companion.core.base.ViewModelState.Error("offline", string3, false, 4, null);
    }
}
