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

package tsptest.errormodel.models;

import com.azure.core.annotation.Generated;
import com.azure.core.annotation.Immutable;
import com.azure.core.models.ResponseError;
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.List;

/**
 * The SubError model.
 */
@Immutable
public final class SubError implements JsonSerializable<SubError> {
    /*
     * One of a server-defined set of error codes.
     */
    @Generated
    private final String code;

    /*
     * A human-readable representation of the error.
     */
    @Generated
    private final String message;

    /*
     * The target of the error.
     */
    @Generated
    private String target;

    /*
     * An array of details about specific errors that led to this reported error.
     */
    @Generated
    private List<ResponseError> details;

    /*
     * An object containing more specific information than the current object about the error.
     */
    @Generated
    private InnerError innerError;

    /*
     * The subCode property.
     */
    @Generated
    private final String subCode;

    /**
     * Creates an instance of SubError class.
     * 
     * @param code the code value to set.
     * @param message the message value to set.
     * @param subCode the subCode value to set.
     */
    @Generated
    private SubError(String code, String message, String subCode) {
        this.code = code;
        this.message = message;
        this.subCode = subCode;
    }

    /**
     * Get the code property: One of a server-defined set of error codes.
     * 
     * @return the code value.
     */
    @Generated
    public String getCode() {
        return this.code;
    }

    /**
     * Get the message property: A human-readable representation of the error.
     * 
     * @return the message value.
     */
    @Generated
    public String getMessage() {
        return this.message;
    }

    /**
     * Get the target property: The target of the error.
     * 
     * @return the target value.
     */
    @Generated
    public String getTarget() {
        return this.target;
    }

    /**
     * Get the details property: An array of details about specific errors that led to this reported error.
     * 
     * @return the details value.
     */
    @Generated
    public List<ResponseError> getDetails() {
        return this.details;
    }

    /**
     * Get the innerError property: An object containing more specific information than the current object about the
     * error.
     * 
     * @return the innerError value.
     */
    @Generated
    public InnerError getInnerError() {
        return this.innerError;
    }

    /**
     * Get the subCode property: The subCode property.
     * 
     * @return the subCode value.
     */
    @Generated
    public String getSubCode() {
        return this.subCode;
    }

    /**
     * {@inheritDoc}
     */
    @Generated
    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeStringField("code", this.code);
        jsonWriter.writeStringField("message", this.message);
        jsonWriter.writeStringField("subCode", this.subCode);
        jsonWriter.writeStringField("target", this.target);
        jsonWriter.writeArrayField("details", this.details, (writer, element) -> writer.writeJson(element));
        jsonWriter.writeJsonField("innererror", this.innerError);
        return jsonWriter.writeEndObject();
    }

    /**
     * Reads an instance of SubError from the JsonReader.
     * 
     * @param jsonReader The JsonReader being read.
     * @return An instance of SubError 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 SubError.
     */
    @Generated
    public static SubError fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(reader -> {
            String code = null;
            String message = null;
            String subCode = null;
            String target = null;
            List<ResponseError> details = null;
            InnerError innerError = null;
            while (reader.nextToken() != JsonToken.END_OBJECT) {
                String fieldName = reader.getFieldName();
                reader.nextToken();

                if ("code".equals(fieldName)) {
                    code = reader.getString();
                } else if ("message".equals(fieldName)) {
                    message = reader.getString();
                } else if ("subCode".equals(fieldName)) {
                    subCode = reader.getString();
                } else if ("target".equals(fieldName)) {
                    target = reader.getString();
                } else if ("details".equals(fieldName)) {
                    details = reader.readArray(reader1 -> ResponseError.fromJson(reader1));
                } else if ("innererror".equals(fieldName)) {
                    innerError = InnerError.fromJson(reader);
                } else {
                    reader.skipChildren();
                }
            }
            SubError deserializedSubError = new SubError(code, message, subCode);
            deserializedSubError.target = target;
            deserializedSubError.details = details;
            deserializedSubError.innerError = innerError;

            return deserializedSubError;
        });
    }
}
