package com.seewo.factoryaging.c1;

import android.content.SharedPreferences;
import android.util.Log;
import com.cvte.vman.VmanMiddleWare;
import com.seewo.factoryaging.FactoryAgingApplication;
import com.seewo.factoryaging.o;
import com.seewo.factoryaging.p;
import j.h0;
import j.i;
import j.k;
import j.k0.m;
import j.p0.c.l;
import j.p0.d.r;
import j.p0.d.s;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/* JADX INFO: compiled from: DdrTestTimeHelper.kt */
/* JADX INFO: loaded from: classes.dex */
public final class d {
    public static final d a = new d();
    private static final i b = k.b(a.b);
    private static final i c = k.b(b.b);
    private static volatile long d = -1;
    private static volatile boolean e;

    /* JADX INFO: compiled from: DdrTestTimeHelper.kt */
    static final class a extends s implements j.p0.c.a<String> {
        public static final a b = new a();

        a() {
            super(0);
        }

        @Override // j.p0.c.a
        /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
        public final String invoke() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        }
    }

    /* JADX INFO: compiled from: DdrTestTimeHelper.kt */
    static final class b extends s implements j.p0.c.a<SharedPreferences> {
        public static final b b = new b();

        b() {
            super(0);
        }

        @Override // j.p0.c.a
        /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
        public final SharedPreferences invoke() {
            return FactoryAgingApplication.e.d().getApplicationContext().getSharedPreferences("ddr_test_time", 0);
        }
    }

    private d() {
    }

    private final String b() {
        Object value = b.getValue();
        r.e(value, "<get-keyStartTime>(...)");
        return (String) value;
    }

    private final SharedPreferences c() {
        Object value = c.getValue();
        r.e(value, "<get-mSp>(...)");
        return (SharedPreferences) value;
    }

    /* JADX WARN: Multi-variable type inference failed */
    public static /* synthetic */ void h(d dVar, l lVar, int i2, Object obj) {
        if ((i2 & 1) != 0) {
            lVar = null;
        }
        dVar.g(lVar);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void i(l lVar) {
        try {
            Iterator<T> it = a.a().a().iterator();
            long jA = 0;
            while (it.hasNext()) {
                jA += ((o) it.next()).a();
            }
            d = jA;
            Log.d("DdrTestTimeHelper", "loadTotalDdrTestTimeAsync: loaded=" + jA);
            if (lVar != null) {
                lVar.invoke(Long.valueOf(jA));
            }
        } catch (Throwable th) {
            Log.e("DdrTestTimeHelper", "loadTotalDdrTestTimeAsync: error", th);
            if (lVar != null) {
                lVar.invoke(0L);
            }
        }
    }

    public final p a() {
        o oVar;
        Map<String, ?> all = c().getAll();
        r.e(all, "mSp.all");
        ArrayList arrayList = new ArrayList();
        for (Map.Entry<String, ?> entry : all.entrySet()) {
            if (entry.getValue() instanceof Long) {
                String key = entry.getKey();
                r.e(key, "it.key");
                String str = key;
                Object value = entry.getValue();
                if (value == null) {
                    throw new NullPointerException("null cannot be cast to non-null type kotlin.Long");
                }
                oVar = new o(str, ((Long) value).longValue());
            } else {
                oVar = null;
            }
            if (oVar != null) {
                arrayList.add(oVar);
            }
        }
        return new p(arrayList);
    }

    public final long d() {
        Log.d("DdrTestTimeHelper", "getTotalDdrTestTime: cachedTotalTime=" + d);
        if (d >= 0) {
            Log.d("DdrTestTimeHelper", "getTotalDdrTestTime: returning from cache=" + d);
            return d;
        }
        Log.w("DdrTestTimeHelper", "getTotalDdrTestTime: cache not ready, loading from SharedPreferences synchronously!");
        try {
            List<o> listA = a().a();
            Log.d("DdrTestTimeHelper", "getTotalDdrTestTime: ddrTimeList has " + listA.size() + " entries");
            int i2 = 0;
            for (Object obj : listA) {
                int i3 = i2 + 1;
                if (i2 < 0) {
                    m.o();
                    throw null;
                }
                o oVar = (o) obj;
                Log.d("DdrTestTimeHelper", "getTotalDdrTestTime: entry[" + i2 + "] startTime=" + oVar.b() + ", agingTime=" + oVar.a());
                i2 = i3;
            }
            Iterator<T> it = listA.iterator();
            long jA = 0;
            while (it.hasNext()) {
                jA += ((o) it.next()).a();
            }
            d = jA;
            Log.d("DdrTestTimeHelper", "getTotalDdrTestTime: loaded and cached total=" + jA);
            return jA;
        } catch (Throwable th) {
            Log.e("DdrTestTimeHelper", "getTotalDdrTestTime: error, returning 0", th);
            return 0L;
        }
    }

    public final void e() {
        if (e) {
            return;
        }
        e = true;
        h(this, null, 1, null);
    }

    public final void g(final l<? super Long, h0> lVar) {
        new Thread(new Runnable() { // from class: com.seewo.factoryaging.c1.b
            @Override // java.lang.Runnable
            public final void run() {
                d.i(lVar);
            }
        }).start();
    }

    public final boolean j() {
        boolean zNeedToMemoryTest = VmanMiddleWare.getInstance().getFaultDetect().needToMemoryTest();
        boolean zJ = com.seewo.vcommons.data.c.j("sys.aging.debug.ddr.test", false);
        boolean z = zNeedToMemoryTest || zJ;
        if (z) {
            e();
        }
        Log.d("DdrTestTimeHelper", "needDdrTest: " + z + " (vman=" + zNeedToMemoryTest + ", property=" + zJ + ')');
        return z;
    }

    public final void k(long j2) {
        try {
            c().edit().putLong(b(), j2).apply();
            if (d >= 0) {
                d++;
            }
        } catch (Throwable th) {
            Log.e("DdrTestTimeHelper", "saveDdrTestTime: error", th);
        }
    }
}
