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

package io.sentry;

import io.sentry.vendor.gson.stream.JsonToken;
import java.util.concurrent.ConcurrentHashMap;
import java.math.RoundingMode;
import java.math.BigDecimal;
import java.io.IOException;
import java.util.Iterator;
import java.util.Objects;
import java.util.HashMap;
import io.sentry.protocol.profiling.SentryProfile;
import java.io.File;
import io.sentry.profilemeasurements.ProfileMeasurement;
import java.util.Map;
import io.sentry.protocol.SdkVersion;
import org.jetbrains.annotations.NotNull;
import io.sentry.protocol.SentryId;
import org.jetbrains.annotations.Nullable;
import io.sentry.protocol.DebugMeta;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public final class ProfileChunk implements JsonUnknown, JsonSerializable
{
    public static final String PLATFORM_ANDROID = "android";
    public static final String PLATFORM_JAVA = "java";
    @Nullable
    private DebugMeta debugMeta;
    @NotNull
    private SentryId profilerId;
    @NotNull
    private SentryId chunkId;
    @Nullable
    private SdkVersion clientSdk;
    @NotNull
    private final Map<String, ProfileMeasurement> measurements;
    @NotNull
    private String platform;
    @NotNull
    private String release;
    @Nullable
    private String environment;
    @NotNull
    private String version;
    private double timestamp;
    @NotNull
    private final File traceFile;
    @Nullable
    private String sampledProfile;
    @Nullable
    private SentryProfile sentryProfile;
    @Nullable
    private Map<String, Object> unknown;
    
    public ProfileChunk() {
        this(SentryId.EMPTY_ID, SentryId.EMPTY_ID, new File("dummy"), new HashMap<String, ProfileMeasurement>(), 0.0, "android", SentryOptions.empty());
    }
    
    public ProfileChunk(@NotNull final SentryId profilerId, @NotNull final SentryId chunkId, @NotNull final File traceFile, @NotNull final Map<String, ProfileMeasurement> measurements, @NotNull final Double timestamp, @NotNull final String platform, @NotNull final SentryOptions options) {
        this.sampledProfile = null;
        this.profilerId = profilerId;
        this.chunkId = chunkId;
        this.traceFile = traceFile;
        this.measurements = measurements;
        this.debugMeta = null;
        this.clientSdk = options.getSdkVersion();
        this.release = ((options.getRelease() != null) ? options.getRelease() : "");
        this.environment = options.getEnvironment();
        this.platform = platform;
        this.version = "2";
        this.timestamp = timestamp;
    }
    
    @NotNull
    public Map<String, ProfileMeasurement> getMeasurements() {
        return this.measurements;
    }
    
    @Nullable
    public DebugMeta getDebugMeta() {
        return this.debugMeta;
    }
    
    public void setDebugMeta(@Nullable final DebugMeta debugMeta) {
        this.debugMeta = debugMeta;
    }
    
    @Nullable
    public SdkVersion getClientSdk() {
        return this.clientSdk;
    }
    
    @NotNull
    public SentryId getChunkId() {
        return this.chunkId;
    }
    
    @Nullable
    public String getEnvironment() {
        return this.environment;
    }
    
    @NotNull
    public String getPlatform() {
        return this.platform;
    }
    
    @NotNull
    public SentryId getProfilerId() {
        return this.profilerId;
    }
    
    @NotNull
    public String getRelease() {
        return this.release;
    }
    
    @Nullable
    public String getSampledProfile() {
        return this.sampledProfile;
    }
    
    public void setSampledProfile(@Nullable final String sampledProfile) {
        this.sampledProfile = sampledProfile;
    }
    
    @NotNull
    public File getTraceFile() {
        return this.traceFile;
    }
    
    public double getTimestamp() {
        return this.timestamp;
    }
    
    @NotNull
    public String getVersion() {
        return this.version;
    }
    
    @Nullable
    public SentryProfile getSentryProfile() {
        return this.sentryProfile;
    }
    
    public void setSentryProfile(@Nullable final SentryProfile sentryProfile) {
        this.sentryProfile = sentryProfile;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof ProfileChunk)) {
            return false;
        }
        final ProfileChunk that = (ProfileChunk)o;
        return Objects.equals(this.debugMeta, that.debugMeta) && Objects.equals(this.profilerId, that.profilerId) && Objects.equals(this.chunkId, that.chunkId) && Objects.equals(this.clientSdk, that.clientSdk) && Objects.equals(this.measurements, that.measurements) && Objects.equals(this.platform, that.platform) && Objects.equals(this.release, that.release) && Objects.equals(this.environment, that.environment) && Objects.equals(this.version, that.version) && Objects.equals(this.sampledProfile, that.sampledProfile) && Objects.equals(this.unknown, that.unknown) && Objects.equals(this.sentryProfile, that.sentryProfile);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.debugMeta, this.profilerId, this.chunkId, this.clientSdk, this.measurements, this.platform, this.release, this.environment, this.version, this.sampledProfile, this.sentryProfile, this.unknown);
    }
    
    @Override
    public void serialize(@NotNull final ObjectWriter writer, @NotNull final ILogger logger) throws IOException {
        writer.beginObject();
        if (this.debugMeta != null) {
            writer.name("debug_meta").value(logger, this.debugMeta);
        }
        writer.name("profiler_id").value(logger, this.profilerId);
        writer.name("chunk_id").value(logger, this.chunkId);
        if (this.clientSdk != null) {
            writer.name("client_sdk").value(logger, this.clientSdk);
        }
        if (!this.measurements.isEmpty()) {
            final String prevIndent = writer.getIndent();
            writer.setIndent("");
            writer.name("measurements").value(logger, this.measurements);
            writer.setIndent(prevIndent);
        }
        writer.name("platform").value(logger, this.platform);
        writer.name("release").value(logger, this.release);
        if (this.environment != null) {
            writer.name("environment").value(logger, this.environment);
        }
        writer.name("version").value(logger, this.version);
        if (this.sampledProfile != null) {
            writer.name("sampled_profile").value(logger, this.sampledProfile);
        }
        writer.name("timestamp").value(logger, this.doubleToBigDecimal(this.timestamp));
        if (this.sentryProfile != null) {
            writer.name("profile").value(logger, this.sentryProfile);
        }
        if (this.unknown != null) {
            for (final String key : this.unknown.keySet()) {
                final Object value = this.unknown.get(key);
                writer.name(key).value(logger, value);
            }
        }
        writer.endObject();
    }
    
    @NotNull
    private BigDecimal doubleToBigDecimal(@NotNull final Double value) {
        return BigDecimal.valueOf(value).setScale(6, RoundingMode.DOWN);
    }
    
    @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 Builder
    {
        @NotNull
        private final SentryId profilerId;
        @NotNull
        private final SentryId chunkId;
        @NotNull
        private final Map<String, ProfileMeasurement> measurements;
        @NotNull
        private final File traceFile;
        private final double timestamp;
        @NotNull
        private final String platform;
        
        public Builder(@NotNull final SentryId profilerId, @NotNull final SentryId chunkId, @NotNull final Map<String, ProfileMeasurement> measurements, @NotNull final File traceFile, @NotNull final SentryDate timestamp, @NotNull final String platform) {
            this.profilerId = profilerId;
            this.chunkId = chunkId;
            this.measurements = new ConcurrentHashMap<String, ProfileMeasurement>(measurements);
            this.traceFile = traceFile;
            this.timestamp = DateUtils.nanosToSeconds(timestamp.nanoTimestamp());
            this.platform = platform;
        }
        
        public ProfileChunk build(final SentryOptions options) {
            return new ProfileChunk(this.profilerId, this.chunkId, this.traceFile, this.measurements, this.timestamp, this.platform, options);
        }
    }
    
    public static final class JsonKeys
    {
        public static final String DEBUG_META = "debug_meta";
        public static final String PROFILER_ID = "profiler_id";
        public static final String CHUNK_ID = "chunk_id";
        public static final String CLIENT_SDK = "client_sdk";
        public static final String MEASUREMENTS = "measurements";
        public static final String PLATFORM = "platform";
        public static final String RELEASE = "release";
        public static final String ENVIRONMENT = "environment";
        public static final String VERSION = "version";
        public static final String SAMPLED_PROFILE = "sampled_profile";
        public static final String TIMESTAMP = "timestamp";
        public static final String SENTRY_PROFILE = "profile";
    }
    
    public static final class Deserializer implements JsonDeserializer<ProfileChunk>
    {
        @NotNull
        @Override
        public ProfileChunk deserialize(@NotNull final ObjectReader reader, @NotNull final ILogger logger) throws Exception {
            reader.beginObject();
            final ProfileChunk data = new ProfileChunk();
            Map<String, Object> unknown = null;
            while (reader.peek() == JsonToken.NAME) {
                final String nextName2;
                final String nextName = nextName2 = reader.nextName();
                switch (nextName2) {
                    case "debug_meta": {
                        final DebugMeta debugMeta = reader.nextOrNull(logger, (JsonDeserializer<DebugMeta>)new DebugMeta.Deserializer());
                        if (debugMeta != null) {
                            data.debugMeta = debugMeta;
                            continue;
                        }
                        continue;
                    }
                    case "profiler_id": {
                        final SentryId profilerId = reader.nextOrNull(logger, (JsonDeserializer<SentryId>)new SentryId.Deserializer());
                        if (profilerId != null) {
                            data.profilerId = profilerId;
                            continue;
                        }
                        continue;
                    }
                    case "chunk_id": {
                        final SentryId chunkId = reader.nextOrNull(logger, (JsonDeserializer<SentryId>)new SentryId.Deserializer());
                        if (chunkId != null) {
                            data.chunkId = chunkId;
                            continue;
                        }
                        continue;
                    }
                    case "client_sdk": {
                        final SdkVersion clientSdk = reader.nextOrNull(logger, (JsonDeserializer<SdkVersion>)new SdkVersion.Deserializer());
                        if (clientSdk != null) {
                            data.clientSdk = clientSdk;
                            continue;
                        }
                        continue;
                    }
                    case "measurements": {
                        final Map<String, ProfileMeasurement> measurements = reader.nextMapOrNull(logger, (JsonDeserializer<ProfileMeasurement>)new ProfileMeasurement.Deserializer());
                        if (measurements != null) {
                            data.measurements.putAll(measurements);
                            continue;
                        }
                        continue;
                    }
                    case "platform": {
                        final String platform = reader.nextStringOrNull();
                        if (platform != null) {
                            data.platform = platform;
                            continue;
                        }
                        continue;
                    }
                    case "release": {
                        final String release = reader.nextStringOrNull();
                        if (release != null) {
                            data.release = release;
                            continue;
                        }
                        continue;
                    }
                    case "environment": {
                        final String environment = reader.nextStringOrNull();
                        if (environment != null) {
                            data.environment = environment;
                            continue;
                        }
                        continue;
                    }
                    case "version": {
                        final String version = reader.nextStringOrNull();
                        if (version != null) {
                            data.version = version;
                            continue;
                        }
                        continue;
                    }
                    case "sampled_profile": {
                        final String sampledProfile = reader.nextStringOrNull();
                        if (sampledProfile != null) {
                            data.sampledProfile = sampledProfile;
                            continue;
                        }
                        continue;
                    }
                    case "timestamp": {
                        final Double timestamp = reader.nextDoubleOrNull();
                        if (timestamp != null) {
                            data.timestamp = timestamp;
                            continue;
                        }
                        continue;
                    }
                    case "profile": {
                        final SentryProfile sentryProfile = reader.nextOrNull(logger, (JsonDeserializer<SentryProfile>)new SentryProfile.Deserializer());
                        if (sentryProfile != null) {
                            data.sentryProfile = sentryProfile;
                            continue;
                        }
                        continue;
                    }
                    default: {
                        if (unknown == null) {
                            unknown = new ConcurrentHashMap<String, Object>();
                        }
                        reader.nextUnknown(logger, unknown, nextName);
                        continue;
                    }
                }
            }
            data.setUnknown(unknown);
            reader.endObject();
            return data;
        }
    }
}
