package com.seewo.factoryaging.c1;

import android.content.SharedPreferences;
import android.util.Log;
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.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.Map;

/* JADX INFO: compiled from: AgingTimeHelper.kt */
/* JADX INFO: loaded from: classes.dex */
public final class c {
    public static final c a = new c();
    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 long e;

    /* JADX INFO: compiled from: AgingTimeHelper.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: AgingTimeHelper.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("aging_time", 0);
        }
    }

    private c() {
    }

    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(c cVar, l lVar, int i2, Object obj) {
        if ((i2 & 1) != 0) {
            lVar = null;
        }
        cVar.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("AgingTimeHelper", "loadTotalAgingTimeAsync: loaded=" + jA);
            if (lVar != null) {
                lVar.invoke(Long.valueOf(jA));
            }
        } catch (Throwable th) {
            Log.e("AgingTimeHelper", "loadTotalAgingTimeAsync: 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() {
        if (d >= 0) {
            Log.d("AgingTimeHelper", "getTotalAgingTime: from cache=" + d);
            return d;
        }
        try {
            Iterator<T> it = a().a().iterator();
            long jA = 0;
            while (it.hasNext()) {
                jA += ((o) it.next()).a();
            }
            d = jA;
            Log.d("AgingTimeHelper", "getTotalAgingTime: loaded=" + jA);
            return jA;
        } catch (Throwable th) {
            Log.e("AgingTimeHelper", "getTotalAgingTime: error, return 0", th);
            return 0L;
        }
    }

    public final void e() {
        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.a
            @Override // java.lang.Runnable
            public final void run() {
                c.i(lVar);
            }
        }).start();
    }

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