// 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.Fluent;
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 tsptest.armstreamstyleserialization.fluent.models.FunctionConfiguration;

/**
 * The AggregateFunctionProperties model.
 */
@Fluent
public final class AggregateFunctionProperties extends FunctionProperties {
    /*
     * Discriminator property for FunctionProperties.
     */
    private String kind = "aggregate";

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

    /**
     * Get the kind property: Discriminator property for FunctionProperties.
     * 
     * @return the kind value.
     */
    @Override
    public String kind() {
        return this.kind;
    }

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

    /**
     * Set the input property: The input property.
     * 
     * @param input the input value to set.
     * @return the AggregateFunctionProperties object itself.
     */
    public AggregateFunctionProperties withInput(String input) {
        if (this.innerProperties() == null) {
            this.withInnerProperties(new FunctionConfiguration());
        }
        this.innerProperties().withInput(input);
        return this;
    }

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

    /**
     * Set the output property: The output property.
     * 
     * @param output the output value to set.
     * @return the AggregateFunctionProperties object itself.
     */
    public AggregateFunctionProperties withOutput(String output) {
        if (this.innerProperties() == null) {
            this.withInnerProperties(new FunctionConfiguration());
        }
        this.innerProperties().withOutput(output);
        return this;
    }

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

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

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

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

                if ("properties".equals(fieldName)) {
                    deserializedAggregateFunctionProperties.withInnerProperties(FunctionConfiguration.fromJson(reader));
                } else if ("kind".equals(fieldName)) {
                    deserializedAggregateFunctionProperties.kind = reader.getString();
                } else {
                    reader.skipChildren();
                }
            }

            return deserializedAggregateFunctionProperties;
        });
    }
}
