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

package io.sentry;

import io.sentry.hints.Retryable;
import java.io.IOException;
import java.io.FileNotFoundException;
import io.sentry.util.HintUtils;
import io.sentry.hints.Flushable;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.File;
import io.sentry.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public final class EnvelopeSender extends DirectoryProcessor implements IEnvelopeSender
{
    @NotNull
    private final IScopes scopes;
    @NotNull
    private final ISerializer serializer;
    @NotNull
    private final ILogger logger;
    
    public EnvelopeSender(@NotNull final IScopes scopes, @NotNull final ISerializer serializer, @NotNull final ILogger logger, final long flushTimeoutMillis, final int maxQueueSize) {
        super(scopes, logger, flushTimeoutMillis, maxQueueSize);
        this.scopes = Objects.requireNonNull(scopes, "Scopes are required.");
        this.serializer = Objects.requireNonNull(serializer, "Serializer is required.");
        this.logger = Objects.requireNonNull(logger, "Logger is required.");
    }
    
    @Override
    protected void processFile(@NotNull final File file, @NotNull final Hint hint) {
        if (!file.isFile()) {
            this.logger.log(SentryLevel.DEBUG, "'%s' is not a file.", file.getAbsolutePath());
            return;
        }
        if (!this.isRelevantFileName(file.getName())) {
            this.logger.log(SentryLevel.DEBUG, "File '%s' doesn't match extension expected.", file.getAbsolutePath());
            return;
        }
        if (!file.getParentFile().canWrite()) {
            this.logger.log(SentryLevel.WARNING, "File '%s' cannot be deleted so it will not be processed.", file.getAbsolutePath());
            return;
        }
        try (final InputStream is = new BufferedInputStream(new FileInputStream(file))) {
            final SentryEnvelope envelope = this.serializer.deserializeEnvelope(is);
            if (envelope == null) {
                this.logger.log(SentryLevel.ERROR, "Failed to deserialize cached envelope %s", file.getAbsolutePath());
            }
            else {
                this.scopes.captureEnvelope(envelope, hint);
            }
            HintUtils.runIfHasTypeLogIfNot(hint, Flushable.class, this.logger, flushable -> {
                if (!flushable.waitFlush()) {
                    this.logger.log(SentryLevel.WARNING, "Timed out waiting for envelope submission.", new Object[0]);
                }
                return;
            });
        }
        catch (final FileNotFoundException e) {
            this.logger.log(SentryLevel.ERROR, e, "File '%s' cannot be found.", file.getAbsolutePath());
        }
        catch (final IOException e2) {
            this.logger.log(SentryLevel.ERROR, e2, "I/O on file '%s' failed.", file.getAbsolutePath());
        }
        catch (final Throwable e3) {
            this.logger.log(SentryLevel.ERROR, e3, "Failed to capture cached envelope %s", file.getAbsolutePath());
            final FileNotFoundException e;
            HintUtils.runIfHasTypeLogIfNot(hint, Retryable.class, this.logger, retryable -> {
                retryable.setRetry(false);
                this.logger.log(SentryLevel.INFO, e, "File '%s' won't retry.", file.getAbsolutePath());
            });
        }
        finally {
            HintUtils.runIfHasTypeLogIfNot(hint, Retryable.class, this.logger, retryable -> {
                if (!retryable.isRetry()) {
                    this.safeDelete(file, "after trying to capture it");
                    this.logger.log(SentryLevel.DEBUG, "Deleted file %s.", file.getAbsolutePath());
                }
                else {
                    this.logger.log(SentryLevel.INFO, "File not deleted since retry was marked. %s.", file.getAbsolutePath());
                }
            });
        }
    }
    
    @Override
    protected boolean isRelevantFileName(@NotNull final String fileName) {
        return fileName.endsWith(".envelope");
    }
    
    @Override
    public void processEnvelopeFile(@NotNull final String path, @NotNull final Hint hint) {
        Objects.requireNonNull(path, "Path is required.");
        this.processFile(new File(path), hint);
    }
    
    private void safeDelete(@NotNull final File file, @NotNull final String errorMessageSuffix) {
        try {
            if (!file.delete()) {
                this.logger.log(SentryLevel.ERROR, "Failed to delete '%s' %s", file.getAbsolutePath(), errorMessageSuffix);
            }
        }
        catch (final Throwable e) {
            this.logger.log(SentryLevel.ERROR, e, "Failed to delete '%s' %s", file.getAbsolutePath(), errorMessageSuffix);
        }
    }
}
