// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

package com.azure.search.documents.indexes.models;

import com.azure.core.annotation.Fluent;
import com.azure.json.JsonReader;
import com.azure.json.JsonToken;
import com.azure.json.JsonWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Allows you to configure normalization for filterable, sortable, and facetable fields, which by default operate with
 * strict matching. This is a user-defined configuration consisting of at least one or more filters, which modify the
 * token that is stored.
 */
@Fluent
public final class CustomNormalizer extends LexicalNormalizer {
    /*
     * Identifies the concrete type of the normalizer.
     */
    private static final String ODATA_TYPE = "#Microsoft.Azure.Search.CustomNormalizer";

    /*
     * A list of token filters used to filter out or modify the input token. For example, you can specify a lowercase
     * filter that converts all characters to lowercase. The filters are run in the order in which they are listed.
     */
    private List<TokenFilterName> tokenFilters;

    /*
     * A list of character filters used to prepare input text before it is processed. For instance, they can replace
     * certain characters or symbols. The filters are run in the order in which they are listed.
     */
    private List<CharFilterName> charFilters;

    /**
     * Creates an instance of CustomNormalizer class.
     *
     * @param name the name value to set.
     */
    public CustomNormalizer(String name) {
        super(name);
    }

    /**
     * Get the tokenFilters property: A list of token filters used to filter out or modify the input token. For example,
     * you can specify a lowercase filter that converts all characters to lowercase. The filters are run in the order in
     * which they are listed.
     *
     * @return the tokenFilters value.
     */
    public List<TokenFilterName> getTokenFilters() {
        return this.tokenFilters;
    }

    /**
     * Set the tokenFilters property: A list of token filters used to filter out or modify the input token. For example,
     * you can specify a lowercase filter that converts all characters to lowercase. The filters are run in the order in
     * which they are listed.
     *
     * @param tokenFilters the tokenFilters value to set.
     * @return the CustomNormalizer object itself.
     */
    public CustomNormalizer setTokenFilters(List<TokenFilterName> tokenFilters) {
        this.tokenFilters = tokenFilters;
        return this;
    }

    /**
     * Get the charFilters property: A list of character filters used to prepare input text before it is processed. For
     * instance, they can replace certain characters or symbols. The filters are run in the order in which they are
     * listed.
     *
     * @return the charFilters value.
     */
    public List<CharFilterName> getCharFilters() {
        return this.charFilters;
    }

    /**
     * Set the charFilters property: A list of character filters used to prepare input text before it is processed. For
     * instance, they can replace certain characters or symbols. The filters are run in the order in which they are
     * listed.
     *
     * @param charFilters the charFilters value to set.
     * @return the CustomNormalizer object itself.
     */
    public CustomNormalizer setCharFilters(List<CharFilterName> charFilters) {
        this.charFilters = charFilters;
        return this;
    }

    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeStringField("@odata.type", ODATA_TYPE);
        jsonWriter.writeStringField("name", getName());
        jsonWriter.writeArrayField(
                "tokenFilters",
                this.tokenFilters,
                (writer, element) -> writer.writeString(Objects.toString(element, null)));
        jsonWriter.writeArrayField(
                "charFilters",
                this.charFilters,
                (writer, element) -> writer.writeString(Objects.toString(element, null)));
        return jsonWriter.writeEndObject();
    }

    /**
     * Reads an instance of CustomNormalizer from the JsonReader.
     *
     * @param jsonReader The JsonReader being read.
     * @return An instance of CustomNormalizer 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 or the
     *     polymorphic discriminator.
     * @throws IOException If an error occurs while reading the CustomNormalizer.
     */
    public static CustomNormalizer fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(
                reader -> {
                    boolean nameFound = false;
                    String name = null;
                    List<TokenFilterName> tokenFilters = null;
                    List<CharFilterName> charFilters = null;
                    while (reader.nextToken() != JsonToken.END_OBJECT) {
                        String fieldName = reader.getFieldName();
                        reader.nextToken();

                        if ("@odata.type".equals(fieldName)) {
                            String odataType = reader.getString();
                            if (!ODATA_TYPE.equals(odataType)) {
                                throw new IllegalStateException(
                                        "'@odata.type' was expected to be non-null and equal to '"
                                                + ODATA_TYPE
                                                + "'. The found '@odata.type' was '"
                                                + odataType
                                                + "'.");
                            }
                        } else if ("name".equals(fieldName)) {
                            name = reader.getString();
                            nameFound = true;
                        } else if ("tokenFilters".equals(fieldName)) {
                            tokenFilters = reader.readArray(reader1 -> TokenFilterName.fromString(reader1.getString()));
                        } else if ("charFilters".equals(fieldName)) {
                            charFilters = reader.readArray(reader1 -> CharFilterName.fromString(reader1.getString()));
                        } else {
                            reader.skipChildren();
                        }
                    }
                    if (nameFound) {
                        CustomNormalizer deserializedCustomNormalizer = new CustomNormalizer(name);
                        deserializedCustomNormalizer.tokenFilters = tokenFilters;
                        deserializedCustomNormalizer.charFilters = charFilters;

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

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

    /**
     * Set the tokenFilters property: A list of token filters used to filter out or modify the input token. For example,
     * you can specify a lowercase filter that converts all characters to lowercase. The filters are run in the order in
     * which they are listed.
     *
     * @param tokenFilters the tokenFilters value to set.
     * @return the CustomNormalizer object itself.
     */
    public CustomNormalizer setTokenFilters(TokenFilterName... tokenFilters) {
        this.tokenFilters = (tokenFilters == null) ? null : java.util.Arrays.asList(tokenFilters);
        return this;
    }

    /**
     * Set the charFilters property: A list of character filters used to prepare input text before it is processed. For
     * instance, they can replace certain characters or symbols. The filters are run in the order in which they are
     * listed.
     *
     * @param charFilters the charFilters value to set.
     * @return the CustomNormalizer object itself.
     */
    public CustomNormalizer setCharFilters(CharFilterName... charFilters) {
        this.charFilters = (charFilters == null) ? null : java.util.Arrays.asList(charFilters);
        return this;
    }
}
