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

package azure.resourcemanager.multiservicesharedmodels.combined.models;

import com.azure.core.annotation.Fluent;
import com.azure.core.util.CoreUtils;
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.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * Common metadata shared across multiple services.
 */
@Fluent
public final class SharedMetadata implements JsonSerializable<SharedMetadata> {
    /*
     * Creation timestamp of the resource.
     */
    private OffsetDateTime createdAt;

    /*
     * Creator of the resource.
     */
    private String createdBy;

    /*
     * Tags associated with the resource.
     */
    private Map<String, String> tags;

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

    /**
     * Get the createdAt property: Creation timestamp of the resource.
     * 
     * @return the createdAt value.
     */
    public OffsetDateTime createdAt() {
        return this.createdAt;
    }

    /**
     * Set the createdAt property: Creation timestamp of the resource.
     * 
     * @param createdAt the createdAt value to set.
     * @return the SharedMetadata object itself.
     */
    public SharedMetadata withCreatedAt(OffsetDateTime createdAt) {
        this.createdAt = createdAt;
        return this;
    }

    /**
     * Get the createdBy property: Creator of the resource.
     * 
     * @return the createdBy value.
     */
    public String createdBy() {
        return this.createdBy;
    }

    /**
     * Set the createdBy property: Creator of the resource.
     * 
     * @param createdBy the createdBy value to set.
     * @return the SharedMetadata object itself.
     */
    public SharedMetadata withCreatedBy(String createdBy) {
        this.createdBy = createdBy;
        return this;
    }

    /**
     * Get the tags property: Tags associated with the resource.
     * 
     * @return the tags value.
     */
    public Map<String, String> tags() {
        return this.tags;
    }

    /**
     * Set the tags property: Tags associated with the resource.
     * 
     * @param tags the tags value to set.
     * @return the SharedMetadata object itself.
     */
    public SharedMetadata withTags(Map<String, String> tags) {
        this.tags = tags;
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeStringField("createdAt",
            this.createdAt == null ? null : DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(this.createdAt));
        jsonWriter.writeStringField("createdBy", this.createdBy);
        jsonWriter.writeMapField("tags", this.tags, (writer, element) -> writer.writeString(element));
        return jsonWriter.writeEndObject();
    }

    /**
     * Reads an instance of SharedMetadata from the JsonReader.
     * 
     * @param jsonReader The JsonReader being read.
     * @return An instance of SharedMetadata if the JsonReader was pointing to an instance of it, or null if it was
     * pointing to JSON null.
     * @throws IOException If an error occurs while reading the SharedMetadata.
     */
    public static SharedMetadata fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(reader -> {
            SharedMetadata deserializedSharedMetadata = new SharedMetadata();
            while (reader.nextToken() != JsonToken.END_OBJECT) {
                String fieldName = reader.getFieldName();
                reader.nextToken();

                if ("createdAt".equals(fieldName)) {
                    deserializedSharedMetadata.createdAt = reader
                        .getNullable(nonNullReader -> CoreUtils.parseBestOffsetDateTime(nonNullReader.getString()));
                } else if ("createdBy".equals(fieldName)) {
                    deserializedSharedMetadata.createdBy = reader.getString();
                } else if ("tags".equals(fieldName)) {
                    Map<String, String> tags = reader.readMap(reader1 -> reader1.getString());
                    deserializedSharedMetadata.tags = tags;
                } else {
                    reader.skipChildren();
                }
            }

            return deserializedSharedMetadata;
        });
    }
}
