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

package io.sentry;

import java.io.File;
import io.sentry.transport.RateLimiter;
import java.util.concurrent.RejectedExecutionException;
import java.io.IOException;
import io.sentry.util.IntegrationUtils;
import io.sentry.util.Objects;
import io.sentry.util.AutoClosableReentrantLock;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import java.io.Closeable;

public final class SendCachedEnvelopeFireAndForgetIntegration implements Integration, IConnectionStatusProvider.IConnectionStatusObserver, Closeable
{
    @NotNull
    private final SendFireAndForgetFactory factory;
    @Nullable
    private IConnectionStatusProvider connectionStatusProvider;
    @Nullable
    private IScopes scopes;
    @Nullable
    private SentryOptions options;
    @Nullable
    private SendFireAndForget sender;
    private final AtomicBoolean isInitialized;
    private final AtomicBoolean isClosed;
    @NotNull
    private final AutoClosableReentrantLock lock;
    
    public SendCachedEnvelopeFireAndForgetIntegration(@NotNull final SendFireAndForgetFactory factory) {
        this.isInitialized = new AtomicBoolean(false);
        this.isClosed = new AtomicBoolean(false);
        this.lock = new AutoClosableReentrantLock();
        this.factory = Objects.requireNonNull(factory, "SendFireAndForgetFactory is required");
    }
    
    @Override
    public void register(@NotNull final IScopes scopes, @NotNull final SentryOptions options) {
        this.scopes = Objects.requireNonNull(scopes, "Scopes are required");
        this.options = Objects.requireNonNull(options, "SentryOptions is required");
        final String cachedDir = options.getCacheDirPath();
        if (!this.factory.hasValidPath(cachedDir, options.getLogger())) {
            options.getLogger().log(SentryLevel.ERROR, "No cache dir path is defined in options.", new Object[0]);
            return;
        }
        options.getLogger().log(SentryLevel.DEBUG, "SendCachedEventFireAndForgetIntegration installed.", new Object[0]);
        IntegrationUtils.addIntegrationToSdkVersion("SendCachedEnvelopeFireAndForget");
        this.sendCachedEnvelopes(scopes, options);
    }
    
    @Override
    public void close() throws IOException {
        this.isClosed.set(true);
        if (this.connectionStatusProvider != null) {
            this.connectionStatusProvider.removeConnectionStatusObserver(this);
        }
    }
    
    @Override
    public void onConnectionStatusChanged(@NotNull final IConnectionStatusProvider.ConnectionStatus status) {
        if (this.scopes != null && this.options != null && status != IConnectionStatusProvider.ConnectionStatus.DISCONNECTED) {
            this.sendCachedEnvelopes(this.scopes, this.options);
        }
    }
    
    private void sendCachedEnvelopes(@NotNull final IScopes scopes, @NotNull final SentryOptions options) {
        try (final ISentryLifecycleToken ignored = this.lock.acquire()) {
            options.getExecutorService().submit(() -> {
                try {
                    if (this.isClosed.get()) {
                        options.getLogger().log(SentryLevel.INFO, "SendCachedEnvelopeFireAndForgetIntegration, not trying to send after closing.", new Object[0]);
                    }
                    else {
                        if (!this.isInitialized.getAndSet(true)) {
                            (this.connectionStatusProvider = options.getConnectionStatusProvider()).addConnectionStatusObserver(this);
                            this.sender = this.factory.create(scopes, options);
                        }
                        if (this.connectionStatusProvider != null && this.connectionStatusProvider.getConnectionStatus() == IConnectionStatusProvider.ConnectionStatus.DISCONNECTED) {
                            options.getLogger().log(SentryLevel.INFO, "SendCachedEnvelopeFireAndForgetIntegration, no connection.", new Object[0]);
                        }
                        else {
                            final RateLimiter rateLimiter = scopes.getRateLimiter();
                            if (rateLimiter != null && rateLimiter.isActiveForCategory(DataCategory.All)) {
                                options.getLogger().log(SentryLevel.INFO, "SendCachedEnvelopeFireAndForgetIntegration, rate limiting active.", new Object[0]);
                            }
                            else if (this.sender == null) {
                                options.getLogger().log(SentryLevel.ERROR, "SendFireAndForget factory is null.", new Object[0]);
                            }
                            else {
                                this.sender.send();
                            }
                        }
                    }
                }
                catch (final Throwable e3) {
                    options.getLogger().log(SentryLevel.ERROR, "Failed trying to send cached events.", e3);
                }
                return;
            });
        }
        catch (final RejectedExecutionException e) {
            options.getLogger().log(SentryLevel.ERROR, "Failed to call the executor. Cached events will not be sent. Did you call Sentry.close()?", e);
        }
        catch (final Throwable e2) {
            options.getLogger().log(SentryLevel.ERROR, "Failed to call the executor. Cached events will not be sent", e2);
        }
    }
    
    public interface SendFireAndForgetFactory
    {
        @Nullable
        SendFireAndForget create(@NotNull final IScopes p0, @NotNull final SentryOptions p1);
        
        default boolean hasValidPath(@Nullable final String dirPath, @NotNull final ILogger logger) {
            if (dirPath == null || dirPath.isEmpty()) {
                logger.log(SentryLevel.INFO, "No cached dir path is defined in options.", new Object[0]);
                return false;
            }
            return true;
        }
        
        @NotNull
        default SendFireAndForget processDir(@NotNull final DirectoryProcessor directoryProcessor, @NotNull final String dirPath, @NotNull final ILogger logger) {
            final File dirFile = new File(dirPath);
            return () -> {
                logger.log(SentryLevel.DEBUG, "Started processing cached files from %s", dirPath);
                directoryProcessor.processDirectory(dirFile);
                logger.log(SentryLevel.DEBUG, "Finished processing cached files from %s", dirPath);
            };
        }
    }
    
    public interface SendFireAndForget
    {
        void send();
    }
    
    public interface SendFireAndForgetDirPath
    {
        @Nullable
        String getDirPath();
    }
}
