package yg;

import ch.j;
import com.adobe.marketing.mobile.internal.util.i;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import wg.i0;
import wg.t;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes4.dex */
public class a {

    /* renamed from: a, reason: collision with root package name */
    private final String f52732a;

    /* JADX INFO: Access modifiers changed from: package-private */
    public a(String str) {
        this.f52732a = str;
    }

    private boolean a(String str, String str2) {
        return (j.a(str) || j.a(str2)) ? false : true;
    }

    private boolean d(xg.a aVar, String str, String str2) {
        if (aVar != null && !j.a(str2)) {
            HashMap hashMap = new HashMap();
            hashMap.put("pathToFile", str);
            Date b10 = aVar.b().b();
            if (b10 != null) {
                hashMap.put("expiryInMillis", String.valueOf(b10.getTime()));
            }
            if (aVar.c() != null) {
                hashMap.putAll(aVar.c());
            }
            try {
                return com.adobe.marketing.mobile.internal.util.c.h(new File(str2), new ByteArrayInputStream(new JSONObject(hashMap).toString().getBytes(StandardCharsets.UTF_8)), false);
            } catch (Exception e10) {
                t.a("Services", "CacheFileManager", "Cannot create cache metadata %s", e10);
            }
        }
        return false;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public File b(String str) {
        if (j.a(str)) {
            return null;
        }
        File c10 = i0.f().e().c();
        if (!com.adobe.marketing.mobile.internal.util.c.e(c10)) {
            t.a("Services", "CacheFileManager", "App cache directory is not writable.", new Object[0]);
            return null;
        }
        File file = new File(c10, this.f52732a + File.separator + str);
        if (file.exists() || file.mkdirs()) {
            return file;
        }
        t.a("Services", "CacheFileManager", "Cannot create cache bucket.", new Object[0]);
        return null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean c(String str, String str2, xg.a aVar) {
        if (!a(str, str2)) {
            return false;
        }
        String g10 = g(str, str2);
        if (g10 == null) {
            t.a("Services", "CacheFileManager", "Entry location for cache name: [%s], cache key [%s] is null.", str, str2);
            return false;
        }
        if (!com.adobe.marketing.mobile.internal.util.c.h(new File(g10), aVar.a(), false)) {
            t.a("Services", "CacheFileManager", "Failed to save cache file for cache name: [%s], cache key [%s].", str, str2);
            return false;
        }
        if (d(aVar, g10, i(str, str2))) {
            return true;
        }
        t.a("Services", "CacheFileManager", "Failed to save metadata forcache name: [%s], cache key [%s].", str, str2);
        com.adobe.marketing.mobile.internal.util.c.b(new File(g10), true);
        return false;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean e(String str, String str2) {
        if (!a(str, str2)) {
            return false;
        }
        File f10 = f(str, str2);
        if (f10 == null) {
            t.a("Services", "CacheFileManager", "Cannot delete cache file. No file to delete.", new Object[0]);
            return true;
        }
        if (!com.adobe.marketing.mobile.internal.util.c.b(f10, true)) {
            t.a("Services", "CacheFileManager", "Failed to delete cache file for cache name [%s], key: [%s]", str, str2);
            return false;
        }
        String i10 = i(str, str2);
        if (i10 != null) {
            t.a("Services", "CacheFileManager", "Failed to delete cache metadata file for cache name [%s], key: [%s]", str, str2);
            com.adobe.marketing.mobile.internal.util.c.b(new File(i10), true);
        }
        return true;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public File f(String str, String str2) {
        String g10 = g(str, str2);
        if (g10 == null) {
            return null;
        }
        File file = new File(g10);
        if (file.exists()) {
            return file;
        }
        return null;
    }

    String g(String str, String str2) {
        if (!a(str, str2)) {
            return null;
        }
        String c10 = i.c(str2);
        StringBuilder sb2 = new StringBuilder();
        sb2.append(i0.f().e().c().getPath());
        String str3 = File.separator;
        sb2.append(str3);
        sb2.append(this.f52732a);
        sb2.append(str3);
        sb2.append(str);
        sb2.append(str3);
        sb2.append(c10);
        return sb2.toString();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Map h(String str, String str2) {
        if (!a(str, str2)) {
            return null;
        }
        String i10 = i(str, str2);
        if (i10 == null) {
            t.a("Services", "CacheFileManager", "Metadata location forcache name: [%s], cache key [%s] is null.", str, str2);
            return null;
        }
        String g10 = com.adobe.marketing.mobile.internal.util.c.g(new File(i10));
        if (g10 == null) {
            t.a("Services", "CacheFileManager", "Metadata stored forcache name: [%s], cache key [%s] is null.", str, str2);
            return null;
        }
        try {
            HashMap hashMap = new HashMap();
            JSONObject jSONObject = new JSONObject(new JSONTokener(g10));
            Iterator<String> keys = jSONObject.keys();
            while (keys.hasNext()) {
                String next = keys.next();
                hashMap.put(next, jSONObject.optString(next));
            }
            return hashMap;
        } catch (JSONException e10) {
            t.a("Services", "CacheFileManager", "Cannot create cache metadata forcache name: [%s], cache key: [%s] due to %s", str, str2, e10.getMessage());
            return null;
        }
    }

    String i(String str, String str2) {
        if (!a(str, str2)) {
            return null;
        }
        return g(str, str2) + "_metadata.txt";
    }
}
