// 
// Decompiled by Procyon v0.6.0
// 

package io.sentry.cache;

import java.io.Reader;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import org.jetbrains.annotations.Nullable;
import io.sentry.JsonDeserializer;
import java.io.OutputStream;
import java.io.Writer;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.io.File;
import io.sentry.SentryLevel;
import org.jetbrains.annotations.NotNull;
import io.sentry.SentryOptions;
import java.nio.charset.Charset;

final class CacheUtils
{
    private static final Charset UTF_8;
    
    static <T> void store(@NotNull final SentryOptions options, @NotNull final T entity, @NotNull final String dirName, @NotNull final String fileName) {
        final File cacheDir = ensureCacheDir(options, dirName);
        if (cacheDir == null) {
            options.getLogger().log(SentryLevel.INFO, "Cache dir is not set, cannot store in scope cache", new Object[0]);
            return;
        }
        final File file = new File(cacheDir, fileName);
        try (final OutputStream outputStream = new FileOutputStream(file);
             final Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, CacheUtils.UTF_8))) {
            options.getSerializer().serialize(entity, writer);
        }
        catch (final Throwable e) {
            options.getLogger().log(SentryLevel.ERROR, e, "Error persisting entity: %s", fileName);
        }
    }
    
    static void delete(@NotNull final SentryOptions options, @NotNull final String dirName, @NotNull final String fileName) {
        final File cacheDir = ensureCacheDir(options, dirName);
        if (cacheDir == null) {
            options.getLogger().log(SentryLevel.INFO, "Cache dir is not set, cannot delete from scope cache", new Object[0]);
            return;
        }
        final File file = new File(cacheDir, fileName);
        options.getLogger().log(SentryLevel.DEBUG, "Deleting %s from scope cache", fileName);
        if (!file.delete()) {
            options.getLogger().log(SentryLevel.INFO, "Failed to delete: %s", file.getAbsolutePath());
        }
    }
    
    @Nullable
    static <T, R> T read(@NotNull final SentryOptions options, @NotNull final String dirName, @NotNull final String fileName, @NotNull final Class<T> clazz, @Nullable final JsonDeserializer<R> elementDeserializer) {
        final File cacheDir = ensureCacheDir(options, dirName);
        if (cacheDir == null) {
            options.getLogger().log(SentryLevel.INFO, "Cache dir is not set, cannot read from scope cache", new Object[0]);
            return null;
        }
        final File file = new File(cacheDir, fileName);
        if (file.exists()) {
            try (final Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), CacheUtils.UTF_8))) {
                if (elementDeserializer == null) {
                    final T deserialize = options.getSerializer().deserialize(reader, clazz);
                    reader.close();
                    return deserialize;
                }
                return options.getSerializer().deserializeCollection(reader, clazz, elementDeserializer);
            }
            catch (final Throwable e) {
                options.getLogger().log(SentryLevel.ERROR, e, "Error reading entity from scope cache: %s", fileName);
                return null;
            }
        }
        options.getLogger().log(SentryLevel.DEBUG, "No entry stored for %s", fileName);
        return null;
    }
    
    @Nullable
    static File ensureCacheDir(@NotNull final SentryOptions options, @NotNull final String cacheDirName) {
        final String cacheDir = options.getCacheDirPath();
        if (cacheDir == null) {
            return null;
        }
        final File dir = new File(cacheDir, cacheDirName);
        dir.mkdirs();
        return dir;
    }
    
    static {
        UTF_8 = Charset.forName("UTF-8");
    }
}
