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

package tsptest.armstreamstyleserialization.models;

import com.azure.core.annotation.Immutable;
import com.azure.json.JsonReader;
import com.azure.json.JsonSerializable;
import com.azure.json.JsonToken;
import com.azure.json.JsonWriter;
import java.io.IOException;

/**
 * Test extensible enum type for discriminator.
 */
@Immutable
public class Dog implements JsonSerializable<Dog> {
    /*
     * discriminator property
     */
    private DogKind kind = DogKind.fromString("Dog");

    /*
     * Weight of the dog
     */
    private int weight;

    /*
     * dna of the dog
     */
    private String dna;

    /**
     * Creates an instance of Dog class.
     */
    protected Dog() {
    }

    /**
     * Get the kind property: discriminator property.
     * 
     * @return the kind value.
     */
    public DogKind kind() {
        return this.kind;
    }

    /**
     * Get the weight property: Weight of the dog.
     * 
     * @return the weight value.
     */
    public int weight() {
        return this.weight;
    }

    /**
     * Set the weight property: Weight of the dog.
     * 
     * @param weight the weight value to set.
     * @return the Dog object itself.
     */
    Dog withWeight(int weight) {
        this.weight = weight;
        return this;
    }

    /**
     * Get the dna property: dna of the dog.
     * 
     * @return the dna value.
     */
    public String dna() {
        return this.dna;
    }

    /**
     * Set the dna property: dna of the dog.
     * 
     * @param dna the dna value to set.
     * @return the Dog object itself.
     */
    Dog withDna(String dna) {
        this.dna = dna;
        return this;
    }

    /**
     * Validates the instance.
     * 
     * @throws IllegalArgumentException thrown if the instance is not valid.
     */
    public void validate() {
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeIntField("weight", this.weight);
        jsonWriter.writeStringField("kind", this.kind == null ? null : this.kind.toString());
        return jsonWriter.writeEndObject();
    }

    /**
     * Reads an instance of Dog from the JsonReader.
     * 
     * @param jsonReader The JsonReader being read.
     * @return An instance of Dog 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 Dog.
     */
    public static Dog fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(reader -> {
            String discriminatorValue = null;
            try (JsonReader readerToUse = reader.bufferObject()) {
                readerToUse.nextToken(); // Prepare for reading
                while (readerToUse.nextToken() != JsonToken.END_OBJECT) {
                    String fieldName = readerToUse.getFieldName();
                    readerToUse.nextToken();
                    if ("kind".equals(fieldName)) {
                        discriminatorValue = readerToUse.getString();
                        break;
                    } else {
                        readerToUse.skipChildren();
                    }
                }
                // Use the discriminator value to determine which subtype should be deserialized.
                if ("golden".equals(discriminatorValue)) {
                    return Golden.fromJson(readerToUse.reset());
                } else {
                    return fromJsonKnownDiscriminator(readerToUse.reset());
                }
            }
        });
    }

    static Dog fromJsonKnownDiscriminator(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(reader -> {
            Dog deserializedDog = new Dog();
            while (reader.nextToken() != JsonToken.END_OBJECT) {
                String fieldName = reader.getFieldName();
                reader.nextToken();

                if ("weight".equals(fieldName)) {
                    deserializedDog.weight = reader.getInt();
                } else if ("dna".equals(fieldName)) {
                    deserializedDog.dna = reader.getString();
                } else if ("kind".equals(fieldName)) {
                    deserializedDog.kind = DogKind.fromString(reader.getString());
                } else {
                    reader.skipChildren();
                }
            }

            return deserializedDog;
        });
    }
}
