// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System.Collections.Generic;
using System.Text.Json;
using Azure;
using Azure.Core;

namespace NotificationHubs.Models
{
    public partial class Installation : IUtf8JsonSerializable
    {
        void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
        {
            writer.WriteStartObject();
            writer.WritePropertyName("installationId");
            writer.WriteStringValue(InstallationId);
            if (Optional.IsDefined(UserId))
            {
                writer.WritePropertyName("userId");
                writer.WriteStringValue(UserId);
            }
            writer.WritePropertyName("tags");
            writer.WriteStartArray();
            foreach (var item in Tags)
            {
                writer.WriteStringValue(item);
            }
            writer.WriteEndArray();
            writer.WritePropertyName("templates");
            writer.WriteStartObject();
            foreach (var item in Templates)
            {
                writer.WritePropertyName(item.Key);
                writer.WriteStringValue(item.Value);
            }
            writer.WriteEndObject();
            writer.WriteEndObject();
        }

        internal static Installation DeserializeInstallation(JsonElement element)
        {
            string installationId = default;
            Optional<string> userId = default;
            Optional<string> expirationTime = default;
            Optional<string> lastUpdate = default;
            IList<string> tags = default;
            IDictionary<string, string> templates = default;
            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("installationId"))
                {
                    installationId = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("userId"))
                {
                    userId = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("expirationTime"))
                {
                    expirationTime = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("lastUpdate"))
                {
                    lastUpdate = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("tags"))
                {
                    List<string> array = new List<string>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(item.GetString());
                    }
                    tags = array;
                    continue;
                }
                if (property.NameEquals("templates"))
                {
                    Dictionary<string, string> dictionary = new Dictionary<string, string>();
                    foreach (var property0 in property.Value.EnumerateObject())
                    {
                        dictionary.Add(property0.Name, property0.Value.GetString());
                    }
                    templates = dictionary;
                    continue;
                }
            }
            return new Installation(installationId, userId, expirationTime, lastUpdate, tags, templates);
        }

        internal RequestContent ToRequestContent()
        {
            var content = new Utf8JsonRequestContent();
            content.JsonWriter.WriteObjectValue(this);
            return content;
        }

        internal static Installation FromResponse(Response response)
        {
            using var document = JsonDocument.Parse(response.Content);
            return DeserializeInstallation(document.RootElement);
        }
    }
}
