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

package io.sentry;

import java.util.HashMap;
import io.sentry.vendor.gson.stream.JsonToken;
import java.io.IOException;
import java.util.Iterator;
import io.sentry.util.Objects;
import java.util.Map;
import java.util.concurrent.Callable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public final class SentryEnvelopeItemHeader implements JsonSerializable, JsonUnknown
{
    @Nullable
    private final String contentType;
    @Nullable
    private final Integer itemCount;
    @Nullable
    private final String fileName;
    @Nullable
    private final String platform;
    @NotNull
    private final SentryItemType type;
    private final int length;
    @Nullable
    private final Callable<Integer> getLength;
    @Nullable
    private final String attachmentType;
    @Nullable
    private Map<String, Object> unknown;
    
    @NotNull
    public SentryItemType getType() {
        return this.type;
    }
    
    public int getLength() {
        if (this.getLength != null) {
            try {
                return this.getLength.call();
            }
            catch (final Throwable ignored) {
                return -1;
            }
        }
        return this.length;
    }
    
    @Nullable
    public String getContentType() {
        return this.contentType;
    }
    
    @Nullable
    public String getFileName() {
        return this.fileName;
    }
    
    @Nullable
    public String getPlatform() {
        return this.platform;
    }
    
    @ApiStatus.Internal
    public SentryEnvelopeItemHeader(@NotNull final SentryItemType type, final int length, @Nullable final String contentType, @Nullable final String fileName, @Nullable final String attachmentType, @Nullable final String platform, @Nullable final Integer itemCount) {
        this.type = Objects.requireNonNull(type, "type is required");
        this.contentType = contentType;
        this.length = length;
        this.fileName = fileName;
        this.getLength = null;
        this.attachmentType = attachmentType;
        this.platform = platform;
        this.itemCount = itemCount;
    }
    
    SentryEnvelopeItemHeader(@NotNull final SentryItemType type, @Nullable final Callable<Integer> getLength, @Nullable final String contentType, @Nullable final String fileName, @Nullable final String attachmentType) {
        this(type, getLength, contentType, fileName, attachmentType, null, null);
    }
    
    SentryEnvelopeItemHeader(@NotNull final SentryItemType type, @Nullable final Callable<Integer> getLength, @Nullable final String contentType, @Nullable final String fileName, @Nullable final String attachmentType, @Nullable final String platform, @Nullable final Integer itemCount) {
        this.type = Objects.requireNonNull(type, "type is required");
        this.contentType = contentType;
        this.length = -1;
        this.fileName = fileName;
        this.getLength = getLength;
        this.attachmentType = attachmentType;
        this.platform = platform;
        this.itemCount = itemCount;
    }
    
    SentryEnvelopeItemHeader(@NotNull final SentryItemType type, @Nullable final Callable<Integer> getLength, @Nullable final String contentType, @Nullable final String fileName) {
        this(type, getLength, contentType, fileName, null);
    }
    
    @Nullable
    public String getAttachmentType() {
        return this.attachmentType;
    }
    
    @Override
    public void serialize(@NotNull final ObjectWriter writer, @NotNull final ILogger logger) throws IOException {
        writer.beginObject();
        if (this.contentType != null) {
            writer.name("content_type").value(this.contentType);
        }
        if (this.fileName != null) {
            writer.name("filename").value(this.fileName);
        }
        writer.name("type").value(logger, this.type);
        if (this.attachmentType != null) {
            writer.name("attachment_type").value(this.attachmentType);
        }
        if (this.platform != null) {
            writer.name("platform").value(this.platform);
        }
        if (this.itemCount != null) {
            writer.name("item_count").value(this.itemCount);
        }
        writer.name("length").value(this.getLength());
        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 CONTENT_TYPE = "content_type";
        public static final String FILENAME = "filename";
        public static final String TYPE = "type";
        public static final String ATTACHMENT_TYPE = "attachment_type";
        public static final String LENGTH = "length";
        public static final String PLATFORM = "platform";
        public static final String ITEM_COUNT = "item_count";
    }
    
    public static final class Deserializer implements JsonDeserializer<SentryEnvelopeItemHeader>
    {
        @NotNull
        @Override
        public SentryEnvelopeItemHeader deserialize(@NotNull final ObjectReader reader, @NotNull final ILogger logger) throws Exception {
            reader.beginObject();
            String contentType = null;
            String fileName = null;
            SentryItemType type = null;
            int length = 0;
            String attachmentType = null;
            String platform = null;
            Integer itemCount = null;
            Map<String, Object> unknown = null;
            while (reader.peek() == JsonToken.NAME) {
                final String nextName2;
                final String nextName = nextName2 = reader.nextName();
                switch (nextName2) {
                    case "content_type": {
                        contentType = reader.nextStringOrNull();
                        continue;
                    }
                    case "filename": {
                        fileName = reader.nextStringOrNull();
                        continue;
                    }
                    case "type": {
                        type = reader.nextOrNull(logger, (JsonDeserializer<SentryItemType>)new SentryItemType.Deserializer());
                        continue;
                    }
                    case "length": {
                        length = reader.nextInt();
                        continue;
                    }
                    case "attachment_type": {
                        attachmentType = reader.nextStringOrNull();
                        continue;
                    }
                    case "platform": {
                        platform = reader.nextStringOrNull();
                        continue;
                    }
                    case "item_count": {
                        itemCount = reader.nextIntegerOrNull();
                        continue;
                    }
                    default: {
                        if (unknown == null) {
                            unknown = new HashMap<String, Object>();
                        }
                        reader.nextUnknown(logger, unknown, nextName);
                        continue;
                    }
                }
            }
            if (type == null) {
                throw this.missingRequiredFieldException("type", logger);
            }
            final SentryEnvelopeItemHeader sentryEnvelopeItemHeader = new SentryEnvelopeItemHeader(type, length, contentType, fileName, attachmentType, platform, itemCount);
            sentryEnvelopeItemHeader.setUnknown(unknown);
            reader.endObject();
            return sentryEnvelopeItemHeader;
        }
        
        private Exception missingRequiredFieldException(final String field, final ILogger logger) {
            final String message = "Missing required field \"" + field + "\"";
            final Exception exception = new IllegalStateException(message);
            logger.log(SentryLevel.ERROR, message, exception);
            return exception;
        }
    }
}
