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

package com.azure.security.keyvault.administration.implementation.models;

import com.azure.core.annotation.Fluent;
import com.azure.json.JsonReader;
import com.azure.json.JsonSerializable;
import com.azure.json.JsonToken;
import com.azure.json.JsonWriter;
import com.azure.security.keyvault.administration.models.KeyVaultSettingType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/** The Setting model. */
@Fluent
public final class Setting implements JsonSerializable<Setting> {
    /*
     * The account setting to be updated
     */
    private final String name;

    /*
     * The value of the pool setting.
     */
    private final String value;

    /*
     * The type specifier of the value.
     */
    private KeyVaultSettingType type;

    /**
     * Creates an instance of Setting class.
     *
     * @param name the name value to set.
     * @param value the value value to set.
     */
    public Setting(String name, String value) {
        this.name = name;
        this.value = value;
    }

    /**
     * Get the name property: The account setting to be updated.
     *
     * @return the name value.
     */
    public String getName() {
        return this.name;
    }

    /**
     * Get the value property: The value of the pool setting.
     *
     * @return the value value.
     */
    public String getValue() {
        return this.value;
    }

    /**
     * Get the type property: The type specifier of the value.
     *
     * @return the type value.
     */
    public KeyVaultSettingType getType() {
        return this.type;
    }

    /**
     * Set the type property: The type specifier of the value.
     *
     * @param type the type value to set.
     * @return the Setting object itself.
     */
    public Setting setType(KeyVaultSettingType type) {
        this.type = type;
        return this;
    }

    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeStringField("name", this.name);
        jsonWriter.writeStringField("value", this.value);
        jsonWriter.writeStringField("type", Objects.toString(this.type, null));
        return jsonWriter.writeEndObject();
    }

    /**
     * Reads an instance of Setting from the JsonReader.
     *
     * @param jsonReader The JsonReader being read.
     * @return An instance of Setting 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 Setting.
     */
    public static Setting fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(
                reader -> {
                    boolean nameFound = false;
                    String name = null;
                    boolean valueFound = false;
                    String value = null;
                    KeyVaultSettingType type = null;
                    while (reader.nextToken() != JsonToken.END_OBJECT) {
                        String fieldName = reader.getFieldName();
                        reader.nextToken();

                        if ("name".equals(fieldName)) {
                            name = reader.getString();
                            nameFound = true;
                        } else if ("value".equals(fieldName)) {
                            value = reader.getString();
                            valueFound = true;
                        } else if ("type".equals(fieldName)) {
                            type = KeyVaultSettingType.fromString(reader.getString());
                        } else {
                            reader.skipChildren();
                        }
                    }
                    if (nameFound && valueFound) {
                        Setting deserializedSetting = new Setting(name, value);
                        deserializedSetting.type = type;

                        return deserializedSetting;
                    }
                    List<String> missingProperties = new ArrayList<>();
                    if (!nameFound) {
                        missingProperties.add("name");
                    }
                    if (!valueFound) {
                        missingProperties.add("value");
                    }

                    throw new IllegalStateException(
                            "Missing required property/properties: " + String.join(", ", missingProperties));
                });
    }
}
