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

package com.azure.data.appconfiguration.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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/** Enables filtering of key-values. */
@Fluent
public final class SnapshotSettingFilter implements JsonSerializable<SnapshotSettingFilter> {
    /*
     * Filters key-values by their key field.
     */
    private final String key;

    /*
     * Filters key-values by their label field.
     */
    private String label;

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

    /**
     * Get the key property: Filters {@link ConfigurationSetting} by their key field.
     *
     * @return the key value.
     */
    public String getKey() {
        return this.key;
    }

    /**
     * Get the label property: Filters {@link ConfigurationSetting} by their label field.
     *
     * @return the label value.
     */
    public String getLabel() {
        return this.label;
    }

    /**
     * Set the label property: Filters {@link ConfigurationSetting} by their label field.
     *
     * @param label the label value to set.
     * @return the SnapshotSettingFilter object itself.
     */
    public SnapshotSettingFilter setLabel(String label) {
        this.label = label;
        return this;
    }

    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeStringField("key", this.key);
        jsonWriter.writeStringField("label", this.label);
        return jsonWriter.writeEndObject();
    }

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

                        if ("key".equals(fieldName)) {
                            key = reader.getString();
                            keyFound = true;
                        } else if ("label".equals(fieldName)) {
                            label = reader.getString();
                        } else {
                            reader.skipChildren();
                        }
                    }
                    if (keyFound) {
                        SnapshotSettingFilter deserializedSnapshotSettingFilter = new SnapshotSettingFilter(key);
                        deserializedSnapshotSettingFilter.label = label;

                        return deserializedSnapshotSettingFilter;
                    }
                    List<String> missingProperties = new ArrayList<>();
                    if (!keyFound) {
                        missingProperties.add("key");
                    }

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