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

package tsptest.armstreamstyleserialization.implementation.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 java.util.List;
import tsptest.armstreamstyleserialization.models.Result;

/**
 * The ListResult model.
 */
@Immutable
public final class ListResult implements JsonSerializable<ListResult> {
    /*
     * The items property.
     */
    private List<Result> items;

    /*
     * The nextLink property.
     */
    private String nextLink;

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

    /**
     * Get the items property: The items property.
     * 
     * @return the items value.
     */
    public List<Result> items() {
        return this.items;
    }

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

    /**
     * Validates the instance.
     * 
     * @throws IllegalArgumentException thrown if the instance is not valid.
     */
    public void validate() {
        if (items() == null) {
            throw LOGGER.atError()
                .log(new IllegalArgumentException("Missing required property items in model ListResult"));
        } else {
            items().forEach(e -> e.validate());
        }
    }

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

    /**
     * {@inheritDoc}
     */
    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeArrayField("items", this.items, (writer, element) -> writer.writeJson(element));
        jsonWriter.writeStringField("nextLink", this.nextLink);
        return jsonWriter.writeEndObject();
    }

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

                if ("items".equals(fieldName)) {
                    List<Result> items = reader.readArray(reader1 -> Result.fromJson(reader1));
                    deserializedListResult.items = items;
                } else if ("nextLink".equals(fieldName)) {
                    deserializedListResult.nextLink = reader.getString();
                } else {
                    reader.skipChildren();
                }
            }

            return deserializedListResult;
        });
    }
}
