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

package io.sentry.profilemeasurements;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import io.sentry.vendor.gson.stream.JsonToken;
import io.sentry.ObjectReader;
import io.sentry.JsonDeserializer;
import java.math.RoundingMode;
import java.math.BigDecimal;
import java.io.IOException;
import java.util.Iterator;
import io.sentry.ILogger;
import io.sentry.ObjectWriter;
import io.sentry.util.Objects;
import io.sentry.DateUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import org.jetbrains.annotations.ApiStatus;
import io.sentry.JsonSerializable;
import io.sentry.JsonUnknown;

@ApiStatus.Internal
public final class ProfileMeasurementValue implements JsonUnknown, JsonSerializable
{
    @Nullable
    private Map<String, Object> unknown;
    private double timestamp;
    @NotNull
    private String relativeStartNs;
    private double value;
    
    public ProfileMeasurementValue() {
        this(0L, 0, 0L);
    }
    
    public ProfileMeasurementValue(@NotNull final Long relativeStartNs, @NotNull final Number value, final long nanoTimestamp) {
        this.relativeStartNs = relativeStartNs.toString();
        this.value = value.doubleValue();
        this.timestamp = DateUtils.nanosToSeconds(nanoTimestamp);
    }
    
    public double getTimestamp() {
        return this.timestamp;
    }
    
    public double getValue() {
        return this.value;
    }
    
    @NotNull
    public String getRelativeStartNs() {
        return this.relativeStartNs;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || this.getClass() != o.getClass()) {
            return false;
        }
        final ProfileMeasurementValue that = (ProfileMeasurementValue)o;
        return Objects.equals(this.unknown, that.unknown) && this.relativeStartNs.equals(that.relativeStartNs) && this.value == that.value && this.timestamp == that.timestamp;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.unknown, this.relativeStartNs, this.value);
    }
    
    @Override
    public void serialize(@NotNull final ObjectWriter writer, @NotNull final ILogger logger) throws IOException {
        writer.beginObject();
        writer.name("value").value(logger, this.value);
        writer.name("elapsed_since_start_ns").value(logger, this.relativeStartNs);
        writer.name("timestamp").value(logger, this.doubleToBigDecimal(this.timestamp));
        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();
    }
    
    @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 JsonKeys
    {
        public static final String VALUE = "value";
        public static final String START_NS = "elapsed_since_start_ns";
        public static final String TIMESTAMP = "timestamp";
    }
    
    public static final class Deserializer implements JsonDeserializer<ProfileMeasurementValue>
    {
        @NotNull
        @Override
        public ProfileMeasurementValue deserialize(@NotNull final ObjectReader reader, @NotNull final ILogger logger) throws Exception {
            reader.beginObject();
            final ProfileMeasurementValue data = new ProfileMeasurementValue();
            Map<String, Object> unknown = null;
            while (reader.peek() == JsonToken.NAME) {
                final String nextName2;
                final String nextName = nextName2 = reader.nextName();
                switch (nextName2) {
                    case "value": {
                        final Double value = reader.nextDoubleOrNull();
                        if (value != null) {
                            data.value = value;
                            continue;
                        }
                        continue;
                    }
                    case "elapsed_since_start_ns": {
                        final String startNs = reader.nextStringOrNull();
                        if (startNs != null) {
                            data.relativeStartNs = startNs;
                            continue;
                        }
                        continue;
                    }
                    case "timestamp": {
                        Double timestamp;
                        try {
                            timestamp = reader.nextDoubleOrNull();
                        }
                        catch (final NumberFormatException e) {
                            final Date date = reader.nextDateOrNull(logger);
                            timestamp = ((date != null) ? Double.valueOf(DateUtils.dateToSeconds(date)) : null);
                        }
                        if (timestamp != null) {
                            data.timestamp = timestamp;
                            continue;
                        }
                        continue;
                    }
                    default: {
                        if (unknown == null) {
                            unknown = new ConcurrentHashMap<String, Object>();
                        }
                        reader.nextUnknown(logger, unknown, nextName);
                        continue;
                    }
                }
            }
            data.setUnknown(unknown);
            reader.endObject();
            return data;
        }
    }
}
