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

package io.sentry;

import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.Date;
import io.sentry.cache.IEnvelopeCache;
import java.io.File;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import io.sentry.cache.EnvelopeCache;
import org.jetbrains.annotations.NotNull;
import java.nio.charset.Charset;

final class PreviousSessionFinalizer implements Runnable
{
    private static final Charset UTF_8;
    @NotNull
    private final SentryOptions options;
    @NotNull
    private final IScopes scopes;
    
    PreviousSessionFinalizer(@NotNull final SentryOptions options, @NotNull final IScopes scopes) {
        this.options = options;
        this.scopes = scopes;
    }
    
    @Override
    public void run() {
        final String cacheDirPath = this.options.getCacheDirPath();
        if (cacheDirPath == null) {
            this.options.getLogger().log(SentryLevel.INFO, "Cache dir is not set, not finalizing the previous session.", new Object[0]);
            return;
        }
        if (!this.options.isEnableAutoSessionTracking()) {
            this.options.getLogger().log(SentryLevel.DEBUG, "Session tracking is disabled, bailing from previous session finalizer.", new Object[0]);
            return;
        }
        final IEnvelopeCache cache = this.options.getEnvelopeDiskCache();
        if (cache instanceof EnvelopeCache && !((EnvelopeCache)cache).waitPreviousSessionFlush()) {
            this.options.getLogger().log(SentryLevel.WARNING, "Timed out waiting to flush previous session to its own file in session finalizer.", new Object[0]);
            return;
        }
        final File previousSessionFile = EnvelopeCache.getPreviousSessionFile(cacheDirPath);
        final ISerializer serializer = this.options.getSerializer();
        if (previousSessionFile.exists()) {
            this.options.getLogger().log(SentryLevel.WARNING, "Current session is not ended, we'd need to end it.", new Object[0]);
            try (final Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(previousSessionFile), PreviousSessionFinalizer.UTF_8))) {
                final Session session = serializer.deserialize(reader, Session.class);
                if (session == null) {
                    this.options.getLogger().log(SentryLevel.ERROR, "Stream from path %s resulted in a null envelope.", previousSessionFile.getAbsolutePath());
                }
                else {
                    Date timestamp = null;
                    final File crashMarkerFile = new File(this.options.getCacheDirPath(), ".sentry-native/last_crash");
                    if (crashMarkerFile.exists()) {
                        this.options.getLogger().log(SentryLevel.INFO, "Crash marker file exists, last Session is gonna be Crashed.", new Object[0]);
                        timestamp = this.getTimestampFromCrashMarkerFile(crashMarkerFile);
                        if (!crashMarkerFile.delete()) {
                            this.options.getLogger().log(SentryLevel.ERROR, "Failed to delete the crash marker file. %s.", crashMarkerFile.getAbsolutePath());
                        }
                        session.update(Session.State.Crashed, null, true);
                    }
                    if (session.getAbnormalMechanism() == null) {
                        session.end(timestamp);
                    }
                    final SentryEnvelope fromSession = SentryEnvelope.from(serializer, session, this.options.getSdkVersion());
                    this.scopes.captureEnvelope(fromSession);
                }
            }
            catch (final Throwable e) {
                this.options.getLogger().log(SentryLevel.ERROR, "Error processing previous session.", e);
            }
            if (!previousSessionFile.delete()) {
                this.options.getLogger().log(SentryLevel.WARNING, "Failed to delete the previous session file.", new Object[0]);
            }
        }
    }
    
    @Nullable
    private Date getTimestampFromCrashMarkerFile(@NotNull final File markerFile) {
        try (final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(markerFile), PreviousSessionFinalizer.UTF_8))) {
            final String timestamp = reader.readLine();
            this.options.getLogger().log(SentryLevel.DEBUG, "Crash marker file has %s timestamp.", timestamp);
            return DateUtils.getDateTime(timestamp);
        }
        catch (final IOException e) {
            this.options.getLogger().log(SentryLevel.ERROR, "Error reading the crash marker file.", e);
        }
        catch (final IllegalArgumentException e2) {
            this.options.getLogger().log(SentryLevel.ERROR, e2, "Error converting the crash timestamp.", new Object[0]);
        }
        return null;
    }
    
    static {
        UTF_8 = Charset.forName("UTF-8");
    }
}
