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

package io.sentry;

import org.jetbrains.annotations.ApiStatus;
import java.util.Iterator;
import java.util.Collection;
import java.util.ArrayList;
import java.util.HashMap;
import org.jetbrains.annotations.Nullable;
import io.sentry.util.AutoClosableReentrantLock;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import java.util.Map;

public final class Hint
{
    @NotNull
    private static final Map<String, Class<?>> PRIMITIVE_MAPPINGS;
    @NotNull
    private final Map<String, Object> internalStorage;
    @NotNull
    private final List<Attachment> attachments;
    @NotNull
    private final AutoClosableReentrantLock lock;
    @Nullable
    private Attachment screenshot;
    @Nullable
    private Attachment viewHierarchy;
    @Nullable
    private Attachment threadDump;
    @Nullable
    private ReplayRecording replayRecording;
    
    public Hint() {
        this.internalStorage = new HashMap<String, Object>();
        this.attachments = new ArrayList<Attachment>();
        this.lock = new AutoClosableReentrantLock();
        this.screenshot = null;
        this.viewHierarchy = null;
        this.threadDump = null;
        this.replayRecording = null;
    }
    
    @NotNull
    public static Hint withAttachment(@Nullable final Attachment attachment) {
        final Hint hint = new Hint();
        hint.addAttachment(attachment);
        return hint;
    }
    
    @NotNull
    public static Hint withAttachments(@Nullable final List<Attachment> attachments) {
        final Hint hint = new Hint();
        hint.addAttachments(attachments);
        return hint;
    }
    
    public void set(@NotNull final String name, @Nullable final Object hint) {
        try (final ISentryLifecycleToken ignored = this.lock.acquire()) {
            this.internalStorage.put(name, hint);
        }
    }
    
    @Nullable
    public Object get(@NotNull final String name) {
        try (final ISentryLifecycleToken ignored = this.lock.acquire()) {
            return this.internalStorage.get(name);
        }
    }
    
    @Nullable
    public <T> T getAs(@NotNull final String name, @NotNull final Class<T> clazz) {
        try (final ISentryLifecycleToken ignored = this.lock.acquire()) {
            final Object hintValue = this.internalStorage.get(name);
            if (clazz.isInstance(hintValue)) {
                final Object o = hintValue;
                if (ignored != null) {
                    ignored.close();
                }
                return (T)o;
            }
            if (this.isCastablePrimitive(hintValue, clazz)) {
                final Object o2 = hintValue;
                if (ignored != null) {
                    ignored.close();
                }
                return (T)o2;
            }
            return null;
        }
    }
    
    public void remove(@NotNull final String name) {
        try (final ISentryLifecycleToken ignored = this.lock.acquire()) {
            this.internalStorage.remove(name);
        }
    }
    
    public void addAttachment(@Nullable final Attachment attachment) {
        if (attachment != null) {
            this.attachments.add(attachment);
        }
    }
    
    public void addAttachments(@Nullable final List<Attachment> attachments) {
        if (attachments != null) {
            this.attachments.addAll(attachments);
        }
    }
    
    @NotNull
    public List<Attachment> getAttachments() {
        return new ArrayList<Attachment>(this.attachments);
    }
    
    public void replaceAttachments(@Nullable final List<Attachment> attachments) {
        this.clearAttachments();
        this.addAttachments(attachments);
    }
    
    public void clearAttachments() {
        this.attachments.clear();
    }
    
    @ApiStatus.Internal
    public void clear() {
        try (final ISentryLifecycleToken ignored = this.lock.acquire()) {
            final Iterator<Map.Entry<String, Object>> iterator = this.internalStorage.entrySet().iterator();
            while (iterator.hasNext()) {
                final Map.Entry<String, Object> entry = iterator.next();
                if (entry.getKey() == null || !entry.getKey().startsWith("sentry:")) {
                    iterator.remove();
                }
            }
        }
    }
    
    public void setScreenshot(@Nullable final Attachment screenshot) {
        this.screenshot = screenshot;
    }
    
    @Nullable
    public Attachment getScreenshot() {
        return this.screenshot;
    }
    
    public void setViewHierarchy(@Nullable final Attachment viewHierarchy) {
        this.viewHierarchy = viewHierarchy;
    }
    
    @Nullable
    public Attachment getViewHierarchy() {
        return this.viewHierarchy;
    }
    
    public void setThreadDump(@Nullable final Attachment threadDump) {
        this.threadDump = threadDump;
    }
    
    @Nullable
    public Attachment getThreadDump() {
        return this.threadDump;
    }
    
    @Nullable
    public ReplayRecording getReplayRecording() {
        return this.replayRecording;
    }
    
    public void setReplayRecording(@Nullable final ReplayRecording replayRecording) {
        this.replayRecording = replayRecording;
    }
    
    private boolean isCastablePrimitive(@Nullable final Object hintValue, @NotNull final Class<?> clazz) {
        final Class<?> nonPrimitiveClass = Hint.PRIMITIVE_MAPPINGS.get(clazz.getCanonicalName());
        return hintValue != null && clazz.isPrimitive() && nonPrimitiveClass != null && nonPrimitiveClass.isInstance(hintValue);
    }
    
    static {
        (PRIMITIVE_MAPPINGS = new HashMap<String, Class<?>>()).put("boolean", Boolean.class);
        Hint.PRIMITIVE_MAPPINGS.put("char", Character.class);
        Hint.PRIMITIVE_MAPPINGS.put("byte", Byte.class);
        Hint.PRIMITIVE_MAPPINGS.put("short", Short.class);
        Hint.PRIMITIVE_MAPPINGS.put("int", Integer.class);
        Hint.PRIMITIVE_MAPPINGS.put("long", Long.class);
        Hint.PRIMITIVE_MAPPINGS.put("float", Float.class);
        Hint.PRIMITIVE_MAPPINGS.put("double", Double.class);
    }
}
