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

package io.sentry.instrumentation.file;

import io.sentry.util.StringUtils;
import java.io.Closeable;
import java.io.IOException;
import io.sentry.SentryIntegrationPackageStorage;
import io.sentry.util.Platform;
import io.sentry.IScopes;
import io.sentry.SentryStackTraceFactory;
import io.sentry.SpanStatus;
import org.jetbrains.annotations.NotNull;
import io.sentry.SentryOptions;
import java.io.File;
import org.jetbrains.annotations.Nullable;
import io.sentry.ISpan;

final class FileIOSpanManager
{
    @Nullable
    private final ISpan currentSpan;
    @Nullable
    private final File file;
    @NotNull
    private final SentryOptions options;
    @NotNull
    private SpanStatus spanStatus;
    private long byteCount;
    @NotNull
    private final SentryStackTraceFactory stackTraceFactory;
    
    @Nullable
    static ISpan startSpan(@NotNull final IScopes scopes, @NotNull final String op) {
        final ISpan parent = Platform.isAndroid() ? scopes.getTransaction() : scopes.getSpan();
        return (parent != null) ? parent.startChild(op) : null;
    }
    
    FileIOSpanManager(@Nullable final ISpan currentSpan, @Nullable final File file, @NotNull final SentryOptions options) {
        this.spanStatus = SpanStatus.OK;
        this.currentSpan = currentSpan;
        this.file = file;
        this.options = options;
        this.stackTraceFactory = new SentryStackTraceFactory(options);
        SentryIntegrationPackageStorage.getInstance().addIntegration("FileIO");
    }
    
     <T> T performIO(@NotNull final FileIOCallable<T> operation) throws IOException {
        try {
            final T result = operation.call();
            if (result instanceof Integer) {
                final int resUnboxed = (int)result;
                if (resUnboxed != -1) {
                    this.byteCount += resUnboxed;
                }
            }
            else if (result instanceof Long) {
                final long resUnboxed2 = (long)result;
                if (resUnboxed2 != -1L) {
                    this.byteCount += resUnboxed2;
                }
            }
            return result;
        }
        catch (final IOException exception) {
            this.spanStatus = SpanStatus.INTERNAL_ERROR;
            if (this.currentSpan != null) {
                this.currentSpan.setThrowable(exception);
            }
            throw exception;
        }
    }
    
    void finish(@NotNull final Closeable delegate) throws IOException {
        try {
            delegate.close();
        }
        catch (final IOException exception) {
            this.spanStatus = SpanStatus.INTERNAL_ERROR;
            if (this.currentSpan != null) {
                this.currentSpan.setThrowable(exception);
            }
            throw exception;
        }
        finally {
            this.finishSpan();
        }
    }
    
    private void finishSpan() {
        if (this.currentSpan != null) {
            final String byteCountToString = StringUtils.byteCountToString(this.byteCount);
            if (this.file != null) {
                final String description = this.getDescription(this.file);
                this.currentSpan.setDescription(description);
                if (this.options.isSendDefaultPii()) {
                    this.currentSpan.setData("file.path", this.file.getAbsolutePath());
                }
            }
            else {
                this.currentSpan.setDescription(byteCountToString);
            }
            this.currentSpan.setData("file.size", this.byteCount);
            final boolean isMainThread = this.options.getThreadChecker().isMainThread();
            this.currentSpan.setData("blocked_main_thread", isMainThread);
            if (isMainThread) {
                this.currentSpan.setData("call_stack", this.stackTraceFactory.getInAppCallStack());
            }
            this.currentSpan.finish(this.spanStatus);
        }
    }
    
    @NotNull
    private String getDescription(@NotNull final File file) {
        final String byteCountToString = StringUtils.byteCountToString(this.byteCount);
        if (this.options.isSendDefaultPii()) {
            return file.getName() + " (" + byteCountToString + ")";
        }
        final int lastDotIndex = file.getName().lastIndexOf(46);
        if (lastDotIndex > 0 && lastDotIndex < file.getName().length() - 1) {
            final String fileExtension = file.getName().substring(lastDotIndex);
            return "***" + fileExtension + " (" + byteCountToString + ")";
        }
        return "*** (" + byteCountToString + ")";
    }
    
    @FunctionalInterface
    interface FileIOCallable<T>
    {
        T call() throws IOException;
    }
}
