// 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.JsonToken;
import com.azure.json.JsonWriter;
import java.io.IOException;
import java.util.List;
import tsptest.armstreamstyleserialization.models.Result;

/**
 * The ListResultSummary model.
 */
@Immutable
public final class ListResultSummary extends ListResult {
    /*
     * The summary property.
     */
    private String summary;

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

    /*
     * The items property.
     */
    private List<Result> items;

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

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

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

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

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

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

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

    /**
     * Reads an instance of ListResultSummary from the JsonReader.
     * 
     * @param jsonReader The JsonReader being read.
     * @return An instance of ListResultSummary 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 ListResultSummary.
     */
    public static ListResultSummary fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(reader -> {
            ListResultSummary deserializedListResultSummary = new ListResultSummary();
            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));
                    deserializedListResultSummary.items = items;
                } else if ("nextLink".equals(fieldName)) {
                    deserializedListResultSummary.nextLink = reader.getString();
                } else if ("summary".equals(fieldName)) {
                    deserializedListResultSummary.summary = reader.getString();
                } else {
                    reader.skipChildren();
                }
            }

            return deserializedListResultSummary;
        });
    }
}
