package com.apm.insight.runtime;

import android.content.Context;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import com.apm.insight.entity.Header;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes.dex */
public final class o {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private static o f5511a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private File f5512b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private File f5513c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private File f5514d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private Context f5515e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private a f5516f = null;

    public static class a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private long f5519a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private long f5520b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private File f5521c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private JSONObject f5522d;

        public /* synthetic */ a(File file, byte b10) {
            this(file);
        }

        public static /* synthetic */ void d(a aVar) {
            aVar.f5521c.delete();
        }

        private a(File file) {
            this.f5522d = null;
            this.f5521c = file;
            String[] strArrSplit = file.getName().split("-|\\.");
            if (strArrSplit.length >= 2) {
                this.f5519a = Long.parseLong(strArrSplit[0]);
                this.f5520b = Long.parseLong(strArrSplit[1]);
                return;
            }
            String name = file.getName();
            if (TextUtils.isEmpty(name) || name.length() < 13) {
                return;
            }
            String strSubstring = name.substring(0, 13);
            if (TextUtils.isDigitsOnly(strSubstring)) {
                long j10 = Long.parseLong(strSubstring);
                this.f5519a = j10;
                this.f5520b = j10;
            }
        }

        /* JADX INFO: Access modifiers changed from: private */
        public JSONObject a() {
            if (this.f5522d == null) {
                try {
                    this.f5522d = new JSONObject(com.apm.insight.l.f.a(this.f5521c.getAbsolutePath(), "\n"));
                } catch (Throwable unused) {
                }
                if (this.f5522d == null) {
                    this.f5522d = new JSONObject();
                }
            }
            return this.f5522d;
        }

        public static /* synthetic */ boolean a(a aVar, long j10) {
            long j11 = aVar.f5519a;
            if (j11 > j10 && j11 - j10 > 604800000) {
                return true;
            }
            long j12 = aVar.f5520b;
            if (j12 >= j10 || j10 - j12 <= 604800000) {
                return aVar.f5521c.lastModified() < j10 && j10 - aVar.f5521c.lastModified() > 604800000;
            }
            return true;
        }
    }

    private o(Context context) {
        File fileC = com.apm.insight.l.j.c(context);
        if (!fileC.exists() || (!fileC.isDirectory() && fileC.delete())) {
            fileC.mkdirs();
            com.apm.insight.runtime.a.b.a();
        }
        this.f5512b = fileC;
        this.f5513c = new File(fileC, "did");
        this.f5514d = new File(fileC, "device_uuid");
        this.f5515e = context;
    }

    public static o a() {
        if (f5511a == null) {
            f5511a = new o(com.apm.insight.e.g());
        }
        return f5511a;
    }

    public final String b() {
        try {
            return com.apm.insight.l.f.a(this.f5513c.getAbsolutePath(), "\n");
        } catch (Throwable unused) {
            return SessionDescription.SUPPORTED_SDP_VERSION;
        }
    }

    public final String c() {
        try {
            return com.apm.insight.l.f.a(this.f5514d.getAbsolutePath(), "\n");
        } catch (Throwable unused) {
            return null;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:22:0x005e  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a(Map<String, Object> map, JSONArray jSONArray) {
        char c6;
        JSONObject jSONObjectA = Header.a(this.f5515e).a(map);
        if (Header.c(jSONObjectA)) {
            return;
        }
        long jCurrentTimeMillis = System.currentTimeMillis();
        if (this.f5516f == null) {
            c(".ctx");
        }
        a aVar = this.f5516f;
        if (aVar == null) {
            a(jCurrentTimeMillis, jCurrentTimeMillis, jSONObjectA, jSONArray);
            return;
        }
        JSONObject jSONObjectA2 = aVar.a();
        if (Header.c(jSONObjectA2)) {
            c6 = 2;
        } else if (Header.c(jSONObjectA)) {
            c6 = 0;
        } else if (String.valueOf(jSONObjectA.opt("update_version_code")).equals(String.valueOf(jSONObjectA2.opt("update_version_code"))) && Header.d(jSONObjectA2)) {
            c6 = 1;
        }
        if (c6 == 1) {
            a(aVar.f5519a, jCurrentTimeMillis, jSONObjectA, jSONArray);
            com.apm.insight.l.f.a(aVar.f5521c);
        } else if (c6 == 2) {
            a(jCurrentTimeMillis, jCurrentTimeMillis, jSONObjectA, jSONArray);
        }
        try {
            ArrayList<a> arrayListC = c("");
            if (arrayListC.size() <= 6) {
                return;
            }
            for (a aVar2 : arrayListC) {
                if (a.a(aVar2, jCurrentTimeMillis)) {
                    a.d(aVar2);
                }
            }
        } catch (Throwable th) {
            com.apm.insight.c.a();
            j.a(th, "NPTH_CATCH");
        }
    }

    private ArrayList<a> c(final String str) {
        File[] fileArrListFiles = this.f5512b.listFiles(new FilenameFilter() { // from class: com.apm.insight.runtime.o.1
            @Override // java.io.FilenameFilter
            public final boolean accept(File file, String str2) {
                return str2.endsWith(str) && Pattern.compile("^\\d{1,13}-\\d{1,13}.*").matcher(str2).matches();
            }
        });
        ArrayList<a> arrayList = new ArrayList<>();
        if (fileArrListFiles == null) {
            return arrayList;
        }
        com.apm.insight.a.a((Object) ("foundRuntimeContextFiles " + fileArrListFiles.length));
        byte b10 = 0;
        a aVar = null;
        for (File file : fileArrListFiles) {
            try {
                a aVar2 = new a(file, b10);
                arrayList.add(aVar2);
                if (this.f5516f == null && ".ctx".equals(str) && (aVar == null || aVar2.f5520b >= aVar.f5520b)) {
                    aVar = aVar2;
                }
            } catch (Throwable th) {
                com.apm.insight.c.a();
                j.a(th, "NPTH_CATCH");
            }
        }
        if (this.f5516f == null && aVar != null) {
            this.f5516f = aVar;
        }
        return arrayList;
    }

    public final void b(String str) {
        try {
            com.apm.insight.l.f.a(this.f5514d, str, false);
        } catch (Throwable unused) {
        }
    }

    @Nullable
    public final JSONArray b(long j10) {
        File file;
        String strA;
        Iterator<a> it = c(".allData").iterator();
        while (true) {
            if (!it.hasNext()) {
                file = null;
                break;
            }
            a next = it.next();
            if (j10 >= next.f5519a && j10 <= next.f5520b) {
                file = next.f5521c;
                break;
            }
        }
        if (file == null) {
            a aVar = null;
            for (a aVar2 : c(".allData")) {
                if (aVar == null || Math.abs(aVar.f5520b - j10) > Math.abs(aVar2.f5520b - j10)) {
                    aVar = aVar2;
                }
            }
            file = aVar == null ? null : aVar.f5521c;
        }
        if (file == null) {
            return null;
        }
        try {
            strA = com.apm.insight.l.f.a(file.getAbsolutePath(), "\n");
            try {
                return new JSONArray(strA);
            } catch (Throwable th) {
                th = th;
                com.apm.insight.c.a();
                j.a(new IOException("content :".concat(String.valueOf(strA)), th), "NPTH_CATCH");
                return null;
            }
        } catch (Throwable th2) {
            th = th2;
            strA = null;
        }
    }

    private void a(long j10, long j11, JSONObject jSONObject, JSONArray jSONArray) {
        File file = new File(this.f5512b, j10 + "-" + j11 + ".ctx");
        File file2 = new File(this.f5512b, j10 + "-" + j11 + ".allData");
        try {
            com.apm.insight.l.f.a(file, jSONObject);
            com.apm.insight.l.f.a(file2, jSONArray);
            this.f5516f = new a(file, (byte) 0);
        } catch (IOException e10) {
            com.apm.insight.c.a();
            j.a(e10, "NPTH_CATCH");
        }
    }

    public final void a(String str) {
        try {
            com.apm.insight.l.f.a(this.f5513c, str, false);
        } catch (Throwable unused) {
        }
    }

    @Nullable
    public final JSONObject a(long j10) {
        JSONObject jSONObject;
        File file;
        boolean z10;
        String strA;
        Iterator<a> it = c(".ctx").iterator();
        while (true) {
            jSONObject = null;
            if (!it.hasNext()) {
                file = null;
                break;
            }
            a next = it.next();
            if (j10 >= next.f5519a && j10 <= next.f5520b) {
                file = next.f5521c;
                break;
            }
        }
        if (file == null) {
            a aVar = null;
            for (a aVar2 : c(".ctx")) {
                if (aVar == null || Math.abs(aVar.f5520b - j10) > Math.abs(aVar2.f5520b - j10)) {
                    aVar = aVar2;
                }
            }
            file = aVar == null ? null : aVar.f5521c;
            z10 = true;
        } else {
            z10 = false;
        }
        if (file != null) {
            try {
                strA = com.apm.insight.l.f.a(file.getAbsolutePath(), "\n");
                try {
                    jSONObject = new JSONObject(strA);
                } catch (Throwable th) {
                    th = th;
                    com.apm.insight.c.a();
                    j.a(new IOException("content :".concat(String.valueOf(strA)), th), "NPTH_CATCH");
                }
            } catch (Throwable th2) {
                th = th2;
                strA = null;
            }
        }
        if (jSONObject != null && z10) {
            try {
                jSONObject.put("unauthentic_version", 1);
            } catch (JSONException e10) {
                com.apm.insight.c.a();
                j.a(e10, "NPTH_CATCH");
            }
        }
        return jSONObject;
    }
}
