// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) TypeSpec Code Generator.

package tsptest.namingjavaparser.models;

import com.azure.core.annotation.Generated;
import com.azure.core.annotation.Immutable;
import com.azure.core.util.CoreUtils;
import com.azure.json.JsonReader;
import com.azure.json.JsonToken;
import com.azure.json.JsonWriter;
import java.io.IOException;

/**
 * The BytesData model.
 */
@Immutable
public final class BytesData extends Data {
    /*
     * The @data.kind property.
     */
    @Generated
    private String type = "bytes";

    /*
     * Data as {@code byte[]}
     */
    @Generated
    private final byte[] dataAsBytes;

    /**
     * Creates an instance of BytesData class.
     * 
     * @param dataAsBytes the dataAsBytes value to set.
     */
    @Generated
    private BytesData(byte[] dataAsBytes) {
        this.dataAsBytes = dataAsBytes;
    }

    /**
     * Get the type property: The &#064;data.kind property.
     * 
     * @return the type value.
     */
    @Generated
    @Override
    public String getType() {
        return this.type;
    }

    /**
     * Get the dataAsBytes property: Data as {@code byte[]}.
     * 
     * @return the dataAsBytes value.
     */
    @Generated
    public byte[] getDataAsBytes() {
        return CoreUtils.clone(this.dataAsBytes);
    }

    /**
     * {@inheritDoc}
     */
    @Generated
    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeBinaryField("data_bytes", this.dataAsBytes);
        jsonWriter.writeStringField("@data.kind", this.type);
        return jsonWriter.writeEndObject();
    }

    /**
     * Reads an instance of BytesData from the JsonReader.
     * 
     * @param jsonReader The JsonReader being read.
     * @return An instance of BytesData if the JsonReader was pointing to an instance of it, or null if it was pointing
     * to JSON null.
     * @throws IllegalStateException If the deserialized JSON object was missing any required properties.
     * @throws IOException If an error occurs while reading the BytesData.
     */
    @Generated
    public static BytesData fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(reader -> {
            byte[] dataAsBytes = null;
            String type = "bytes";
            while (reader.nextToken() != JsonToken.END_OBJECT) {
                String fieldName = reader.getFieldName();
                reader.nextToken();

                if ("data_bytes".equals(fieldName)) {
                    dataAsBytes = reader.getBinary();
                } else if ("@data.kind".equals(fieldName)) {
                    type = reader.getString();
                } else {
                    reader.skipChildren();
                }
            }
            BytesData deserializedBytesData = new BytesData(dataAsBytes);
            deserializedBytesData.type = type;

            return deserializedBytesData;
        });
    }
}
