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

package tsptest.armstreamstyleserialization.fluent.models;

import com.azure.core.annotation.Fluent;
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.models.FunctionProperties;

/**
 * The Function model.
 */
@Fluent
public final class FunctionInner implements JsonSerializable<FunctionInner> {
    /*
     * The properties property.
     */
    private FunctionProperties properties;

    /**
     * Creates an instance of FunctionInner class.
     */
    public FunctionInner() {
    }

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

    /**
     * Set the properties property: The properties property.
     * 
     * @param properties the properties value to set.
     * @return the FunctionInner object itself.
     */
    public FunctionInner withProperties(FunctionProperties properties) {
        this.properties = properties;
        return this;
    }

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

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

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

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

                if ("properties".equals(fieldName)) {
                    deserializedFunctionInner.properties = FunctionProperties.fromJson(reader);
                } else {
                    reader.skipChildren();
                }
            }

            return deserializedFunctionInner;
        });
    }
}
