package com.bytedance.android.livesdk.i18n;

import X.0aP;
import X.0b0;
import X.2cN;
import X.7lW;
import X.C1490Riw;
import X.C1491Rix;
import X.C1495Rj1;
import X.C1498Rj4;
import X.C1510RjG;
import X.C1511RjH;
import X.C1512RjI;
import X.C1513RjJ;
import X.C1514RjK;
import X.C1515RjL;
import X.C1516RjM;
import X.C1517RjN;
import X.Mk2;
import X.Mk3;
import X.PhE;
import X.PiN;
import X.PlI;
import android.content.Context;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class I18nDbManager {
    public static volatile C1495Rj1 informationDao;
    public static volatile boolean isPrepared;
    public static volatile C1498Rj4 translationDao;
    public Map<String, String> cachedTranslationMap;
    public long cachedVersion;
    public C1511RjH dbCallback;
    public boolean isQuerying;
    public boolean isUpdating;
    public String locale;
    public 2cN queryDisposable;
    public 2cN updateDisposable;

    static {
        Covode.recordClassIndex(16845);
    }

    private void updateCachedContentIfNeed() {
        long j = this.cachedVersion;
        if (j >= 0) {
            updateTranslations(j, this.cachedTranslationMap);
            this.cachedVersion = -1;
            this.cachedTranslationMap = null;
        }
    }

    public void destroy() {
        0aP.LIZ(3, "i18n_translation", "I18nDbManager destroy");
        2cN r0 = this.queryDisposable;
        if (r0 != null && !r0.isDisposed()) {
            this.queryDisposable.dispose();
        }
        2cN r02 = this.updateDisposable;
        if (r02 != null && !r02.isDisposed()) {
            this.updateDisposable.dispose();
        }
        this.dbCallback = null;
    }

    public void queryTranslations() {
        0aP.LIZ(3, "i18n_translation", "query translations");
        if (this.isQuerying) {
            0aP.LIZ(3, "i18n_translation", "is querying, return");
            return;
        }
        this.isQuerying = true;
        this.queryDisposable = PiN.LIZ(new C1513RjJ(this)).LIZIZ(PhE.LIZIZ(PlI.LIZJ)).LIZ(new C1514RjK(this), new C1515RjL(this));
    }

    public final /* synthetic */ C1510RjG lambda$queryTranslations$0$I18nDbManager() {
        C1490Riw LIZ = informationDao.LIZ("locale");
        if (LIZ == null) {
            throw new Exception("locale info not found in db");
        } else if (TextUtils.equals(this.locale, LIZ.LIZIZ)) {
            C1510RjG rjG = new C1510RjG();
            rjG.LIZ = this.locale;
            C1490Riw LIZ2 = informationDao.LIZ("version");
            if (LIZ2 != null) {
                rjG.LIZIZ = Long.valueOf(LIZ2.LIZIZ).longValue();
                List<C1491Rix> LIZ3 = translationDao.LIZ();
                if (!LIZ3.isEmpty()) {
                    0aP.LIZ(3, "i18n_translation", LIZ3.size() + " translations found in db, locale is " + this.locale + ", version is " + rjG.LIZIZ);
                    HashMap hashMap = new HashMap();
                    for (C1491Rix next : LIZ3) {
                        if (!TextUtils.isEmpty(next.LIZ) && !TextUtils.isEmpty(next.LIZIZ)) {
                            hashMap.put(next.LIZ, next.LIZIZ);
                        }
                    }
                    rjG.LIZJ = hashMap;
                    return rjG;
                }
                throw new Exception("translations not found for locale: " + this.locale);
            }
            throw new Exception("version info not found in db");
        } else {
            throw new Exception("locale in db is " + LIZ.LIZIZ + ", but you are request for " + this.locale);
        }
    }

    public final /* synthetic */ void lambda$updateTranslations$4$I18nDbManager(Boolean bool) {
        this.isUpdating = false;
        updateCachedContentIfNeed();
    }

    public static Context INVOKEVIRTUAL_com_bytedance_android_livesdk_i18n_I18nDbManager_com_ss_android_ugc_aweme_lancet_launch_ApplicationContextLancet_getApplicationContext(Context context) {
        Context applicationContext = context.getApplicationContext();
        if (7lW.LIZJ && applicationContext == null) {
            return 7lW.LIZ;
        }
        return applicationContext;
    }

    public final /* synthetic */ void lambda$queryTranslations$1$I18nDbManager(C1510RjG rjG) {
        this.isQuerying = false;
        C1511RjH rjH = this.dbCallback;
        if (rjH != null) {
            rjH.LIZ(this.locale, rjG);
        }
    }

    public final /* synthetic */ void lambda$queryTranslations$2$I18nDbManager(Throwable th) {
        this.isQuerying = false;
        C1511RjH rjH = this.dbCallback;
        if (rjH != null) {
            rjH.LIZ(this.locale, new Exception(th));
        }
    }

    public final /* synthetic */ void lambda$updateTranslations$5$I18nDbManager(Throwable th) {
        this.isUpdating = false;
        C1511RjH rjH = this.dbCallback;
        if (rjH != null) {
            rjH.LIZ(new Exception(th));
        }
        updateCachedContentIfNeed();
    }

    /* JADX WARNING: Code restructure failed: missing block: B:14:0x0039, code lost:
        com.bytedance.frameworks.apm.trace.MethodCollector.o(9195);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:15:0x003c, code lost:
        return;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static void prepareInit(android.content.Context r5) {
        /*
            r4 = 9195(0x23eb, float:1.2885E-41)
            com.bytedance.frameworks.apm.trace.MethodCollector.i(r4)
            boolean r0 = isPrepared
            if (r0 != 0) goto L_0x0043
            java.lang.Class<com.bytedance.android.livesdk.i18n.I18nDbManager> r3 = com.bytedance.android.livesdk.i18n.I18nDbManager.class
            monitor-enter(r3)
            boolean r0 = isPrepared     // Catch:{ all -> 0x003d }
            if (r0 != 0) goto L_0x0038
            if (r5 != 0) goto L_0x0017
            monitor-exit(r3)     // Catch:{ all -> 0x003d }
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r4)
            return
        L_0x0017:
            android.content.Context r2 = INVOKEVIRTUAL_com_bytedance_android_livesdk_i18n_I18nDbManager_com_ss_android_ugc_aweme_lancet_launch_ApplicationContextLancet_getApplicationContext(r5)     // Catch:{ all -> 0x003d }
            java.lang.Class<com.bytedance.android.livesdk.i18n.db.I18nDatabase> r1 = com.bytedance.android.livesdk.i18n.db.I18nDatabase.class
            java.lang.String r0 = "i18n_live"
            X.0G4 r0 = X.0G3.LIZ(r2, r1, r0)     // Catch:{ all -> 0x003d }
            X.0G7 r1 = r0.LIZIZ()     // Catch:{ all -> 0x003d }
            com.bytedance.android.livesdk.i18n.db.I18nDatabase r1 = (com.bytedance.android.livesdk.i18n.db.I18nDatabase) r1     // Catch:{ all -> 0x003d }
            X.Rj4 r0 = r1.LJIIIIZZ()     // Catch:{ all -> 0x003d }
            translationDao = r0     // Catch:{ all -> 0x003d }
            X.Rj1 r0 = r1.LJIIIZ()     // Catch:{ all -> 0x003d }
            informationDao = r0     // Catch:{ all -> 0x003d }
            r0 = 1
            isPrepared = r0     // Catch:{ all -> 0x003d }
        L_0x0038:
            monitor-exit(r3)     // Catch:{ all -> 0x003d }
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r4)
            return
        L_0x003d:
            r0 = move-exception
            monitor-exit(r3)
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r4)
            throw r0
        L_0x0043:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r4)
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.android.livesdk.i18n.I18nDbManager.prepareInit(android.content.Context):void");
    }

    public I18nDbManager(String str, C1511RjH rjH) {
        this.locale = str;
        this.dbCallback = rjH;
        prepareInit(INVOKEVIRTUAL_com_bytedance_android_livesdk_i18n_I18nDbManager_com_ss_android_ugc_aweme_lancet_launch_ApplicationContextLancet_getApplicationContext(0b0.LJ()));
    }

    public void updateTranslations(long j, Map<String, String> map) {
        0aP.LIZ(3, "i18n_translation", "update translations in db");
        if (TextUtils.isEmpty(this.locale) || map == null || map.isEmpty()) {
            if (TextUtils.isEmpty(this.locale)) {
                0aP.LIZ(6, "i18n_translation", "locale is empty, return");
            } else {
                0aP.LIZ(6, "i18n_translation", "translation map is empty, return");
            }
        } else if (this.isUpdating) {
            this.cachedVersion = j;
            this.cachedTranslationMap = map;
            0aP.LIZ(3, "i18n_translation", "is updating, saved as cache and return");
        } else {
            this.isUpdating = true;
            this.updateDisposable = PiN.LIZ(new C1512RjI(this, map, j)).LIZ(Mk2.LIZ(Mk3.LIZ)).LIZIZ(PhE.LIZIZ(PlI.LIZJ)).LIZ(new C1516RjM(this), new C1517RjN(this));
        }
    }

    public final /* synthetic */ Boolean lambda$updateTranslations$3$I18nDbManager(Map map, long j) {
        Set<Map.Entry> entrySet = map.entrySet();
        ArrayList arrayList = new ArrayList();
        for (Map.Entry entry : entrySet) {
            if (!TextUtils.isEmpty((CharSequence) entry.getKey()) && !TextUtils.isEmpty((CharSequence) entry.getValue())) {
                arrayList.add(new C1491Rix((String) entry.getKey(), (String) entry.getValue()));
            }
        }
        translationDao.LIZIZ();
        translationDao.LIZ(arrayList);
        informationDao.LIZ(new C1490Riw("locale", this.locale));
        informationDao.LIZ(new C1490Riw("version", String.valueOf(j)));
        0aP.LIZ(3, "i18n_translation", arrayList.size() + " translations saved in db, locale is " + this.locale + ", version is " + j);
        return true;
    }
}
