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

package io.sentry;

import java.io.IOException;
import java.net.URI;
import org.jetbrains.annotations.TestOnly;
import io.sentry.util.Platform;
import java.net.HttpURLConnection;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;
import java.util.concurrent.RejectedExecutionException;
import io.sentry.util.IntegrationUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.ApiStatus;
import java.io.Closeable;

@ApiStatus.Internal
public final class SpotlightIntegration implements Integration, SentryOptions.BeforeEnvelopeCallback, Closeable
{
    @Nullable
    private SentryOptions options;
    @NotNull
    private ILogger logger;
    @NotNull
    private ISentryExecutorService executorService;
    
    public SpotlightIntegration() {
        this.logger = NoOpLogger.getInstance();
        this.executorService = NoOpSentryExecutorService.getInstance();
    }
    
    @Override
    public void register(@NotNull final IScopes scopes, @NotNull final SentryOptions options) {
        this.options = options;
        this.logger = options.getLogger();
        if (options.getBeforeEnvelopeCallback() == null && options.isEnableSpotlight()) {
            this.executorService = new SentryExecutorService(options);
            options.setBeforeEnvelopeCallback(this);
            this.logger.log(SentryLevel.DEBUG, "SpotlightIntegration enabled.", new Object[0]);
            IntegrationUtils.addIntegrationToSdkVersion("Spotlight");
        }
        else {
            this.logger.log(SentryLevel.DEBUG, "SpotlightIntegration is not enabled. BeforeEnvelopeCallback is already set or spotlight is not enabled.", new Object[0]);
        }
    }
    
    @Override
    public void execute(@NotNull final SentryEnvelope envelope, @Nullable final Hint hint) {
        try {
            this.executorService.submit(() -> this.sendEnvelope(envelope));
        }
        catch (final RejectedExecutionException e) {
            this.logger.log(SentryLevel.WARNING, "Spotlight envelope submission rejected.", e);
        }
    }
    
    private void sendEnvelope(@NotNull final SentryEnvelope envelope) {
        try {
            if (this.options == null) {
                throw new IllegalArgumentException("SentryOptions are required to send envelopes.");
            }
            final String spotlightConnectionUrl = this.getSpotlightConnectionUrl();
            final HttpURLConnection connection = this.createConnection(spotlightConnectionUrl);
            try (final OutputStream outputStream = connection.getOutputStream();
                 final GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) {
                this.options.getSerializer().serialize(envelope, gzip);
            }
            catch (final Throwable e) {
                this.logger.log(SentryLevel.ERROR, "An exception occurred while submitting the envelope to the Sentry server.", e);
            }
            finally {
                final int responseCode = connection.getResponseCode();
                this.logger.log(SentryLevel.DEBUG, "Envelope sent to spotlight: %d", responseCode);
                this.closeAndDisconnect(connection);
            }
        }
        catch (final Exception e2) {
            this.logger.log(SentryLevel.ERROR, "An exception occurred while creating the connection to spotlight.", e2);
        }
    }
    
    @TestOnly
    public String getSpotlightConnectionUrl() {
        if (this.options != null && this.options.getSpotlightConnectionUrl() != null) {
            return this.options.getSpotlightConnectionUrl();
        }
        if (Platform.isAndroid()) {
            return "http://10.0.2.2:8969/stream";
        }
        return "http://localhost:8969/stream";
    }
    
    @NotNull
    private HttpURLConnection createConnection(@NotNull final String url) throws Exception {
        final HttpURLConnection connection = (HttpURLConnection)URI.create(url).toURL().openConnection();
        connection.setReadTimeout(1000);
        connection.setConnectTimeout(1000);
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Encoding", "gzip");
        connection.setRequestProperty("Content-Type", "application/x-sentry-envelope");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Connection", "close");
        connection.connect();
        return connection;
    }
    
    private void closeAndDisconnect(@NotNull final HttpURLConnection connection) {
        try {
            connection.getInputStream().close();
        }
        catch (final IOException ex) {}
        finally {
            connection.disconnect();
        }
    }
    
    @Override
    public void close() throws IOException {
        this.executorService.close(0L);
        if (this.options != null && this.options.getBeforeEnvelopeCallback() == this) {
            this.options.setBeforeEnvelopeCallback(null);
        }
    }
}
