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

package io.sentry;

import org.jetbrains.annotations.TestOnly;
import java.io.File;
import org.jetbrains.annotations.NotNull;
import io.sentry.util.AutoClosableReentrantLock;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public final class SentryCrashLastRunState
{
    private static final SentryCrashLastRunState INSTANCE;
    private boolean readCrashedLastRun;
    @Nullable
    private Boolean crashedLastRun;
    @NotNull
    private final AutoClosableReentrantLock crashedLastRunLock;
    
    private SentryCrashLastRunState() {
        this.crashedLastRunLock = new AutoClosableReentrantLock();
    }
    
    public static SentryCrashLastRunState getInstance() {
        return SentryCrashLastRunState.INSTANCE;
    }
    
    @Nullable
    public Boolean isCrashedLastRun(@Nullable final String cacheDirPath, final boolean deleteFile) {
        try (final ISentryLifecycleToken ignored = this.crashedLastRunLock.acquire()) {
            if (this.readCrashedLastRun) {
                final Boolean crashedLastRun = this.crashedLastRun;
                if (ignored != null) {
                    ignored.close();
                }
                return crashedLastRun;
            }
            if (cacheDirPath == null) {
                final Boolean b = null;
                if (ignored != null) {
                    ignored.close();
                }
                return b;
            }
            this.readCrashedLastRun = true;
            final File javaMarker = new File(cacheDirPath, "last_crash");
            final File nativeMarker = new File(cacheDirPath, ".sentry-native/last_crash");
            boolean exists = false;
            try {
                if (javaMarker.exists()) {
                    exists = true;
                    javaMarker.delete();
                }
                else if (nativeMarker.exists()) {
                    exists = true;
                    if (deleteFile) {
                        nativeMarker.delete();
                    }
                }
            }
            catch (final Throwable t) {}
            this.crashedLastRun = exists;
        }
        return this.crashedLastRun;
    }
    
    public void setCrashedLastRun(final boolean crashedLastRun) {
        try (final ISentryLifecycleToken ignored = this.crashedLastRunLock.acquire()) {
            if (!this.readCrashedLastRun) {
                this.crashedLastRun = crashedLastRun;
                this.readCrashedLastRun = true;
            }
        }
    }
    
    @TestOnly
    public void reset() {
        try (final ISentryLifecycleToken ignored = this.crashedLastRunLock.acquire()) {
            this.readCrashedLastRun = false;
            this.crashedLastRun = null;
        }
    }
    
    static {
        INSTANCE = new SentryCrashLastRunState();
    }
}
