package com.tencent.mm.plugin.backup.bakoldlogic.d;

import android.os.Looper;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.bp.d;
import com.tencent.mm.cd.h;
import com.tencent.mm.compatible.e.q;
import com.tencent.mm.g.a.aa;
import com.tencent.mm.g.a.bp;
import com.tencent.mm.kernel.g;
import com.tencent.mm.model.aw;
import com.tencent.mm.model.c;
import com.tencent.mm.modelvideo.t;
import com.tencent.mm.plugin.backup.b.a;
import com.tencent.mm.plugin.backup.bakoldlogic.d.c.1;
import com.tencent.mm.plugin.backup.bakoldlogic.d.c.2;
import com.tencent.mm.plugin.messenger.foundation.a.k;
import com.tencent.mm.pluginsdk.model.app.i;
import com.tencent.mm.pointers.PLong;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.ak;
import com.tencent.mm.sdk.platformtools.al;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.mm.storage.au;
import com.tencent.mm.storage.bs;
import com.tencent.mm.storage.o;
import com.tencent.mm.storage.z;
import com.tencent.mm.vfs.e;
import com.tencent.ytcommon.BuildConfig;
import java.util.HashMap;
import junit.framework.Assert;

public final class b extends a {
    private static b jAO;
    private static int jAR = 0;
    private c jAP;
    private a jAQ;
    private c jAS;

    static /* synthetic */ int Fc() {
        int i = jAR;
        jAR = i + 1;
        return i;
    }

    static /* synthetic */ int Fd() {
        int i = jAR;
        jAR = i - 1;
        return i;
    }

    static /* synthetic */ boolean a(b bVar, PLong pLong, PLong pLong2, PLong pLong3, a aVar, int i) {
        AppMethodBeat.i(18020);
        boolean a = bVar.a(pLong, pLong2, pLong3, aVar, i);
        AppMethodBeat.o(18020);
        return a;
    }

    public static b aUY() {
        b bVar;
        AppMethodBeat.i(18011);
        if (jAO == null) {
            bVar = new b();
            jAO = bVar;
            a(bVar);
        }
        bVar = jAO;
        AppMethodBeat.o(18011);
        return bVar;
    }

    public final void aSp() {
        jAO = null;
    }

    public final c aUZ() {
        AppMethodBeat.i(18012);
        if (this.jAP == null) {
            this.jAP = new c();
        }
        c cVar = this.jAP;
        AppMethodBeat.o(18012);
        return cVar;
    }

    public final a aVa() {
        AppMethodBeat.i(18013);
        if (this.jAQ == null) {
            this.jAQ = new a();
        }
        a aVar = this.jAQ;
        AppMethodBeat.o(18013);
        return aVar;
    }

    private static void aVb() {
        AppMethodBeat.i(18014);
        com.tencent.mm.sdk.b.a.xxA.m(new aa());
        AppMethodBeat.o(18014);
    }

    private c aVc() {
        AppMethodBeat.i(18015);
        if (this.jAS == null) {
            this.jAS = new c();
        }
        c cVar = this.jAS;
        AppMethodBeat.o(18015);
        return cVar;
    }

    public final void a(a aVar) {
        AppMethodBeat.i(18016);
        long anU = bo.anU();
        ab.i("MicroMsg.BakOldTempDbModel", "initTempDB needSyncPauser:%b %s", new Object[]{Boolean.FALSE, bo.dpG()});
        al.n(new 1(this, anU, aVar), 1000);
        AppMethodBeat.o(18016);
    }

    private boolean a(PLong pLong, PLong pLong2, PLong pLong3, a aVar, int i) {
        AppMethodBeat.i(18017);
        aw.ZK();
        c.closeDB();
        ab.d("MicroMsg.BakOldTempDbModel", "bakoldInitTempDBimp after close db");
        aw.ZK();
        long asZ = e.asZ(c.Rv());
        aw.ZK();
        pLong3.value = asZ + e.asZ(c.Rw());
        asZ = pLong3.value;
        aw.ZK();
        if (com.tencent.mm.plugin.backup.bakoldlogic.a.a.a(asZ, pLong, pLong2, c.getAccPath())) {
            aw.ZK();
            String Rv = c.Rv();
            StringBuilder stringBuilder = new StringBuilder();
            aw.ZK();
            e.y(Rv, stringBuilder.append(c.Rv()).append(".tem").toString());
            aw.ZK();
            Rv = c.Rw();
            stringBuilder = new StringBuilder();
            aw.ZK();
            e.y(Rv, stringBuilder.append(c.Rw()).append(".tem").toString());
            StringBuilder stringBuilder2 = new StringBuilder();
            aw.ZK();
            Rv = stringBuilder2.append(c.Rw()).append("-journal").toString();
            stringBuilder = new StringBuilder();
            aw.ZK();
            e.y(Rv, stringBuilder.append(c.Rw()).append(".tem-journal").toString());
            stringBuilder2 = new StringBuilder();
            aw.ZK();
            Rv = stringBuilder2.append(c.Rw()).append("-wal").toString();
            stringBuilder = new StringBuilder();
            aw.ZK();
            e.y(Rv, stringBuilder.append(c.Rw()).append(".tem-wal").toString());
            ab.i("MicroMsg.BakOldTempDbModel", "bakoldInitTempDBimp before reset account");
            g.RN().release();
            g.RN().initialize();
            ab.i("MicroMsg.BakOldTempDbModel", "bakoldInitTempDBimp before TemAccStg setAccInfo");
            c aVc = aVc();
            aw.ZK();
            String accPath = c.getAccPath();
            aw.ZK();
            ab.i("MicroMsg.BakOldTempStorage", "accPath:%s, accUin:%d", new Object[]{accPath, Integer.valueOf(c.QF())});
            aVc.uin = r2;
            aVc.eJM = accPath;
            ab.i("MicroMsg.BakOldTempDbModel", "bakoldInitTempDBimp before TemAccStg initDB");
            c aVc2 = aVc();
            stringBuilder2 = new StringBuilder();
            aw.ZK();
            accPath = stringBuilder2.append(c.Rv()).append(".tem").toString();
            aw.ZK();
            int QF = c.QF();
            stringBuilder2 = new StringBuilder();
            aw.ZK();
            ab.i("MicroMsg.BakOldTempStorage", "bakoldInitDB isTempDB:%s  cache:%s uin:%s db:%s %s", new Object[]{aVc2.jBf, accPath, Integer.valueOf(QF), stringBuilder2.append(c.Rw()).append(".tem").toString(), bo.dpG()});
            if (aVc2.eJN != null) {
                ab.e("MicroMsg.BakOldTempStorage", "bakoldInitDB dataDB is already init!!!");
            }
            if (i > 0) {
                if (!(aVc2.jBf == null || aVc2.jBf.booleanValue())) {
                    ab.e("MicroMsg.BakOldTempStorage", "bakoldInitDB isTempDB is false!!!");
                    aVc2.aVf();
                }
                Assert.assertTrue("bakoldInitDB here  isTempDb should null :" + aVc2.jBf, aVc2.jBf == null);
            }
            aVc2.jBf = Boolean.TRUE;
            aVc2.eJN = new h(new 2(aVc2));
            h hVar = aVc2.eJN;
            String str = BuildConfig.FLAVOR;
            long j = (long) QF;
            q.LK();
            if (hVar.b(accPath, r2, str, j, new HashMap(), false)) {
                aVc2.jym = new z(aVc2.eJN);
                k kVar = (k) g.K(k.class);
                aVc2.jyn = kVar.d(aVc2.eJN);
                aVc2.jyp = kVar.e(aVc2.eJN);
                aVc2.jyo = kVar.a(aVc2.eJN, aVc2.jyn, aVc2.jyp);
                aVc2.jyo.a(new au(aVc2.jyo));
                aVc2.jyo.a(new o(aVc2.jyo));
                aVc2.jys = kVar.f(aVc2.eJN);
                aVc2.jyq = new com.tencent.mm.at.g(aVc2.eJN);
                if (d.afj("emoji")) {
                    aVc2.jyr = new com.tencent.mm.storage.emotion.d(aVc2.eJN);
                }
                aVc2.jyu = ((com.tencent.mm.plugin.chatroom.a.c) g.K(com.tencent.mm.plugin.chatroom.a.c.class)).c(aVc2.eJN);
                aVc2.jyt = new t(aVc2.eJN);
                aVc2.jyv = new com.tencent.mm.pluginsdk.model.app.k(aVc2.eJN);
                aVc2.jyw = new i(aVc2.eJN);
                aVc2.jyx = new com.tencent.mm.pluginsdk.model.app.c(aVc2.eJN);
                aVc2.jBd = new bs(aVc2.jym);
                aVc2.jBd.c(new 1(aVc2));
                aVc2.jBd.duo();
                ab.i("MicroMsg.BakOldTempDbModel", "bakoldInitTempDBimp after TemAccStg initDB");
                aVb();
                aVar.jBa = true;
                aVar.jBb = pLong.value;
                aVar.jBc = pLong2.value;
                aVar.dbSize = pLong3.value;
                com.tencent.mm.plugin.backup.bakoldlogic.a.a.Fo(com.tencent.mm.plugin.backup.bakoldlogic.a.a.aUy());
                AppMethodBeat.o(18017);
                return true;
            }
            com.tencent.mm.model.b bVar = new com.tencent.mm.model.b((byte) 0);
            AppMethodBeat.o(18017);
            throw bVar;
        }
        ab.e("MicroMsg.BakOldTempDbModel", "bakoldInitTempDBimp data free error, len %d", new Object[]{Long.valueOf(pLong3.value)});
        aVar.jBa = false;
        aVar.jBb = pLong.value;
        aVar.jBc = pLong2.value;
        aVar.dbSize = pLong3.value;
        AppMethodBeat.o(18017);
        return false;
    }

    public static void aVd() {
        AppMethodBeat.i(18018);
        StringBuilder stringBuilder = new StringBuilder();
        aw.ZK();
        e.deleteFile(stringBuilder.append(c.Rv()).append(".tem").toString());
        stringBuilder = new StringBuilder();
        aw.ZK();
        e.deleteFile(stringBuilder.append(c.Rv()).append(".ini.tem").toString());
        stringBuilder = new StringBuilder();
        aw.ZK();
        e.deleteFile(stringBuilder.append(c.Rw()).append(".tem").toString());
        stringBuilder = new StringBuilder();
        aw.ZK();
        e.deleteFile(stringBuilder.append(c.Rw()).append(".ini.tem").toString());
        AppMethodBeat.o(18018);
    }

    public final void c(Runnable runnable, int i) {
        AppMethodBeat.i(18019);
        if (i < 0 || jAR == 0) {
            if (i < 0) {
                ab.e("MicroMsg.BakOldTempDbModel", "closeTempDB no left tryCount!!");
            }
            ab.i("MicroMsg.BakOldTempDbModel", "closeDB before");
            aVc().aVf();
            ab.i("MicroMsg.BakOldTempDbModel", "closeDB after");
            if (runnable != null) {
                runnable.run();
                AppMethodBeat.o(18019);
                return;
            }
        }
        new ak(Looper.getMainLooper()).postDelayed(new 3(this, runnable, i), 500);
        AppMethodBeat.o(18019);
    }

    static /* synthetic */ void aVe() {
        AppMethodBeat.i(18021);
        long anU = bo.anU();
        aw.ZK();
        c.closeDB();
        StringBuilder stringBuilder = new StringBuilder();
        aw.ZK();
        String stringBuilder2 = stringBuilder.append(c.Rv()).append(".tem").toString();
        aw.ZK();
        e.y(stringBuilder2, c.Rv());
        stringBuilder = new StringBuilder();
        aw.ZK();
        stringBuilder2 = stringBuilder.append(c.Rw()).append(".tem").toString();
        aw.ZK();
        e.y(stringBuilder2, c.Rw());
        stringBuilder = new StringBuilder();
        aw.ZK();
        stringBuilder2 = stringBuilder.append(c.Rw()).append(".tem-journal").toString();
        StringBuilder stringBuilder3 = new StringBuilder();
        aw.ZK();
        e.y(stringBuilder2, stringBuilder3.append(c.Rw()).append("-journal").toString());
        stringBuilder = new StringBuilder();
        aw.ZK();
        stringBuilder2 = stringBuilder.append(c.Rw()).append(".tem-wal").toString();
        stringBuilder3 = new StringBuilder();
        aw.ZK();
        e.y(stringBuilder2, stringBuilder3.append(c.Rw()).append("-wal").toString());
        com.tencent.mm.sdk.b.a.xxA.m(new bp());
        g.RN().release();
        g.RN().initialize();
        aVd();
        aVb();
        ab.i("MicroMsg.BakOldTempDbModel", "bakoldRecoverFromTempDbImp finish cost time[%d]", new Object[]{Long.valueOf(bo.gb(anU))});
        AppMethodBeat.o(18021);
    }
}
