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

package io.sentry;

import java.util.concurrent.ConcurrentHashMap;
import io.sentry.vendor.gson.stream.JsonToken;
import java.io.IOException;
import java.util.Iterator;
import java.util.HashMap;
import io.sentry.util.CollectionUtils;
import java.util.Collection;
import java.util.ArrayList;
import org.jetbrains.annotations.TestOnly;
import io.sentry.protocol.SentryId;
import java.util.Map;
import java.util.List;
import io.sentry.protocol.SentryException;
import io.sentry.protocol.SentryThread;
import org.jetbrains.annotations.Nullable;
import io.sentry.protocol.Message;
import org.jetbrains.annotations.NotNull;
import java.util.Date;

public final class SentryEvent extends SentryBaseEvent implements JsonUnknown, JsonSerializable
{
    @NotNull
    private Date timestamp;
    @Nullable
    private Message message;
    @Nullable
    private String logger;
    @Nullable
    private SentryValues<SentryThread> threads;
    @Nullable
    private SentryValues<SentryException> exception;
    @Nullable
    private SentryLevel level;
    @Nullable
    private String transaction;
    @Nullable
    private List<String> fingerprint;
    @Nullable
    private Map<String, Object> unknown;
    @Nullable
    private Map<String, String> modules;
    
    SentryEvent(@NotNull final SentryId eventId, @NotNull final Date timestamp) {
        super(eventId);
        this.timestamp = timestamp;
    }
    
    public SentryEvent(@Nullable final Throwable throwable) {
        this();
        this.throwable = throwable;
    }
    
    public SentryEvent() {
        this(new SentryId(), DateUtils.getCurrentDateTime());
    }
    
    @TestOnly
    public SentryEvent(@NotNull final Date timestamp) {
        this(new SentryId(), timestamp);
    }
    
    public Date getTimestamp() {
        return (Date)this.timestamp.clone();
    }
    
    public void setTimestamp(@NotNull final Date timestamp) {
        this.timestamp = timestamp;
    }
    
    @Nullable
    public Message getMessage() {
        return this.message;
    }
    
    public void setMessage(@Nullable final Message message) {
        this.message = message;
    }
    
    @Nullable
    public String getLogger() {
        return this.logger;
    }
    
    public void setLogger(@Nullable final String logger) {
        this.logger = logger;
    }
    
    @Nullable
    public List<SentryThread> getThreads() {
        if (this.threads != null) {
            return this.threads.getValues();
        }
        return null;
    }
    
    public void setThreads(@Nullable final List<SentryThread> threads) {
        this.threads = new SentryValues<SentryThread>(threads);
    }
    
    @Nullable
    public List<SentryException> getExceptions() {
        return (this.exception == null) ? null : this.exception.getValues();
    }
    
    public void setExceptions(@Nullable final List<SentryException> exception) {
        this.exception = new SentryValues<SentryException>(exception);
    }
    
    @Nullable
    public SentryLevel getLevel() {
        return this.level;
    }
    
    public void setLevel(@Nullable final SentryLevel level) {
        this.level = level;
    }
    
    @Nullable
    public String getTransaction() {
        return this.transaction;
    }
    
    public void setTransaction(@Nullable final String transaction) {
        this.transaction = transaction;
    }
    
    @Nullable
    public List<String> getFingerprints() {
        return this.fingerprint;
    }
    
    public void setFingerprints(@Nullable final List<String> fingerprint) {
        this.fingerprint = ((fingerprint != null) ? new ArrayList<String>(fingerprint) : null);
    }
    
    @Nullable
    Map<String, String> getModules() {
        return this.modules;
    }
    
    public void setModules(@Nullable final Map<String, String> modules) {
        this.modules = CollectionUtils.newHashMap(modules);
    }
    
    public void setModule(@NotNull final String key, @NotNull final String value) {
        if (this.modules == null) {
            this.modules = new HashMap<String, String>();
        }
        this.modules.put(key, value);
    }
    
    public void removeModule(@NotNull final String key) {
        if (this.modules != null) {
            this.modules.remove(key);
        }
    }
    
    @Nullable
    public String getModule(@NotNull final String key) {
        if (this.modules != null) {
            return this.modules.get(key);
        }
        return null;
    }
    
    public boolean isCrashed() {
        return this.getUnhandledException() != null;
    }
    
    @Nullable
    public SentryException getUnhandledException() {
        if (this.exception != null) {
            for (final SentryException e : this.exception.getValues()) {
                if (e.getMechanism() != null && e.getMechanism().isHandled() != null && !e.getMechanism().isHandled()) {
                    return e;
                }
            }
        }
        return null;
    }
    
    public boolean isErrored() {
        return this.exception != null && !this.exception.getValues().isEmpty();
    }
    
    @Override
    public void serialize(@NotNull final ObjectWriter writer, @NotNull final ILogger logger) throws IOException {
        writer.beginObject();
        writer.name("timestamp").value(logger, this.timestamp);
        if (this.message != null) {
            writer.name("message").value(logger, this.message);
        }
        if (this.logger != null) {
            writer.name("logger").value(this.logger);
        }
        if (this.threads != null && !this.threads.getValues().isEmpty()) {
            writer.name("threads");
            writer.beginObject();
            writer.name("values").value(logger, this.threads.getValues());
            writer.endObject();
        }
        if (this.exception != null && !this.exception.getValues().isEmpty()) {
            writer.name("exception");
            writer.beginObject();
            writer.name("values").value(logger, this.exception.getValues());
            writer.endObject();
        }
        if (this.level != null) {
            writer.name("level").value(logger, this.level);
        }
        if (this.transaction != null) {
            writer.name("transaction").value(this.transaction);
        }
        if (this.fingerprint != null) {
            writer.name("fingerprint").value(logger, this.fingerprint);
        }
        if (this.modules != null) {
            writer.name("modules").value(logger, this.modules);
        }
        new Serializer().serialize(this, writer, logger);
        if (this.unknown != null) {
            for (final String key : this.unknown.keySet()) {
                final Object value = this.unknown.get(key);
                writer.name(key);
                writer.value(logger, value);
            }
        }
        writer.endObject();
    }
    
    @Nullable
    @Override
    public Map<String, Object> getUnknown() {
        return this.unknown;
    }
    
    @Override
    public void setUnknown(@Nullable final Map<String, Object> unknown) {
        this.unknown = unknown;
    }
    
    public static final class JsonKeys
    {
        public static final String TIMESTAMP = "timestamp";
        public static final String MESSAGE = "message";
        public static final String LOGGER = "logger";
        public static final String THREADS = "threads";
        public static final String EXCEPTION = "exception";
        public static final String LEVEL = "level";
        public static final String TRANSACTION = "transaction";
        public static final String FINGERPRINT = "fingerprint";
        public static final String MODULES = "modules";
    }
    
    public static final class Deserializer implements JsonDeserializer<SentryEvent>
    {
        @NotNull
        @Override
        public SentryEvent deserialize(@NotNull final ObjectReader reader, @NotNull final ILogger logger) throws Exception {
            reader.beginObject();
            final SentryEvent event = new SentryEvent();
            Map<String, Object> unknown = null;
            final SentryBaseEvent.Deserializer baseEventDeserializer = new SentryBaseEvent.Deserializer();
            while (reader.peek() == JsonToken.NAME) {
                final String nextName2;
                final String nextName = nextName2 = reader.nextName();
                switch (nextName2) {
                    case "timestamp": {
                        final Date deserializedTimestamp = reader.nextDateOrNull(logger);
                        if (deserializedTimestamp != null) {
                            event.timestamp = deserializedTimestamp;
                            continue;
                        }
                        continue;
                    }
                    case "message": {
                        event.message = reader.nextOrNull(logger, (JsonDeserializer<Message>)new Message.Deserializer());
                        continue;
                    }
                    case "logger": {
                        event.logger = reader.nextStringOrNull();
                        continue;
                    }
                    case "threads": {
                        reader.beginObject();
                        reader.nextName();
                        event.threads = (SentryValues<SentryThread>)new SentryValues(reader.nextListOrNull(logger, (JsonDeserializer<T>)new SentryThread.Deserializer()));
                        reader.endObject();
                        continue;
                    }
                    case "exception": {
                        reader.beginObject();
                        reader.nextName();
                        event.exception = (SentryValues<SentryException>)new SentryValues(reader.nextListOrNull(logger, (JsonDeserializer<T>)new SentryException.Deserializer()));
                        reader.endObject();
                        continue;
                    }
                    case "level": {
                        event.level = reader.nextOrNull(logger, (JsonDeserializer<SentryLevel>)new SentryLevel.Deserializer());
                        continue;
                    }
                    case "transaction": {
                        event.transaction = reader.nextStringOrNull();
                        continue;
                    }
                    case "fingerprint": {
                        final List<String> deserializedFingerprint = (List<String>)reader.nextObjectOrNull();
                        if (deserializedFingerprint != null) {
                            event.fingerprint = deserializedFingerprint;
                            continue;
                        }
                        continue;
                    }
                    case "modules": {
                        final Map<String, String> deserializedModules = (Map<String, String>)reader.nextObjectOrNull();
                        event.modules = CollectionUtils.newConcurrentHashMap(deserializedModules);
                        continue;
                    }
                    default: {
                        if (!baseEventDeserializer.deserializeValue(event, nextName, reader, logger)) {
                            if (unknown == null) {
                                unknown = new ConcurrentHashMap<String, Object>();
                            }
                            reader.nextUnknown(logger, unknown, nextName);
                            continue;
                        }
                        continue;
                    }
                }
            }
            event.setUnknown(unknown);
            reader.endObject();
            return event;
        }
    }
}
