// 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.core.util.logging.ClientLogger;
import com.azure.json.JsonReader;
import com.azure.json.JsonSerializable;
import com.azure.json.JsonToken;
import com.azure.json.JsonWriter;
import java.io.IOException;
import tsptest.armstreamstyleserialization.fluent.models.ResultData;

/**
 * The Result model.
 */
@Immutable
public final class Result implements JsonSerializable<Result> {
    /*
     * The name property.
     */
    private String name;

    /*
     * The data property.
     */
    private ResultData innerData;

    /**
     * Creates an instance of Result class.
     */
    private Result() {
    }

    /**
     * Get the name property: The name property.
     * 
     * @return the name value.
     */
    public String name() {
        return this.name;
    }

    /**
     * Get the innerData property: The data property.
     * 
     * @return the innerData value.
     */
    private ResultData innerData() {
        return this.innerData;
    }

    /**
     * Get the prop1 property: The prop1 property.
     * 
     * @return the prop1 value.
     */
    public String prop1() {
        return this.innerData() == null ? null : this.innerData().prop1();
    }

    /**
     * Get the prop2 property: The prop2 property.
     * 
     * @return the prop2 value.
     */
    public String prop2() {
        return this.innerData() == null ? null : this.innerData().prop2();
    }

    /**
     * Validates the instance.
     * 
     * @throws IllegalArgumentException thrown if the instance is not valid.
     */
    public void validate() {
        if (name() == null) {
            throw LOGGER.atError().log(new IllegalArgumentException("Missing required property name in model Result"));
        }
        if (innerData() != null) {
            innerData().validate();
        }
    }

    private static final ClientLogger LOGGER = new ClientLogger(Result.class);

    /**
     * {@inheritDoc}
     */
    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeStringField("name", this.name);
        return jsonWriter.writeEndObject();
    }

    /**
     * Reads an instance of Result from the JsonReader.
     * 
     * @param jsonReader The JsonReader being read.
     * @return An instance of Result 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 Result.
     */
    public static Result fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(reader -> {
            Result deserializedResult = new Result();
            while (reader.nextToken() != JsonToken.END_OBJECT) {
                String fieldName = reader.getFieldName();
                reader.nextToken();

                if ("name".equals(fieldName)) {
                    deserializedResult.name = reader.getString();
                } else if ("data".equals(fieldName)) {
                    deserializedResult.innerData = ResultData.fromJson(reader);
                } else {
                    reader.skipChildren();
                }
            }

            return deserializedResult;
        });
    }
}
